update-shout-fix-2

This commit is contained in:
Untone 2024-03-05 17:53:49 +03:00
parent 12a9880815
commit 7c75c2accc

View File

@ -178,17 +178,18 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
user_id = info.context.get('user_id') user_id = info.context.get('user_id')
roles = info.context.get('roles', []) roles = info.context.get('roles', [])
shout_input = shout_input or {} shout_input = shout_input or {}
current_time = int(time.time())
shout_id = shout_id or shout_input.get('id')
slug = shout_input.get('slug')
if not user_id: if not user_id:
return {"error": "unauthorized"} return {"error": "unauthorized"}
try: try:
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
current_time = int(time.time()) if author:
shout_id = shout_id or shout_input.get('id')
slug = shout_input.get('slug')
if slug:
shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first() shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first()
if not shout_by_id:
return {'error': 'shout not found'}
if shout_by_id and slug != shout_by_id.slug: if shout_by_id and slug != shout_by_id.slug:
same_slug_shout = ( same_slug_shout = (
session.query(Shout) session.query(Shout)
@ -206,19 +207,9 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
) )
shout_input['slug'] = slug shout_input['slug'] = slug
if author and isinstance(shout_id, int):
shout = (
session.query(Shout)
.options(joinedload(Shout.authors), joinedload(Shout.topics))
.filter(Shout.id == shout_id)
.first()
)
if not shout:
return {'error': 'shout not found'}
if ( if (
shout.created_by != author.id shout_by_id.created_by != author.id
and not filter(lambda x: x == author.id, shout.authors) and not filter(lambda x: x == author.id, shout_by_id.authors)
and 'editor' not in roles and 'editor' not in roles
): ):
return {'error': 'access denied'} return {'error': 'access denied'}
@ -226,28 +217,28 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
# topics patch # topics patch
topics_input = shout_input.get('topics') topics_input = shout_input.get('topics')
if topics_input: if topics_input:
patch_topics(session, shout, topics_input) patch_topics(session, shout_by_id, topics_input)
del shout_input['topics'] del shout_input['topics']
# main topic # main topic
main_topic = shout_input.get('main_topic') main_topic = shout_input.get('main_topic')
if main_topic: if main_topic:
patch_main_topic(session, main_topic, shout) patch_main_topic(session, main_topic, shout_by_id)
shout_input['updated_at'] = current_time shout_input['updated_at'] = current_time
shout_input['published_at'] = current_time if publish else None shout_input['published_at'] = current_time if publish else None
Shout.update(shout, shout_input) Shout.update(shout_by_id, shout_input)
session.add(shout) session.add(shout_by_id)
session.commit() session.commit()
shout_dict = shout.dict() shout_dict = shout_by_id.dict()
if not publish: if not publish:
await notify_shout(shout_dict, 'update') await notify_shout(shout_dict, 'update')
else: else:
await notify_shout(shout_dict, 'published') await notify_shout(shout_dict, 'published')
# search service indexing # search service indexing
search_service.index(shout) search_service.index(shout_by_id)
return {'shout': shout_dict} return {'shout': shout_dict}
except Exception as exc: except Exception as exc: