author-stats-fix
Some checks failed
Deploy on push / deploy (push) Failing after 6m48s

This commit is contained in:
2025-08-31 22:29:40 +03:00
parent 832f6529e7
commit aebca9c522

View File

@@ -64,6 +64,7 @@ class AuthorsBy(TypedDict, total=False):
order: str | None order: str | None
after: int | None after: int | None
stat: str | None stat: str | None
id: int | None # Добавляем поле id для фильтрации по ID
# Вспомогательная функция для получения всех авторов без статистики # Вспомогательная функция для получения всех авторов без статистики
@@ -753,21 +754,36 @@ async def get_author(
author_dict["stat"] = cached_author["stat"] author_dict["stat"] = cached_author["stat"]
if not author_dict or not author_dict.get("stat"): if not author_dict or not author_dict.get("stat"):
# update stat from db # update stat from db using new comprehensive stats
author_query = select(Author).where(Author.id == author_id) try:
result = get_with_stat(author_query) # Используем новый резолвер для получения полной статистики
if result: filter_by: AuthorsBy = {}
author_with_stat = result[0] if slug:
if isinstance(author_with_stat, Author): filter_by["slug"] = slug
# Кэшируем полные данные для админов elif author_id:
original_dict = author_with_stat.dict() filter_by["id"] = author_id # author_id уже int
_t = asyncio.create_task(cache_author(original_dict))
# Возвращаем отфильтрованную версию authors_with_stats = await get_authors_with_stats(limit=1, offset=0, by=filter_by)
author_dict = author_with_stat.dict(is_admin) if authors_with_stats and len(authors_with_stats) > 0:
# Добавляем статистику author_dict = authors_with_stats[0]
if hasattr(author_with_stat, "stat"): # Фильтруем данные согласно правам доступа
author_dict["stat"] = author_with_stat.stat if not is_admin:
# Убираем защищенные поля для не-админов
protected_fields = ["email", "phone"]
for field in protected_fields:
author_dict.pop(field, None)
# Кэшируем полные данные для админов
_t = asyncio.create_task(cache_author(author_dict))
except Exception as e:
logger.error(f"Error getting author stats: {e}")
# Fallback to basic author data without stats
with local_session() as session:
if slug:
author = session.query(Author).filter_by(slug=slug).first()
else:
author = session.query(Author).filter_by(id=author_id).first()
if author:
author_dict = author.dict(is_admin)
except ValueError: except ValueError:
pass pass
except Exception as exc: except Exception as exc: