error-handle-create-shout
All checks were successful
Deploy on push / deploy (push) Successful in 24s

This commit is contained in:
Untone 2024-02-26 12:22:55 +03:00
parent a93fa7fb18
commit 4a57866c3d

View File

@ -40,69 +40,70 @@ async def get_shouts_drafts(_, info):
@mutation.field('create_shout') @mutation.field('create_shout')
@login_required @login_required
def create_shout(_, info, inp): def create_shout(_, info, inp):
user_id = info.context['user_id'] user_id = info.context.get('user_id')
with local_session() as session: if user_id:
author = session.query(Author).filter(Author.user == user_id).first() with local_session() as session:
if isinstance(author, Author): author = session.query(Author).filter(Author.user == user_id).first()
current_time = int(time.time()) if isinstance(author, Author):
slug = inp.get('slug') or f'draft-{current_time}' current_time = int(time.time())
shout_dict = { slug = inp.get('slug') or f'draft-{current_time}'
'title': inp.get('title', ''), shout_dict = {
'subtitle': inp.get('subtitle', ''), 'title': inp.get('title', ''),
'lead': inp.get('lead', ''), 'subtitle': inp.get('subtitle', ''),
'description': inp.get('description', ''), 'lead': inp.get('lead', ''),
'body': inp.get('body', ''), 'description': inp.get('description', ''),
'layout': inp.get('layout', 'article'), 'body': inp.get('body', ''),
'created_by': author.id, 'layout': inp.get('layout', 'article'),
'authors': [], 'created_by': author.id,
'slug': slug, 'authors': [],
'topics': inp.get('topics', []), 'slug': slug,
'published_at': None, 'topics': inp.get('topics', []),
'created_at': current_time, # Set created_at as Unix timestamp 'published_at': None,
} 'created_at': current_time, # Set created_at as Unix timestamp
same_slug_shout = ( }
session.query(Shout)
.filter(Shout.slug == shout_dict.get('slug'))
.first()
)
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_dict.get('slug')) .filter(Shout.slug == shout_dict.get('slug'))
.first() .first()
) )
c += 1 c = 1
shout_dict['slug'] += f'-{c}' while same_slug_shout is not None:
new_shout = Shout(**shout_dict) same_slug_shout = (
session.add(new_shout) session.query(Shout)
session.commit() .filter(Shout.slug == shout_dict.get('slug'))
.first()
# NOTE: requesting new shout back )
shout = session.query(Shout).where(Shout.slug == slug).first() c += 1
if shout: shout_dict['slug'] += f'-{c}'
sa = ShoutAuthor(shout=shout.id, author=author.id) new_shout = Shout(**shout_dict)
session.add(sa) session.add(new_shout)
topics = (
session.query(Topic)
.filter(Topic.slug.in_(inp.get('topics', [])))
.all()
)
for topic in topics:
t = ShoutTopic(topic=topic.id, shout=shout.id)
session.add(t)
session.commit() session.commit()
reactions_follow(author.id, shout.id, True) # NOTE: requesting new shout back
shout = session.query(Shout).where(Shout.slug == slug).first()
if shout:
sa = ShoutAuthor(shout=shout.id, author=author.id)
session.add(sa)
# notifier topics = (
# await notify_shout(shout_dict, 'create') session.query(Topic)
.filter(Topic.slug.in_(inp.get('topics', [])))
.all()
)
for topic in topics:
t = ShoutTopic(topic=topic.id, shout=shout.id)
session.add(t)
return {'shout': shout} session.commit()
return {'error': 'cant create shout'} reactions_follow(author.id, shout.id, True)
# notifier
# await notify_shout(shout_dict, 'create')
return {'shout': shout}
return {'error': 'cant create shout' if user_id else 'unauthorized'}
def patch_main_topic(session, main_topic, shout): def patch_main_topic(session, main_topic, shout):