load_authors_by-fix
All checks were successful
Deploy on push / deploy (push) Successful in 1m9s

This commit is contained in:
Untone 2024-06-06 14:16:16 +03:00
parent 3f12bcfd39
commit 4743581395

View File

@ -107,6 +107,7 @@ async def get_author_id(_, _info, user: str):
async def load_authors_by(_, _info, by, limit, offset): async def load_authors_by(_, _info, by, limit, offset):
logger.debug(f"loading authors by {by}") logger.debug(f"loading authors by {by}")
authors_query = select(Author) authors_query = select(Author)
if by.get("slug"): if by.get("slug"):
authors_query = authors_query.filter(Author.slug.ilike(f"%{by['slug']}%")) authors_query = authors_query.filter(Author.slug.ilike(f"%{by['slug']}%"))
elif by.get("name"): elif by.get("name"):
@ -114,36 +115,35 @@ async def load_authors_by(_, _info, by, limit, offset):
elif by.get("topic"): elif by.get("topic"):
authors_query = ( authors_query = (
authors_query authors_query
.join(ShoutAuthor) .join(ShoutAuthor) # Первое соединение ShoutAuthor
.join(ShoutTopic, ShoutAuthor.shout == ShoutTopic.shout) .join(ShoutTopic, ShoutAuthor.shout == ShoutTopic.shout)
.join(Topic, ShoutTopic.topic == Topic.id) .join(Topic, ShoutTopic.topic == Topic.id)
.filter(Topic.slug == str(by["topic"])) .filter(Topic.slug == str(by["topic"]))
) )
if by.get("last_seen"): # in unix time if by.get("last_seen"): # в unix time
before = int(time.time()) - by["last_seen"] before = int(time.time()) - by["last_seen"]
authors_query = authors_query.filter(Author.last_seen > before) authors_query = authors_query.filter(Author.last_seen > before)
elif by.get("created_at"): # in unix time elif by.get("created_at"): # в unix time
before = int(time.time()) - by["created_at"] before = int(time.time()) - by["created_at"]
authors_query = authors_query.filter(Author.created_at > before) authors_query = authors_query.filter(Author.created_at > before)
authors = []
authors_query = authors_query.limit(limit).offset(offset) authors_query = authors_query.limit(limit).offset(offset)
with local_session() as session: with local_session() as session:
authors_nostat = session.execute(authors_query) authors_nostat = session.execute(authors_query).all()
if authors_nostat: authors = []
for [a] in authors_nostat: for a in authors_nostat:
author_dict = None
if isinstance(a, Author): if isinstance(a, Author):
author_dict = await get_cached_author(a.id, get_with_stat) author_dict = await get_cached_author(a.id, get_with_stat)
if not author_dict or not isinstance(author_dict.get("shouts"), int): if author_dict and isinstance(author_dict.get("shouts"), int):
break authors.append(author_dict)
# order # order
order = by.get("order") order = by.get("order")
if order in ["shouts", "followers"]: if order in ["shouts", "followers"]:
authors_query = authors_query.order_by(desc(text(f"{order}_stat"))) authors_query = authors_query.order_by(desc(text(f"{order}_stat")))
# group by # group by
authors = get_with_stat(authors_query) authors = get_with_stat(authors_query)
return authors or [] return authors or []