From 0d111bda47f266b5f743b8ac28624354d62073b1 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 7 Mar 2024 14:42:48 +0300 Subject: [PATCH] refactored-get-my-shout --- resolvers/editor.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/resolvers/editor.py b/resolvers/editor.py index 434fda57..76016bb7 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -22,25 +22,23 @@ from services.logger import root_logger as logger @query.field('get_my_shout') @login_required async def get_my_shout(_, info, shout_id: int): - user_id = info.context.get('user_id') - shout = None - error = None with local_session() as session: - author = session.query(Author).filter(Author.user == user_id).first() + user_id = info.context.get('user_id', '') + if not user_id: + return {'error': 'unauthorized', 'shout': None} shout = session.query(Shout).filter(Shout.id == shout_id).first() - if shout and author: - if not shout.published_at: - user_id = info.context.get('user_id', '') - roles = info.context.get('roles', []) - if 'editor' in roles or filter( - lambda x: x.id == author.id, [x for x in shout.authors] - ): - error = None - else: - error = 'forbidden' - shout = None - return {'error': error, 'shout': shout.dict()} - return {'error': 'no shout found', 'shout': None} + if not shout: + return {'error': 'no shout found', 'shout': None} + if not shout.published_at: + author = session.query(Author).filter(Author.user == user_id).first() + if not author: + return {'error': 'no author found', 'shout': None} + roles = info.context.get('roles', []) + if 'editor' not in roles and not filter( + lambda x: x.id == author.id, [x for x in shout.authors] + ): + return {'error': 'forbidden', 'shout': None} + return {'error': None, 'shout': shout.dict()} @query.field('get_shouts_drafts')