This commit is contained in:
parent
3e58164ae8
commit
f1444cbe10
|
@ -5,8 +5,7 @@ from sqlalchemy import and_, desc, select, or_, distinct, func
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from orm.author import Author, AuthorFollower, AuthorRating
|
from orm.author import Author, AuthorFollower, AuthorRating
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.shout import ShoutAuthor, ShoutTopic
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from resolvers.follower import query_follows
|
from resolvers.follower import query_follows
|
||||||
from resolvers.stat import get_authors_with_stat, execute_with_ministat, author_follows_authors, author_follows_topics
|
from resolvers.stat import get_authors_with_stat, execute_with_ministat, author_follows_authors, author_follows_topics
|
||||||
|
@ -37,64 +36,6 @@ def get_authors_all(_, _info):
|
||||||
return authors
|
return authors
|
||||||
|
|
||||||
|
|
||||||
def count_author_comments_rating(session, author_id) -> int:
|
|
||||||
replied_alias = aliased(Reaction)
|
|
||||||
replies_likes = (
|
|
||||||
session.query(replied_alias)
|
|
||||||
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
|
||||||
.where(
|
|
||||||
and_(
|
|
||||||
replied_alias.created_by == author_id,
|
|
||||||
replied_alias.kind == ReactionKind.COMMENT.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.filter(replied_alias.kind == ReactionKind.LIKE.value)
|
|
||||||
.count()
|
|
||||||
) or 0
|
|
||||||
replies_dislikes = (
|
|
||||||
session.query(replied_alias)
|
|
||||||
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
|
||||||
.where(
|
|
||||||
and_(
|
|
||||||
replied_alias.created_by == author_id,
|
|
||||||
replied_alias.kind == ReactionKind.COMMENT.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
|
||||||
.count()
|
|
||||||
) or 0
|
|
||||||
|
|
||||||
return replies_likes - replies_dislikes
|
|
||||||
|
|
||||||
|
|
||||||
def count_author_shouts_rating(session, author_id) -> int:
|
|
||||||
shouts_likes = (
|
|
||||||
session.query(Reaction, Shout)
|
|
||||||
.join(Shout, Shout.id == Reaction.shout)
|
|
||||||
.filter(
|
|
||||||
and_(
|
|
||||||
Shout.authors.any(id=author_id),
|
|
||||||
Reaction.kind == ReactionKind.LIKE.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.count()
|
|
||||||
or 0
|
|
||||||
)
|
|
||||||
shouts_dislikes = (
|
|
||||||
session.query(Reaction, Shout)
|
|
||||||
.join(Shout, Shout.id == Reaction.shout)
|
|
||||||
.filter(
|
|
||||||
and_(
|
|
||||||
Shout.authors.any(id=author_id),
|
|
||||||
Reaction.kind == ReactionKind.DISLIKE.value,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.count()
|
|
||||||
or 0
|
|
||||||
)
|
|
||||||
return shouts_likes - shouts_dislikes
|
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author')
|
@query.field('get_author')
|
||||||
def get_author(_, _info, slug='', author_id=None):
|
def get_author(_, _info, slug='', author_id=None):
|
||||||
q = None
|
q = None
|
||||||
|
|
|
@ -5,10 +5,9 @@ from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.topic import TopicFollower, Topic
|
from orm.topic import TopicFollower, Topic
|
||||||
from resolvers.author import count_author_shouts_rating, count_author_comments_rating
|
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from orm.author import AuthorFollower, Author, AuthorRating
|
from orm.author import AuthorFollower, Author, AuthorRating
|
||||||
from orm.shout import ShoutTopic, ShoutAuthor
|
from orm.shout import ShoutTopic, ShoutAuthor, Shout
|
||||||
from services.follows import update_author_cache
|
from services.follows import update_author_cache
|
||||||
from services.logger import root_logger as logger
|
from services.logger import root_logger as logger
|
||||||
|
|
||||||
|
@ -51,6 +50,64 @@ def add_author_stat_columns(q):
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
|
def count_author_comments_rating(session, author_id) -> int:
|
||||||
|
replied_alias = aliased(Reaction)
|
||||||
|
replies_likes = (
|
||||||
|
session.query(replied_alias)
|
||||||
|
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
||||||
|
.where(
|
||||||
|
and_(
|
||||||
|
replied_alias.created_by == author_id,
|
||||||
|
replied_alias.kind == ReactionKind.COMMENT.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(replied_alias.kind == ReactionKind.LIKE.value)
|
||||||
|
.count()
|
||||||
|
) or 0
|
||||||
|
replies_dislikes = (
|
||||||
|
session.query(replied_alias)
|
||||||
|
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
||||||
|
.where(
|
||||||
|
and_(
|
||||||
|
replied_alias.created_by == author_id,
|
||||||
|
replied_alias.kind == ReactionKind.COMMENT.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
||||||
|
.count()
|
||||||
|
) or 0
|
||||||
|
|
||||||
|
return replies_likes - replies_dislikes
|
||||||
|
|
||||||
|
|
||||||
|
def count_author_shouts_rating(session, author_id) -> int:
|
||||||
|
shouts_likes = (
|
||||||
|
session.query(Reaction, Shout)
|
||||||
|
.join(Shout, Shout.id == Reaction.shout)
|
||||||
|
.filter(
|
||||||
|
and_(
|
||||||
|
Shout.authors.any(id=author_id),
|
||||||
|
Reaction.kind == ReactionKind.LIKE.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.count()
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
shouts_dislikes = (
|
||||||
|
session.query(Reaction, Shout)
|
||||||
|
.join(Shout, Shout.id == Reaction.shout)
|
||||||
|
.filter(
|
||||||
|
and_(
|
||||||
|
Shout.authors.any(id=author_id),
|
||||||
|
Reaction.kind == ReactionKind.DISLIKE.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.count()
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
return shouts_likes - shouts_dislikes
|
||||||
|
|
||||||
|
|
||||||
def load_author_ratings(author: Author):
|
def load_author_ratings(author: Author):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
comments_count = (
|
comments_count = (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user