This commit is contained in:
@@ -19,6 +19,7 @@ from orm.author import Author, AuthorFollower
|
|||||||
from orm.community import Community, CommunityAuthor, CommunityFollower
|
from orm.community import Community, CommunityAuthor, CommunityFollower
|
||||||
from orm.reaction import Reaction
|
from orm.reaction import Reaction
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
|
from orm.topic import Topic
|
||||||
from resolvers.stat import get_with_stat
|
from resolvers.stat import get_with_stat
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.search import search_service
|
from services.search import search_service
|
||||||
@@ -148,6 +149,8 @@ async def get_authors_with_stats(
|
|||||||
filter_parts.append(f"id={by['id']}")
|
filter_parts.append(f"id={by['id']}")
|
||||||
if by.get("stat"):
|
if by.get("stat"):
|
||||||
filter_parts.append(f"stat={by['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"
|
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}"
|
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))
|
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
|
# Применяем фильтрацию по параметрам из by
|
||||||
if by:
|
if by:
|
||||||
for key, value in by.items():
|
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):
|
if hasattr(Author, key):
|
||||||
column = getattr(Author, key)
|
column = getattr(Author, key)
|
||||||
base_query = base_query.where(column == value)
|
base_query = base_query.where(column == value)
|
||||||
@@ -995,13 +1016,15 @@ def create_author(**kwargs) -> Author:
|
|||||||
"""
|
"""
|
||||||
author = Author()
|
author = Author()
|
||||||
# Use setattr to avoid MyPy complaints about Column assignment
|
# Use setattr to avoid MyPy complaints about Column assignment
|
||||||
author.update({
|
author.update(
|
||||||
|
{
|
||||||
"id": kwargs.get("user_id"), # Связь с user_id из системы авторизации
|
"id": kwargs.get("user_id"), # Связь с user_id из системы авторизации
|
||||||
"slug": kwargs.get("slug"), # Идентификатор из системы авторизации
|
"slug": kwargs.get("slug"), # Идентификатор из системы авторизации
|
||||||
"created_at": int(time.time()),
|
"created_at": int(time.time()),
|
||||||
"updated_at": int(time.time()),
|
"updated_at": int(time.time()),
|
||||||
"name": kwargs.get("name") or kwargs.get("slug") # если не указано
|
"name": kwargs.get("name") or kwargs.get("slug"), # если не указано
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
session.add(author)
|
session.add(author)
|
||||||
|
|||||||
Reference in New Issue
Block a user