diff --git a/main.py b/main.py index 983710f4..67c1db6e 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ import os -import sentry_sdk from importlib import import_module from os.path import exists @@ -10,6 +9,7 @@ from starlette.routing import Route from services.rediscache import redis from services.schema import resolvers +from services.sentry import start_sentry from services.viewed import ViewedStorage from services.webhook import WebhookEndpoint from settings import DEV_SERVER_PID_FILE_NAME, MODE @@ -17,8 +17,6 @@ from settings import DEV_SERVER_PID_FILE_NAME, MODE import_module('resolvers') schema = make_executable_schema(load_schema_from_path('schema/'), resolvers) -# Initialize GlitchTip SDK with DSN -sentry_sdk.init("https://e8b4aabe17db4a7bbf703304cda33892@glitchtip.discours.io/1") async def start(): if MODE == 'development': @@ -39,9 +37,9 @@ app = Starlette( redis.connect, ViewedStorage.init, # search_service.info, - # start_sentry, + start_sentry, start, ], on_shutdown=[redis.disconnect], debug=True, -) \ No newline at end of file +) diff --git a/resolvers/author.py b/resolvers/author.py index c38cf317..e1b8f525 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -59,7 +59,9 @@ async def get_author(_, _info, slug='', author_id=0): author = None author_dict = None try: - author_query = select(Author).filter(or_(Author.slug == slug, Author.id == author_id)) + author_query = select(Author).filter( + or_(Author.slug == slug, Author.id == author_id) + ) result = await get_authors_with_stat_cached(author_query) if not result: raise ValueError('Author not found') @@ -68,7 +70,7 @@ async def get_author(_, _info, slug='', author_id=0): logger.debug(f'found @{slug} with id {author_id}') if isinstance(author, Author): if not author.stat: - [author] = get_with_stat(author_query) # FIXME: with_rating=True) + [author] = get_with_stat(author_query) # FIXME: with_rating=True) if author: await set_author_cache(author.dict()) logger.debug('updated author stored in cache') @@ -290,8 +292,12 @@ async def get_author_followers(_, _info, slug: str): try: with local_session() as session: author_alias = aliased(Author) - author_id = session.query(author_alias.id).filter(author_alias.slug == slug) - if author_id: + result = ( + session.query(author_alias).filter(author_alias.slug == slug).first() + ) + if result: + [author] = result + author_id = author.id cached = await redis.execute('GET', f'author:{author_id}:followers') if not cached: author_follower_alias = aliased(AuthorFollower, name='af') diff --git a/resolvers/editor.py b/resolvers/editor.py index 11ab1b43..ce338384 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -35,7 +35,7 @@ async def get_my_shout(_, info, shout_id: int): ) if not shout: return {'error': 'no shout found', 'shout': None} - if not shout.published_at: + if not bool(shout.published_at): author = session.query(Author).filter(Author.user == user_id).first() if not author: return {'error': 'no author found', 'shout': None} @@ -241,7 +241,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False): c = 1 while same_slug_shout is not None: c += 1 - slug += f'-{c}' + slug = f'{slug}-{c}' same_slug_shout = ( session.query(Shout).filter(Shout.slug == slug).first() ) diff --git a/resolvers/rating.py b/resolvers/rating.py index 2bcf6f77..2ec7956e 100644 --- a/resolvers/rating.py +++ b/resolvers/rating.py @@ -120,19 +120,19 @@ def get_author_rating_old(session, author: Author): def get_author_rating_shouts(session, author: Author) -> int: q = ( select( - func.coalesce(func.sum( - case( - (Reaction.kind == ReactionKind.LIKE.value, 1), - (Reaction.kind == ReactionKind.DISLIKE.value, -1), - else_=0 - ) - ), 0).label('shouts_rating') + func.coalesce( + func.sum( + case( + (Reaction.kind == ReactionKind.LIKE.value, 1), + (Reaction.kind == ReactionKind.DISLIKE.value, -1), + else_=0, + ) + ), + 0, + ).label('shouts_rating') ) .select_from(Reaction) - .outerjoin( - Shout, - Shout.authors.any(id=author.id) - ) + .outerjoin(Shout, Shout.authors.any(id=author.id)) .outerjoin( Reaction, and_( @@ -150,13 +150,16 @@ def get_author_rating_comments(session, author: Author) -> int: replied_comment = aliased(Reaction) q = ( select( - func.coalesce(func.sum( - case( - (Reaction.kind == ReactionKind.LIKE.value, 1), - (Reaction.kind == ReactionKind.DISLIKE.value, -1), - else_=0 - ) - ), 0).label('shouts_rating') + func.coalesce( + func.sum( + case( + (Reaction.kind == ReactionKind.LIKE.value, 1), + (Reaction.kind == ReactionKind.DISLIKE.value, -1), + else_=0, + ) + ), + 0, + ).label('shouts_rating') ) .select_from(Reaction) .outerjoin( @@ -164,7 +167,9 @@ def get_author_rating_comments(session, author: Author) -> int: and_( replied_comment.kind == ReactionKind.COMMENT.value, replied_comment.created_by == author.id, - Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]), + Reaction.kind.in_( + [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value] + ), Reaction.reply_to == replied_comment.id, Reaction.deleted_at.is_(None), ), @@ -179,26 +184,27 @@ def add_author_rating_columns(q, group_list): # old karma q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id) - q = q.add_columns(func.sum(case((AuthorRating.plus == true(), 1), else_=-1)).label('rating')) + q = q.add_columns( + func.sum(case((AuthorRating.plus == true(), 1), else_=-1)).label('rating') + ) # by shouts rating shout_reaction = aliased(Reaction) shouts_rating_subq = ( select( Author.id, - func.coalesce(func.sum( - case( - (shout_reaction.kind == ReactionKind.LIKE.value, 1), - (shout_reaction.kind == ReactionKind.DISLIKE.value, -1), - else_=0 + func.coalesce( + func.sum( + case( + (shout_reaction.kind == ReactionKind.LIKE.value, 1), + (shout_reaction.kind == ReactionKind.DISLIKE.value, -1), + else_=0, + ) ) - )).label('shouts_rating') + ).label('shouts_rating'), ) .select_from(shout_reaction) - .outerjoin( - Shout, - Shout.authors.any(id=Author.id) - ) + .outerjoin(Shout, Shout.authors.any(id=Author.id)) .outerjoin( shout_reaction, and_( @@ -218,25 +224,35 @@ def add_author_rating_columns(q, group_list): # by comments replied_comment = aliased(Reaction) reaction_2 = aliased(Reaction) - comments_subq = select( - Author.id, - func.coalesce(func.sum( - case( - (reaction_2.kind == ReactionKind.LIKE.value, 1), - (reaction_2.kind == ReactionKind.DISLIKE.value, -1), - else_=0 - ) - )).label('comments_rating'), - ).select_from(reaction_2).outerjoin( - replied_comment, - and_( - replied_comment.kind == ReactionKind.COMMENT.value, - replied_comment.created_by == Author.id, - reaction_2.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]), - reaction_2.reply_to == replied_comment.id, - reaction_2.deleted_at.is_(None) + comments_subq = ( + select( + Author.id, + func.coalesce( + func.sum( + case( + (reaction_2.kind == ReactionKind.LIKE.value, 1), + (reaction_2.kind == ReactionKind.DISLIKE.value, -1), + else_=0, + ) + ) + ).label('comments_rating'), ) - ).group_by(Author.id).subquery() + .select_from(reaction_2) + .outerjoin( + replied_comment, + and_( + replied_comment.kind == ReactionKind.COMMENT.value, + replied_comment.created_by == Author.id, + reaction_2.kind.in_( + [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value] + ), + reaction_2.reply_to == replied_comment.id, + reaction_2.deleted_at.is_(None), + ), + ) + .group_by(Author.id) + .subquery() + ) q = q.outerjoin(comments_subq, Author.id == comments_subq.c.id) q = q.add_columns(comments_subq.c.comments_rating) diff --git a/resolvers/stat.py b/resolvers/stat.py index 03dd274e..f3dd53aa 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -87,8 +87,7 @@ def add_author_stat_columns(q, with_rating=False): # Create a subquery for comments count sub_comments = ( select( - Author.id, - func.coalesce(func.count(Reaction.id)).label('comments_count') + Author.id, func.coalesce(func.count(Reaction.id)).label('comments_count') ) .outerjoin( Reaction, @@ -155,7 +154,11 @@ async def get_authors_with_stat_cached(q): with local_session() as session: for [x] in session.execute(q): stat_str = await redis.execute('GET', f'author:{x.id}') - x.stat = json.loads(stat_str).get('stat') if isinstance(stat_str, str) else {} + x.stat = ( + json.loads(stat_str).get('stat') + if isinstance(stat_str, str) + else {} + ) records.append(x) except Exception as exc: raise Exception(exc)