From 2683982180f29a478ade48346296fb4d5f016098 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 1 Jul 2025 00:01:20 +0300 Subject: [PATCH] invites-table-creating --- CHANGELOG.md | 1 + resolvers/admin.py | 167 +++++++++++++++++++++++++++++++++------------ services/schema.py | 4 +- 3 files changed, 127 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c330278c..ca62016f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Исправлена ошибка в функции `admin_get_invites` - заменен несуществующий атрибут `Shout.created_by_author` на правильное получение автора через поле `created_by` - Исправлена функция `admin_delete_invites_batch` - завершена реализация для корректной обработки пакетного удаления приглашений - Исправлена ошибка в функции `get_shouts_with_links` в файле `resolvers/reader.py` - добавлено значение по умолчанию для поля `slug` у авторов публикаций в полях `authors` и `created_by`, чтобы избежать ошибки "Cannot return null for non-nullable field Author.slug" +- Исправлена ошибка в функции `admin_get_shouts` в файле `resolvers/admin.py` - добавлена полная загрузка информации об авторах для полей `created_by`, `updated_by` и `deleted_by` с корректной обработкой поля `slug` и значениями по умолчанию, чтобы избежать ошибки "Cannot return null for non-nullable field Author.slug" ### Улучшения админ-панели для приглашений diff --git a/resolvers/admin.py b/resolvers/admin.py index ac5b6560..9794e1d6 100644 --- a/resolvers/admin.py +++ b/resolvers/admin.py @@ -9,6 +9,7 @@ from sqlalchemy.sql import func, select from auth.decorators import admin_auth_required from auth.orm import Author, AuthorRole, Role +from orm.community import Community from orm.invite import Invite, InviteStatus from orm.shout import Shout from services.db import local_session @@ -499,50 +500,130 @@ async def admin_get_shouts( "deleted_at": getattr(shout, "deleted_at", None) if not isinstance(shout, dict) else shout.get("deleted_at"), - "created_by": { - "id": getattr(shout, "created_by", None) - if not isinstance(shout, dict) - else shout.get("created_by"), - "email": "unknown", # Заполним при необходимости - "name": "unknown", - }, - "updated_by": None, # Заполним при необходимости - "deleted_by": None, # Заполним при необходимости - "community": { - "id": getattr(shout, "community", None) - if not isinstance(shout, dict) - else shout.get("community"), - "name": "unknown", # Заполним при необходимости - }, - "authors": [ - { - "id": getattr(author, "id", None), - "email": getattr(author, "email", None), - "name": getattr(author, "name", None), - "slug": getattr(author, "slug", None) or f"user-{getattr(author, 'id', 'unknown')}", - } - for author in ( - getattr(shout, "authors", []) - if not isinstance(shout, dict) - else shout.get("authors", []) - ) - ], - "topics": [ - { - "id": getattr(topic, "id", None), - "title": getattr(topic, "title", None), - "slug": getattr(topic, "slug", None), - } - for topic in ( - getattr(shout, "topics", []) if not isinstance(shout, dict) else shout.get("topics", []) - ) - ], - "version_of": getattr(shout, "version_of", None) - if not isinstance(shout, dict) - else shout.get("version_of"), - "draft": getattr(shout, "draft", None) if not isinstance(shout, dict) else shout.get("draft"), - "stat": None, # Заполним при необходимости } + + # Обрабатываем поле created_by - получаем полную информацию об авторе + created_by_id = ( + getattr(shout, "created_by", None) if not isinstance(shout, dict) else shout.get("created_by") + ) + if created_by_id: + created_author = session.query(Author).filter(Author.id == created_by_id).first() + if created_author: + shout_dict["created_by"] = { + "id": created_author.id, + "email": created_author.email, + "name": created_author.name, + "slug": created_author.slug or f"user-{created_author.id}", + } + else: + shout_dict["created_by"] = { + "id": created_by_id, + "email": "unknown", + "name": "unknown", + "slug": f"user-{created_by_id}", + } + else: + shout_dict["created_by"] = None + + # Обрабатываем поле updated_by - получаем полную информацию об авторе + updated_by_id = ( + getattr(shout, "updated_by", None) if not isinstance(shout, dict) else shout.get("updated_by") + ) + if updated_by_id: + updated_author = session.query(Author).filter(Author.id == updated_by_id).first() + if updated_author: + shout_dict["updated_by"] = { + "id": updated_author.id, + "email": updated_author.email, + "name": updated_author.name, + "slug": updated_author.slug or f"user-{updated_author.id}", + } + else: + shout_dict["updated_by"] = { + "id": updated_by_id, + "email": "unknown", + "name": "unknown", + "slug": f"user-{updated_by_id}", + } + else: + shout_dict["updated_by"] = None + + # Обрабатываем поле deleted_by - получаем полную информацию об авторе + deleted_by_id = ( + getattr(shout, "deleted_by", None) if not isinstance(shout, dict) else shout.get("deleted_by") + ) + if deleted_by_id: + deleted_author = session.query(Author).filter(Author.id == deleted_by_id).first() + if deleted_author: + shout_dict["deleted_by"] = { + "id": deleted_author.id, + "email": deleted_author.email, + "name": deleted_author.name, + "slug": deleted_author.slug or f"user-{deleted_author.id}", + } + else: + shout_dict["deleted_by"] = { + "id": deleted_by_id, + "email": "unknown", + "name": "unknown", + "slug": f"user-{deleted_by_id}", + } + else: + shout_dict["deleted_by"] = None + + # Обрабатываем поле community - получаем полную информацию о сообществе + community_id = ( + getattr(shout, "community", None) if not isinstance(shout, dict) else shout.get("community") + ) + if community_id: + community = session.query(Community).filter(Community.id == community_id).first() + if community: + shout_dict["community"] = { + "id": community.id, + "name": community.name, + "slug": community.slug, + } + else: + shout_dict["community"] = { + "id": community_id, + "name": "unknown", + "slug": f"community-{community_id}", + } + else: + shout_dict["community"] = None + + # Обрабатываем поля authors и topics как раньше + shout_dict["authors"] = [ + { + "id": getattr(author, "id", None), + "email": getattr(author, "email", None), + "name": getattr(author, "name", None), + "slug": getattr(author, "slug", None) or f"user-{getattr(author, 'id', 'unknown')}", + } + for author in ( + getattr(shout, "authors", []) if not isinstance(shout, dict) else shout.get("authors", []) + ) + ] + + shout_dict["topics"] = [ + { + "id": getattr(topic, "id", None), + "title": getattr(topic, "title", None), + "slug": getattr(topic, "slug", None), + } + for topic in ( + getattr(shout, "topics", []) if not isinstance(shout, dict) else shout.get("topics", []) + ) + ] + + shout_dict["version_of"] = ( + getattr(shout, "version_of", None) if not isinstance(shout, dict) else shout.get("version_of") + ) + shout_dict["draft"] = ( + getattr(shout, "draft", None) if not isinstance(shout, dict) else shout.get("draft") + ) + shout_dict["stat"] = None # Заполним при необходимости + shouts_data.append(shout_dict) else: # Используем существующую функцию для получения публикаций со статистикой diff --git a/services/schema.py b/services/schema.py index eccc02f2..b4eecd59 100644 --- a/services/schema.py +++ b/services/schema.py @@ -16,7 +16,7 @@ resolvers: List[SchemaBindable] = [query, mutation, type_draft, type_community, def create_all_tables() -> None: """Create all database tables in the correct order.""" from auth.orm import Author, AuthorBookmark, AuthorFollower, AuthorRating - from orm import collection, community, draft, notification, reaction, shout, topic + from orm import collection, community, draft, invite, notification, reaction, shout, topic # Порядок важен - сначала таблицы без внешних ключей, затем зависимые таблицы models_in_order = [ @@ -46,7 +46,7 @@ def create_all_tables() -> None: notification.NotificationSeen, # Зависит от Notification collection.Collection, # Зависит от Author collection.ShoutCollection, # Зависит от Collection и Shout - # invite.Invite + invite.Invite, # Зависит от Author и Shout ] with local_session() as session: