feat: Implement project management with new models, repositories, and API endpoints, and enhance character management with project association and DTOs.

This commit is contained in:
xds
2026-02-09 16:06:54 +03:00
parent 668aadcdc9
commit 458b6ebfc3
42 changed files with 728 additions and 60 deletions

View File

@@ -46,7 +46,7 @@ class AssetsRepo:
res = await self.collection.insert_one(asset.model_dump())
return str(res.inserted_id)
async def get_assets(self, asset_type: Optional[str] = None, limit: int = 10, offset: int = 0, with_data: bool = False) -> List[Asset]:
async def get_assets(self, asset_type: Optional[str] = None, limit: int = 10, offset: int = 0, with_data: bool = False, created_by: Optional[str] = None, project_id: Optional[str] = None) -> List[Asset]:
filter = {}
if asset_type:
filter["type"] = asset_type
@@ -71,6 +71,9 @@ class AssetsRepo:
# So list DOES NOT return thumbnails by default.
args["thumbnail"] = 0
if project_id:
filter["project_id"] = project_id
res = await self.collection.find(filter, args).sort("created_at", -1).skip(offset).limit(limit).to_list(None)
assets = []
for doc in res:
@@ -157,8 +160,15 @@ class AssetsRepo:
assets.append(Asset(**doc))
return assets
async def get_asset_count(self, character_id: Optional[str] = None) -> int:
return await self.collection.count_documents({"linked_char_id": character_id} if character_id else {})
async def get_asset_count(self, character_id: Optional[str] = None, created_by: Optional[str] = None, project_id: Optional[str] = None) -> int:
filter = {}
if character_id:
filter["linked_char_id"] = character_id
if created_by:
filter["created_by"] = created_by
if project_id:
filter["project_id"] = project_id
return await self.collection.count_documents(filter)
async def get_assets_by_ids(self, asset_ids: List[str]) -> List[Asset]:
object_ids = [ObjectId(asset_id) for asset_id in asset_ids]

View File

@@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional
from bson import ObjectId
from motor.motor_asyncio import AsyncIOMotorClient
@@ -12,7 +12,7 @@ class CharacterRepo:
async def add_character(self, character: Character) -> Character:
op = await self.collection.insert_one(character.model_dump())
character.id = op.inserted_id
character.id = str(op.inserted_id)
return character
async def get_character(self, character_id: str, with_image_data: bool = False) -> Character | None:
@@ -26,18 +26,25 @@ class CharacterRepo:
res["id"] = str(res.pop("_id"))
return Character(**res)
async def get_all_characters(self) -> List[Character]:
docs = await self.collection.find({}, {"character_image_data": 0}).to_list(None)
characters = []
for doc in docs:
# Конвертируем ObjectId в строку и кладем в поле id
async def get_all_characters(self, created_by: Optional[str] = None, project_id: Optional[str] = None) -> List[Character]:
filter = {}
if created_by:
filter["created_by"] = created_by
if project_id:
filter["project_id"] = project_id
args = {"character_image_data": 0} # don't return image data for list
res = await self.collection.find(filter, args).to_list(None)
chars = []
for doc in res:
doc["id"] = str(doc.pop("_id"))
chars.append(Character(**doc))
return chars
# Создаем объект
characters.append(Character(**doc))
async def update_char(self, char_id: str, character: Character) -> bool:
result = await self.collection.update_one({"_id": ObjectId(char_id)}, {"$set": character.model_dump()})
return result.modified_count > 0
return characters
async def update_char(self, char_id: str, character: Character) -> None:
await self.collection.update_one({"_id": ObjectId(char_id)}, {"$set": character.model_dump()})
async def delete_character(self, char_id: str) -> bool:
result = await self.collection.delete_one({"_id": ObjectId(char_id)})
return result.deleted_count > 0

View File

@@ -5,6 +5,7 @@ from repos.char_repo import CharacterRepo
from repos.generation_repo import GenerationRepo
from repos.user_repo import UsersRepo
from repos.albums_repo import AlbumsRepo
from repos.project_repo import ProjectRepo
from typing import Optional
@@ -16,3 +17,5 @@ class DAO:
self.assets = AssetsRepo(client, s3_adapter, db_name)
self.generations = GenerationRepo(client, db_name)
self.albums = AlbumsRepo(client, db_name)
self.projects = ProjectRepo(client, db_name)
self.users = UsersRepo(client, db_name)

View File

