get_author-follows-debug
All checks were successful
Deploy on push / deploy (push) Successful in 22s

This commit is contained in:
Untone 2024-03-28 14:09:11 +03:00
parent e2faec5893
commit 95c54ff0c4

View File

@ -163,45 +163,51 @@ def load_authors_by(_, _info, by, limit, offset):
@query.field('get_author_follows') @query.field('get_author_follows')
async def get_author_follows(_, _info, slug='', user=None, author_id=0): async def get_author_follows(_, _info, slug='', user=None, author_id=0):
author_query = select(Author) try:
if user: author_query = select(Author)
author_query = author_query.filter(Author.user == user) if user:
elif slug: author_query = author_query.filter(Author.user == user)
author_query = author_query.filter(Author.slug == slug) elif slug:
elif author_id: author_query = author_query.filter(Author.slug == slug)
author_query = author_query.filter(Author.id == author_id) elif author_id:
else: author_query = author_query.filter(Author.id == author_id)
raise ValueError('One of slug, user, or author_id must be provided') else:
[author] = local_session().execute(author_query) raise ValueError('One of slug, user, or author_id must be provided')
if isinstance(author, Author): [author] = local_session().execute(author_query)
author_id = author.id.scalar() logger.debug(author.dict())
rkey = f'author:{author_id}:follows-authors' if isinstance(author, Author):
logger.debug(f'getting {author_id} follows authors') author_id = author.id.scalar()
cached = await redis.execute('GET', rkey) rkey = f'author:{author_id}:follows-authors'
if not cached: logger.debug(f'getting {author_id} follows authors')
authors = author_follows_authors(author_id) cached = await redis.execute('GET', rkey)
prepared = [author.dict() for author in authors] if not cached:
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) authors = author_follows_authors(author_id)
elif isinstance(cached, str): prepared = [author.dict() for author in authors]
authors = json.loads(cached) 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' rkey = f'author:{author_id}:follows-topics'
cached = await redis.execute('GET', rkey) cached = await redis.execute('GET', rkey)
if cached and isinstance(cached, str): if cached and isinstance(cached, str):
topics = json.loads(cached) topics = json.loads(cached)
if not cached: if not cached:
topics = author_follows_topics(author_id) topics = author_follows_topics(author_id)
prepared = [topic.dict() for topic in topics] prepared = [topic.dict() for topic in topics]
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder))
return { return {
'topics': topics, 'topics': topics,
'authors': authors, 'authors': authors,
'communities': [ 'communities': [
{'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''} {'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''}
], ],
} }
else: else:
raise ValueError('Author not found') raise ValueError('Author not found')
except Exception:
import traceback
traceback.print_exc()
@query.field('get_author_follows_topics') @query.field('get_author_follows_topics')