From caf45f3d4242bb3c66b46501f4f086499d1813eb Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 29 Feb 2024 10:02:29 +0300 Subject: [PATCH] .dict-fxt --- resolvers/author.py | 7 ++++++- services/db.py | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/resolvers/author.py b/resolvers/author.py index cf97c5f4..f23aba1b 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -50,7 +50,12 @@ async def get_author(_, _info, slug='', author_id=None): cache = await redis.execute('GET', f'id:{author_id}:author') logger.debug(f'result from cache: {cache}') q = select(Author).where(Author.id == author_id) - author_dict = json.loads(cache) if cache else get_with_stat(q)[0].dict() + author_dict = None + if cache: + author_dict = json.loads(cache) + else: + [author] = get_with_stat(q) + author_dict = author.dict() logger.debug(f'author to be stored: {author_dict}') if author: await update_author_cache(author_dict) diff --git a/services/db.py b/services/db.py index 45cc005a..90ecca9d 100644 --- a/services/db.py +++ b/services/db.py @@ -57,6 +57,9 @@ class Base(declarative_base()): data[c] = json.loads(str(value)) else: data[c] = value + # Add synthetic field .stat + if hasattr(self, 'stat'): + data['stat'] = self.stat return data except Exception as e: logger.error(f'Error occurred while converting object to dictionary: {e}')