@@ -25,13 +25,19 @@ class GenerationRepo:
return Generation(**res)
async def get_generations(self, character_id: Optional[str] = None, status: Optional[GenerationStatus] = None,
limit: int = 10, offset: int = 10) -> List[Generation]:
limit: int = 10, offset: int = 10, created_by: Optional[str] = None, project_id: Optional[str] = None) -> List[Generation]:
filter = {"is_deleted": False}
if character_id is not None:
filter["linked_character_id"] = character_id
if status is not None:
filter["status"] = status
if created_by is not None:
filter["created_by"] = created_by
filter["project_id"] = None
if project_id is not None:
filter["project_id"] = project_id
res = await self.collection.find(filter).sort("created_at", -1).skip(
offset).limit(limit).to_list(None)
generations: List[Generation] = []
@@ -40,12 +46,17 @@ class GenerationRepo:
generations.append(Generation(**generation))
return generations
async def count_generations(self, character_id: Optional[str] = None, status: Optional[GenerationStatus] = None, album_id: Optional[str] = None) -> int:
async def count_generations(self, character_id: Optional[str] = None, status: Optional[GenerationStatus] = None,
album_id: Optional[str] = None, created_by: Optional[str] = None, project_id: Optional[str] = None) -> int:
args = {}
if character_id is not None:
args["linked_character_id"] = character_id
if status is not None:
args["status"] = status
if created_by is not None:
args["created_by"] = created_by
if project_id is not None:
args["project_id"] = project_id
return await self.collection.count_documents(args)
async def get_generations_by_ids(self, generation_ids: List[str]) -> List[Generation]:

62
repos/project_repo.py Normal file
View File

@@ -0,0 +1,62 @@
from typing import List, Optional
from bson import ObjectId
from motor.motor_asyncio import AsyncIOMotorClient
from models.Project import Project
class ProjectRepo:
def __init__(self, client: AsyncIOMotorClient, db_name="bot_db"):
self.collection = client[db_name]["projects"]
async def create_project(self, project: Project) -> str:
res = await self.collection.insert_one(project.model_dump())
return str(res.inserted_id)
async def get_project(self, project_id: str) -> Optional[Project]:
if not ObjectId.is_valid(project_id):
return None
res = await self.collection.find_one({"_id": ObjectId(project_id)})
if res:
res["id"] = str(res.pop("_id"))
return Project(**res)
return None
async def get_projects_by_user(self, user_id: str) -> List[Project]:
# Find projects where user is owner OR in members
filter = {
"$or": [
{"owner_id": user_id},
{"members": user_id}
],
"is_deleted": False
}
cursor = self.collection.find(filter).sort("created_at", -1)
projects = []
async for doc in cursor:
doc["id"] = str(doc.pop("_id"))
projects.append(Project(**doc))
return projects
async def add_member(self, project_id: str, user_id: str) -> bool:
res = await self.collection.update_one(
{"_id": ObjectId(project_id)},
{"$addToSet": {"members": user_id}}
)
return res.modified_count > 0
async def remove_member(self, project_id: str, user_id: str) -> bool:
res = await self.collection.update_one(
{"_id": ObjectId(project_id)},
{"$pull": {"members": user_id}}
)
return res.modified_count > 0
async def update_project(self, project_id: str, updates: dict) -> bool:
res = await self.collection.update_one(
{"_id": ObjectId(project_id)},
{"$set": updates}
)
return res.modified_count > 0
async def delete_project(self, project_id: str) -> bool:
res = await self.collection.update_one({"_id": ObjectId(project_id)}, {"$set": {"is_deleted": True}})
return res.modified_count > 0

View File

@@ -19,10 +19,14 @@ class UsersRepo:
self.collection = client[db_name]["users"]
async def get_user(self, user_id: int):
return await self.collection.find_one({"user_id": user_id})
user = await self.collection.find_one({"user_id": user_id})
user["id"] = str(user["_id"])
return user
async def get_user_by_username(self, username: str):
return await self.collection.find_one({"username": username})
user = await self.collection.find_one({"username": username})
user["id"] = str(user["_id"])
return user
async def create_user(self, username: str, password: str, full_name: Optional[str] = None):
"""Создает нового пользователя с username/паролем"""
@@ -38,15 +42,22 @@ class UsersRepo:
"created_at": datetime.now(),
"is_email_user": False, # Теперь это просто "обычный" юзер, не телеграм (хотя поле можно переименовать)
"is_web_user": True,
"is_admin": False
"is_admin": False,
"project_ids": [],
"current_project_id": None
}
result = await self.collection.insert_one(user_doc)
return await self.collection.find_one({"_id": result.inserted_id})
user = await self.collection.find_one({"_id": result.inserted_id})
user["id"] = str(user["_id"])
return user
async def get_pending_users(self):
"""Возвращает список пользователей со статусом PENDING"""
cursor = self.collection.find({"status": UserStatus.PENDING})
return await cursor.to_list(length=100)
users = await cursor.to_list(length=100)
for user in users:
user["id"] = str(user["_id"])
return users
async def approve_user(self, username: str):
await self.collection.update_one(