From 35af07f06734e3b9415daa5073cc5c2ae35359d5 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 1 Sep 2025 10:53:38 +0300 Subject: [PATCH] topic-filtered-authors --- resolvers/author.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 1fe7e1e9..18615fe7 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -19,6 +19,7 @@ from orm.author import Author, AuthorFollower from orm.community import Community, CommunityAuthor, CommunityFollower from orm.reaction import Reaction from orm.shout import Shout, ShoutAuthor, ShoutTopic +from orm.topic import Topic from resolvers.stat import get_with_stat from services.auth import login_required from services.search import search_service @@ -148,6 +149,8 @@ async def get_authors_with_stats( filter_parts.append(f"id={by['id']}") if by.get("stat"): filter_parts.append(f"stat={by['stat']}") + if by.get("topic"): + filter_parts.append(f"topic={by['topic']}") filter_str = ":".join(filter_parts) if filter_parts else "all" cache_key = f"authors:stats:limit={limit}:offset={offset}:order={order_value}:filter={filter_str}" @@ -162,10 +165,28 @@ async def get_authors_with_stats( # Базовый запрос для получения авторов base_query = select(Author).where(Author.deleted_at.is_(None)) + # Специальная обработка фильтра по теме (topic) + if by and by.get("topic"): + topic_value = by["topic"] + logger.debug(f"🔍 Filtering authors by topic: {topic_value}") + + # JOIN с таблицами для фильтрации по теме + # Авторы, которые публиковали статьи с данной темой + base_query = ( + base_query.join(ShoutAuthor, Author.id == ShoutAuthor.author) + .join(Shout, ShoutAuthor.shout == Shout.id) + .join(ShoutTopic, Shout.id == ShoutTopic.shout) + .join(Topic, ShoutTopic.topic == Topic.id) + .where(Topic.slug == topic_value) + .where(Shout.deleted_at.is_(None)) + .where(Shout.published_at.is_not(None)) + .distinct() # Избегаем дубликатов авторов + ) + # Применяем фильтрацию по параметрам из by if by: for key, value in by.items(): - if key != "order" and value is not None: # order обрабатывается отдельно + if key not in ("order", "topic") and value is not None: # order и topic обрабатываются отдельно if hasattr(Author, key): column = getattr(Author, key) base_query = base_query.where(column == value) @@ -995,13 +1016,15 @@ def create_author(**kwargs) -> Author: """ author = Author() # Use setattr to avoid MyPy complaints about Column assignment - author.update({ - "id": kwargs.get("user_id"), # Связь с user_id из системы авторизации - "slug": kwargs.get("slug"), # Идентификатор из системы авторизации - "created_at": int(time.time()), - "updated_at": int(time.time()), - "name": kwargs.get("name") or kwargs.get("slug") # если не указано - }) + author.update( + { + "id": kwargs.get("user_id"), # Связь с user_id из системы авторизации + "slug": kwargs.get("slug"), # Идентификатор из системы авторизации + "created_at": int(time.time()), + "updated_at": int(time.time()), + "name": kwargs.get("name") or kwargs.get("slug"), # если не указано + } + ) with local_session() as session: session.add(author)