This commit is contained in:
parent
7cd7447796
commit
b0e981ece4
|
@ -91,6 +91,8 @@ async def create_shout(_, info, inp):
|
||||||
@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']
|
user_id = info.context['user_id']
|
||||||
|
if not shout_input:
|
||||||
|
shout_input = {}
|
||||||
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()
|
||||||
shout_dict = None
|
shout_dict = None
|
||||||
|
@ -109,90 +111,89 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||||
return {'error': 'shout not found'}
|
return {'error': 'shout not found'}
|
||||||
if shout.created_by is not author.id and author.id not in shout.authors:
|
if shout.created_by is not author.id and author.id not in shout.authors:
|
||||||
return {'error': 'access denied'}
|
return {'error': 'access denied'}
|
||||||
if shout_input is not None:
|
topics_input = shout_input['topics']
|
||||||
topics_input = shout_input['topics']
|
del 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]
|
||||||
new_topics_to_link = [Topic(**new_topic) for new_topic in topics_input if new_topic['id'] < 0]
|
if new_topics_to_link:
|
||||||
if new_topics_to_link:
|
session.add_all(new_topics_to_link)
|
||||||
session.add_all(new_topics_to_link)
|
session.commit()
|
||||||
session.commit()
|
|
||||||
|
|
||||||
for new_topic_to_link in new_topics_to_link:
|
for new_topic_to_link in new_topics_to_link:
|
||||||
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
|
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
|
||||||
session.add(created_unlinked_topic)
|
session.add(created_unlinked_topic)
|
||||||
|
|
||||||
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0]
|
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_to_link_ids = [
|
||||||
existing_topic_input['id']
|
existing_topic_input['id']
|
||||||
for existing_topic_input in existing_topics_input
|
for existing_topic_input in existing_topics_input
|
||||||
if existing_topic_input['id'] not in [topic.id for topic in shout.topics]
|
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:
|
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)
|
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id)
|
||||||
session.add(created_unlinked_topic)
|
session.add(created_unlinked_topic)
|
||||||
|
|
||||||
topic_to_unlink_ids = [
|
topic_to_unlink_ids = [
|
||||||
topic.id
|
topic.id
|
||||||
for topic in shout.topics
|
for topic in shout.topics
|
||||||
if topic.id not in [topic_input['id'] for topic_input in existing_topics_input]
|
if topic.id not in [topic_input['id'] for topic_input in existing_topics_input]
|
||||||
]
|
]
|
||||||
|
|
||||||
session.query(ShoutTopic).filter(
|
session.query(ShoutTopic).filter(
|
||||||
and_(
|
and_(
|
||||||
ShoutTopic.shout == shout.id,
|
ShoutTopic.shout == shout.id,
|
||||||
ShoutTopic.topic.in_(topic_to_unlink_ids),
|
ShoutTopic.topic.in_(topic_to_unlink_ids),
|
||||||
|
)
|
||||||
|
).delete(synchronize_session=False)
|
||||||
|
|
||||||
|
shout_input['updated_at'] = current_time
|
||||||
|
shout_input['published_at'] = current_time if publish else None
|
||||||
|
Shout.update(shout, shout_input)
|
||||||
|
session.add(shout)
|
||||||
|
|
||||||
|
# main topic
|
||||||
|
if 'main_topic' in shout_input:
|
||||||
|
old_main_topic = (
|
||||||
|
session.query(ShoutTopic)
|
||||||
|
.filter(
|
||||||
|
and_(
|
||||||
|
ShoutTopic.shout == shout.id,
|
||||||
|
ShoutTopic.main == True,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
).delete(synchronize_session=False)
|
.first()
|
||||||
|
)
|
||||||
|
|
||||||
shout_input['updated_at'] = current_time
|
main_topic = session.query(Topic).filter(Topic.slug == shout_input['main_topic']).first()
|
||||||
shout_input['published_at'] = current_time if publish else None
|
|
||||||
Shout.update(shout, shout_input)
|
|
||||||
session.add(shout)
|
|
||||||
|
|
||||||
# main topic
|
if main_topic:
|
||||||
if 'main_topic' in shout_input:
|
new_main_topic = (
|
||||||
old_main_topic = (
|
|
||||||
session.query(ShoutTopic)
|
session.query(ShoutTopic)
|
||||||
.filter(
|
.filter(
|
||||||
and_(
|
and_(
|
||||||
ShoutTopic.shout == shout.id,
|
ShoutTopic.shout == shout.id,
|
||||||
ShoutTopic.main == True,
|
ShoutTopic.topic == main_topic.id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.first()
|
.first()
|
||||||
)
|
)
|
||||||
|
|
||||||
main_topic = session.query(Topic).filter(Topic.slug == shout_input['main_topic']).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)
|
||||||
|
|
||||||
if main_topic:
|
ShoutTopic.update(new_main_topic, {'main': True})
|
||||||
new_main_topic = (
|
session.add(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:
|
shout_dict = shout.dict()
|
||||||
ShoutTopic.update(old_main_topic, {'main': False})
|
session.commit()
|
||||||
session.add(old_main_topic)
|
|
||||||
|
|
||||||
ShoutTopic.update(new_main_topic, {'main': True})
|
if not publish:
|
||||||
session.add(new_main_topic)
|
await notify_shout(shout_dict, 'update')
|
||||||
|
else:
|
||||||
shout_dict = shout.dict()
|
await notify_shout(shout_dict, 'published')
|
||||||
session.commit()
|
# search service indexing
|
||||||
|
search_service.index(shout)
|
||||||
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}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user