update-tolerate

This commit is contained in:
Untone 2024-02-27 16:28:54 +03:00
parent 2e68128dfc
commit eb295549fb

View File

@ -173,77 +173,85 @@ def patch_topics(session, shout, topics_input):
@mutation.field('update_shout') @mutation.field('update_shout')
@login_required @login_required
async def update_shout(_, info, shout_id, shout_input=None, publish=False): async def update_shout(_, info, shout_id, shout_input=None, publish=False):
user_id = info.context['user_id'] try:
roles = info.context['roles'] user_id = info.context.get('user_id')
shout_input = shout_input or {} if not user_id:
with local_session() as session: return {"error": "unauthorized"}
author = session.query(Author).filter(Author.user == user_id).first() roles = info.context.get('roles')
current_time = int(time.time()) shout_input = shout_input or {}
shout_id = shout_id or shout_input.get('id') with local_session() as session:
slug = shout_input.get('slug') author = session.query(Author).filter(Author.user == user_id).first()
if slug: current_time = int(time.time())
shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first() shout_id = shout_id or shout_input.get('id')
if shout_by_id and slug != shout_by_id.slug: slug = shout_input.get('slug')
same_slug_shout = ( if slug:
session.query(Shout)
.filter(Shout.slug == shout_input.get('slug')) shout_by_id = session.query(Shout).filter(Shout.id == shout_id).first()
.first() if shout_by_id and slug != shout_by_id.slug:
)
c = 1
while same_slug_shout is not None:
same_slug_shout = ( same_slug_shout = (
session.query(Shout) session.query(Shout)
.filter(Shout.slug == shout_input.get('slug')) .filter(Shout.slug == shout_input.get('slug'))
.first() .first()
) )
c += 1 c = 1
slug += f'-{c}' while same_slug_shout is not None:
shout_input['slug'] = slug same_slug_shout = (
if isinstance(author, Author) and isinstance(shout_id, int): session.query(Shout)
shout = ( .filter(Shout.slug == shout_input.get('slug'))
session.query(Shout) .first()
.options(joinedload(Shout.authors), joinedload(Shout.topics)) )
.filter(Shout.id == shout_id) c += 1
.first() slug += f'-{c}'
) shout_input['slug'] = slug
if not shout: if isinstance(author, Author) and isinstance(shout_id, int):
return {'error': 'shout not found'} shout = (
if ( session.query(Shout)
shout.created_by is not author.id .options(joinedload(Shout.authors), joinedload(Shout.topics))
and author.id not in shout.authors .filter(Shout.id == shout_id)
and 'editor' not in roles .first()
): )
return {'error': 'access denied'}
# topics patch if not shout:
topics_input = shout_input.get('topics') return {'error': 'shout not found'}
if topics_input: if (
patch_topics(session, shout, topics_input) shout.created_by is not author.id
del shout_input['topics'] and author.id not in shout.authors
and 'editor' not in roles
):
return {'error': 'access denied'}
# main topic # topics patch
main_topic = shout_input.get('main_topic') topics_input = shout_input.get('topics')
if main_topic: if topics_input:
patch_main_topic(session, main_topic, shout) patch_topics(session, shout, topics_input)
del shout_input['topics']
shout_input['updated_at'] = current_time # main topic
shout_input['published_at'] = current_time if publish else None main_topic = shout_input.get('main_topic')
Shout.update(shout, shout_input) if main_topic:
session.add(shout) patch_main_topic(session, main_topic, shout)
session.commit()
shout_dict = shout.dict() shout_input['updated_at'] = current_time
shout_input['published_at'] = current_time if publish else None
Shout.update(shout, shout_input)
session.add(shout)
session.commit()
if not publish: shout_dict = shout.dict()
await notify_shout(shout_dict, 'update')
else: if not publish:
await notify_shout(shout_dict, 'published') await notify_shout(shout_dict, 'update')
# search service indexing else:
search_service.index(shout) await notify_shout(shout_dict, 'published')
# search service indexing
search_service.index(shout)
return {'shout': shout_dict}
except Exception as exc:
logger.error(exc)
logger.error(f' cannot update with data: {shout_input}')
return {'shout': shout_dict}
logger.debug(f' cannot update with data: {shout_input}')
return {'error': 'cant update shout'} return {'error': 'cant update shout'}