From 1a371b191ada7fccb58f5cbe6214ced556511e3e Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 14 Nov 2024 14:00:33 +0300 Subject: [PATCH] .. --- .cursorignore | 1 + auth/authenticate.py | 4 ++-- resolvers/reader.py | 5 +++-- services/auth.py | 22 ++++++++++++---------- 4 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 .cursorignore diff --git a/.cursorignore b/.cursorignore new file mode 100644 index 00000000..6f9f00ff --- /dev/null +++ b/.cursorignore @@ -0,0 +1 @@ +# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv) diff --git a/auth/authenticate.py b/auth/authenticate.py index 3ba67832..b1b250ed 100644 --- a/auth/authenticate.py +++ b/auth/authenticate.py @@ -84,8 +84,8 @@ def login_accepted(func): # Если есть авторизация, добавляем данные автора в контекст if auth and auth.logged_in: - # Существующие данные auth остаются - pass + info.context["author"] = auth.author + info.context["user_id"] = auth.author.get("id") else: # Очищаем данные автора из контекста если авторизация отсутствует info.context["author"] = None diff --git a/resolvers/reader.py b/resolvers/reader.py index c2632e9d..984a3da2 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -1,6 +1,7 @@ import json import time +from graphql import GraphQLResolveInfo from sqlalchemy import nulls_last, text from sqlalchemy.orm import aliased from sqlalchemy.sql.expression import and_, asc, case, desc, func, select @@ -353,7 +354,7 @@ def apply_filters(q, filters): @query.field("get_shout") -async def get_shout(_, info, slug="", shout_id=0): +async def get_shout(_, info: GraphQLResolveInfo, slug="", shout_id=0): """ Получение публикации по slug или id. @@ -404,7 +405,7 @@ def apply_sorting(q, options): @query.field("load_shouts_by") @login_accepted -async def load_shouts_by(_, info, options): +async def load_shouts_by(_, info: GraphQLResolveInfo, options): """ Загрузка публикаций с фильтрацией, сортировкой и пагинацией. diff --git a/services/auth.py b/services/auth.py index f7e307c8..c64554d7 100644 --- a/services/auth.py +++ b/services/auth.py @@ -106,27 +106,29 @@ def login_accepted(f): async def decorated_function(*args, **kwargs): info = args[1] req = info.context.get("request") - - # Пробуем получить данные авторизации + + logger.debug("login_accepted: Проверка авторизации пользователя.") user_id, user_roles = await check_auth(req) - + logger.debug(f"login_accepted: user_id={user_id}, user_roles={user_roles}") + if user_id and user_roles: - # Если пользователь авторизован, добавляем его данные в контекст - logger.info(f" got {user_id} roles: {user_roles}") + logger.info(f"login_accepted: Пользователь авторизован: {user_id} с ролями {user_roles}") info.context["user_id"] = user_id.strip() info.context["roles"] = user_roles # Пробуем получить профиль автора author = await get_cached_author_by_user_id(user_id, get_with_stat) - if not author: - logger.warning(f"author profile not found for user {user_id}") - info.context["author"] = author + if author: + logger.debug(f"login_accepted: Найден профиль автора: {author}") + # Предполагается, что `author` является объектом с атрибутом `id` + info.context["author"] = author.dict() + else: + logger.error(f"login_accepted: Профиль автора не найден для пользователя {user_id}. Используем базовые данные.")# Используем базовую информацию об автор else: - # Для неавторизованных пользователей очищаем контекст + logger.debug("login_accepted: Пользователь не авторизован. Очищаем контекст.") info.context["user_id"] = None info.context["roles"] = None info.context["author"] = None return await f(*args, **kwargs) - return decorated_function