From 65fd4df5ef58819d1b5df3ecac5e492c36bda6f1 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 28 Mar 2024 13:33:41 +0300 Subject: [PATCH] get_author_follows-fix --- resolvers/author.py | 74 ++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 3deee521..f73e063e 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -71,7 +71,7 @@ async def get_author(_, _info, slug='', author_id=None): else: logger.warn('author was not cached!') author_query = select(Author).filter(Author.id == author_id) - author = get_with_stat(author_query) + [author] = get_with_stat(author_query) author_dict = author.dict() if author_dict: await set_author_cache(author_dict) @@ -161,40 +161,46 @@ def load_authors_by(_, _info, by, limit, offset): @query.field('get_author_follows') -async def get_author_follows(_, _info, slug='', user=None, author_id=None): - with (local_session() as session): - if user or slug: - author = session.query(Author).filter(or_(Author.user == user, Author.slug == slug)).first() - if author: - author_id = author.id - if author_id: - rkey = f'author:{author_id}:follows-authors' - logger.debug(f'getting {author_id} follows authors') - cached = await redis.execute('GET', rkey) - if not cached: - authors = author_follows_authors(author_id) - prepared = [author.dict() for author in authors] - await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) - else: - authors = json.loads(cached) +async def get_author_follows(_, _info, slug='', user=None, author_id=0): + author_query = select(Author) + if user: + author_query = author_query.filter(Author.user == user) + elif slug: + author_query = author_query.filter(Author.slug == slug) + elif author_id: + author_query = author_query.filter(Author.id == author_id) + else: + raise ValueError('One of slug, user, or author_id must be provided') + [author] = local_session().execute(author_query) + if isinstance(author, Author): + author_id = author.id + rkey = f'author:{author_id}:follows-authors' + logger.debug(f'getting {author_id} follows authors') + cached = await redis.execute('GET', rkey) + if not cached: + authors = author_follows_authors(author_id) + prepared = [author.dict() for author in authors] + await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) + elif isinstance(cached, str): + authors = json.loads(cached) - rkey = f'author:{author_id}:follows-topics' - cached = await redis.execute('GET', rkey) - if cached: - topics = json.loads(cached) - if not cached: - topics = author_follows_topics(author_id) - prepared = [topic.dict() for topic in topics] - await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) - return { - 'topics': topics, - 'authors': authors, - 'communities': [ - {'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''} - ], - } - else: - raise ValueError('Author not found') + rkey = f'author:{author_id}:follows-topics' + cached = await redis.execute('GET', rkey) + if cached: + topics = json.loads(cached) + if not cached: + topics = author_follows_topics(author_id) + prepared = [topic.dict() for topic in topics] + await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) + return { + 'topics': topics, + 'authors': authors, + 'communities': [ + {'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''} + ], + } + else: + raise ValueError('Author not found') @query.field('get_author_follows_topics')