This commit is contained in:
parent
b0e981ece4
commit
d6151c00c8
|
@ -87,32 +87,41 @@ async def create_shout(_, info, inp):
|
|||
return {'error': 'cant create shout'}
|
||||
|
||||
|
||||
@mutation.field('update_shout')
|
||||
@login_required
|
||||
async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||
user_id = info.context['user_id']
|
||||
if not shout_input:
|
||||
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 = (
|
||||
session.query(Shout)
|
||||
.options(
|
||||
joinedload(Shout.authors),
|
||||
joinedload(Shout.topics),
|
||||
def patch_main_topic(session, main_topic, shout):
|
||||
old_main_topic = (
|
||||
session.query(ShoutTopic)
|
||||
.filter(
|
||||
and_(
|
||||
ShoutTopic.shout == shout.id,
|
||||
ShoutTopic.main == True,
|
||||
)
|
||||
)
|
||||
.filter(Shout.id == shout_id)
|
||||
.first()
|
||||
)
|
||||
if not shout:
|
||||
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']
|
||||
|
||||
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)
|
||||
|
@ -146,47 +155,49 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
|||
)
|
||||
).delete(synchronize_session=False)
|
||||
|
||||
|
||||
@mutation.field('update_shout')
|
||||
@login_required
|
||||
async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||
user_id = info.context['user_id']
|
||||
if not shout_input:
|
||||
shout_input = {}
|
||||
with local_session() as session:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
current_time = int(time.time())
|
||||
if author:
|
||||
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 shout.created_by is not author.id and author.id not in shout.authors:
|
||||
return {'error': 'access denied'}
|
||||
|
||||
# topics patch
|
||||
topics_input = shout_input.get('topics')
|
||||
if topics_input:
|
||||
patch_topics(session, shout, topics_input)
|
||||
del shout_input['topics']
|
||||
|
||||
# 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)
|
||||
|
||||
# main topic
|
||||
if 'main_topic' in shout_input:
|
||||
old_main_topic = (
|
||||
session.query(ShoutTopic)
|
||||
.filter(
|
||||
and_(
|
||||
ShoutTopic.shout == shout.id,
|
||||
ShoutTopic.main == True,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
main_topic = session.query(Topic).filter(Topic.slug == shout_input['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)
|
||||
session.commit()
|
||||
|
||||
shout_dict = shout.dict()
|
||||
session.commit()
|
||||
|
||||
if not publish:
|
||||
await notify_shout(shout_dict, 'update')
|
||||
|
@ -196,6 +207,7 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
|||
search_service.index(shout)
|
||||
|
||||
return {'shout': shout_dict}
|
||||
return {'error': 'cannot update'}
|
||||
|
||||
|
||||
@mutation.field('delete_shout')
|
||||
|
|
Loading…
Reference in New Issue
Block a user