From 78c7a41c466a94e88359eaab9a9a36cf0aec3797 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 5 Mar 2024 18:01:29 +0300 Subject: [PATCH] update-shout-fix-4 --- resolvers/reader.py | 63 +++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/resolvers/reader.py b/resolvers/reader.py index d41e1266..be839349 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -233,42 +233,43 @@ async def load_shouts_by(_, _info, options): @login_required @query.field('load_shouts_drafts') async def load_shouts_drafts(_, info): - user_id = info.context['user_id'] - - q = ( - select(Shout) - .options(joinedload(Shout.authors), joinedload(Shout.topics)) - .filter( - and_( - Shout.deleted_at.is_not(None), - Shout.published_at.is_(None) + user_id = info.context.get('user_id') + shouts = [] + if user_id: + q = ( + select(Shout) + .options(joinedload(Shout.authors), joinedload(Shout.topics)) + .filter( + and_( + Shout.deleted_at.is_not(None), + Shout.published_at.is_(None) + ) ) ) - ) - shouts = [] - with local_session() as session: - reader = session.query(Author).filter(Author.user == user_id).first() - if isinstance(reader, Author): - q = q.filter(Shout.created_by == reader.id) - q = q.group_by(Shout.id) - for [shout] in session.execute(q).unique(): - main_topic = ( - session.query(Topic.slug) - .join( - ShoutTopic, - and_( - ShoutTopic.topic == Topic.id, - ShoutTopic.shout == shout.id, - ShoutTopic.main.is_(True), - ), + shouts = [] + with local_session() as session: + reader = session.query(Author).filter(Author.user == user_id).first() + if isinstance(reader, Author): + q = q.filter(Shout.created_by == reader.id) + q = q.group_by(Shout.id) + for [shout] in session.execute(q).unique(): + main_topic = ( + session.query(Topic.slug) + .join( + ShoutTopic, + and_( + ShoutTopic.topic == Topic.id, + ShoutTopic.shout == shout.id, + ShoutTopic.main.is_(True), + ), + ) + .first() ) - .first() - ) - if main_topic: - shout.main_topic = main_topic[0] - shouts.append(shout) + if main_topic: + shout.main_topic = main_topic[0] + shouts.append(shout) return shouts