refactored-get-my-shout

This commit is contained in:
Untone 2024-03-07 14:42:48 +03:00
parent 6f3ed3704a
commit 0d111bda47

View File

@ -22,25 +22,23 @@ from services.logger import root_logger as logger
@query.field('get_my_shout') @query.field('get_my_shout')
@login_required @login_required
async def get_my_shout(_, info, shout_id: int): 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: 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() shout = session.query(Shout).filter(Shout.id == shout_id).first()
if shout and author: if not shout:
if not shout.published_at: return {'error': 'no shout found', 'shout': None}
user_id = info.context.get('user_id', '') if not shout.published_at:
roles = info.context.get('roles', []) author = session.query(Author).filter(Author.user == user_id).first()
if 'editor' in roles or filter( if not author:
lambda x: x.id == author.id, [x for x in shout.authors] return {'error': 'no author found', 'shout': None}
): roles = info.context.get('roles', [])
error = None if 'editor' not in roles and not filter(
else: lambda x: x.id == author.id, [x for x in shout.authors]
error = 'forbidden' ):
shout = None return {'error': 'forbidden', 'shout': None}
return {'error': error, 'shout': shout.dict()} return {'error': None, 'shout': shout.dict()}
return {'error': 'no shout found', 'shout': None}
@query.field('get_shouts_drafts') @query.field('get_shouts_drafts')