From d6151c00c825dad45ca8fd2cd2dfec4b0f71d996 Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 2 Feb 2024 23:59:42 +0300 Subject: [PATCH] update-fix-2 --- resolvers/editor.py | 164 ++++++++++++++++++++++++-------------------- 1 file changed, 88 insertions(+), 76 deletions(-) diff --git a/resolvers/editor.py b/resolvers/editor.py index 06162b9a..3cdc1ba8 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -87,6 +87,75 @@ async def create_shout(_, info, inp): return {'error': 'cant create shout'} +def patch_main_topic(session, main_topic, shout): + old_main_topic = ( + session.query(ShoutTopic) + .filter( + and_( + ShoutTopic.shout == shout.id, + ShoutTopic.main == True, + ) + ) + .first() + ) + + main_topic = session.query(Topic).filter(Topic.slug == main_topic).first() + + if main_topic: + new_main_topic = ( + session.query(ShoutTopic) + .filter( + and_( + ShoutTopic.shout == shout.id, + ShoutTopic.topic == main_topic.id, + ) + ) + .first() + ) + + if old_main_topic and new_main_topic and old_main_topic is not new_main_topic: + ShoutTopic.update(old_main_topic, {'main': False}) + session.add(old_main_topic) + + ShoutTopic.update(new_main_topic, {'main': True}) + session.add(new_main_topic) + + +def patch_topics(session, shout, topics_input): + new_topics_to_link = [Topic(**new_topic) for new_topic in topics_input if new_topic['id'] < 0] + if new_topics_to_link: + session.add_all(new_topics_to_link) + session.commit() + + for new_topic_to_link in new_topics_to_link: + created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id) + session.add(created_unlinked_topic) + + existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0] + existing_topic_to_link_ids = [ + existing_topic_input['id'] + for existing_topic_input in existing_topics_input + if existing_topic_input['id'] not in [topic.id for topic in shout.topics] + ] + + for existing_topic_to_link_id in existing_topic_to_link_ids: + created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id) + session.add(created_unlinked_topic) + + topic_to_unlink_ids = [ + topic.id + for topic in shout.topics + if topic.id not in [topic_input['id'] for topic_input in existing_topics_input] + ] + + session.query(ShoutTopic).filter( + and_( + ShoutTopic.shout == shout.id, + ShoutTopic.topic.in_(topic_to_unlink_ids), + ) + ).delete(synchronize_session=False) + + @mutation.field('update_shout') @login_required async def update_shout(_, info, shout_id, shout_input=None, publish=False): @@ -95,7 +164,6 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False): shout_input = {} with local_session() as session: author = session.query(Author).filter(Author.user == user_id).first() - shout_dict = None current_time = int(time.time()) if author: shout = ( @@ -111,91 +179,35 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False): return {'error': 'shout not found'} if shout.created_by is not author.id and author.id not in shout.authors: return {'error': 'access denied'} - topics_input = shout_input['topics'] - del shout_input['topics'] - new_topics_to_link = [Topic(**new_topic) for new_topic in topics_input if new_topic['id'] < 0] - if new_topics_to_link: - session.add_all(new_topics_to_link) - session.commit() - for new_topic_to_link in new_topics_to_link: - created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id) - session.add(created_unlinked_topic) + # topics patch + topics_input = shout_input.get('topics') + if topics_input: + patch_topics(session, shout, topics_input) + del shout_input['topics'] - existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0] - existing_topic_to_link_ids = [ - existing_topic_input['id'] - for existing_topic_input in existing_topics_input - if existing_topic_input['id'] not in [topic.id for topic in shout.topics] - ] - - for existing_topic_to_link_id in existing_topic_to_link_ids: - created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id) - session.add(created_unlinked_topic) - - topic_to_unlink_ids = [ - topic.id - for topic in shout.topics - if topic.id not in [topic_input['id'] for topic_input in existing_topics_input] - ] - - session.query(ShoutTopic).filter( - and_( - ShoutTopic.shout == shout.id, - ShoutTopic.topic.in_(topic_to_unlink_ids), - ) - ).delete(synchronize_session=False) + # main topic + main_topic = shout_input.get('main_topic') + if main_topic: + patch_main_topic(main_topic) 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() - # main topic - if 'main_topic' in shout_input: - old_main_topic = ( - session.query(ShoutTopic) - .filter( - and_( - ShoutTopic.shout == shout.id, - ShoutTopic.main == True, - ) - ) - .first() - ) + shout_dict = shout.dict() - main_topic = session.query(Topic).filter(Topic.slug == shout_input['main_topic']).first() + if not publish: + await notify_shout(shout_dict, 'update') + else: + await notify_shout(shout_dict, 'published') + # search service indexing + search_service.index(shout) - if main_topic: - new_main_topic = ( - session.query(ShoutTopic) - .filter( - and_( - ShoutTopic.shout == shout.id, - ShoutTopic.topic == main_topic.id, - ) - ) - .first() - ) - - if old_main_topic and new_main_topic and old_main_topic is not new_main_topic: - ShoutTopic.update(old_main_topic, {'main': False}) - session.add(old_main_topic) - - ShoutTopic.update(new_main_topic, {'main': True}) - session.add(new_main_topic) - - shout_dict = shout.dict() - session.commit() - - if not publish: - await notify_shout(shout_dict, 'update') - else: - await notify_shout(shout_dict, 'published') - # search service indexing - search_service.index(shout) - - return {'shout': shout_dict} + return {'shout': shout_dict} + return {'error': 'cannot update'} @mutation.field('delete_shout')