diff --git a/models/module.py b/models/module.py index 4221fbf..94ca8e4 100644 --- a/models/module.py +++ b/models/module.py @@ -64,7 +64,7 @@ class Module(BaseModel): ) attributes: Mapped[list["Attribute"]] = relationship( - secondary="module_attribute", back_populates="modules" + secondary="module_attribute", back_populates="modules", lazy="noload" ) attribute_values: Mapped[list["AttributeValue"]] = relationship( diff --git a/repositories/attribute.py b/repositories/attribute.py index 44d436b..c8f9b18 100644 --- a/repositories/attribute.py +++ b/repositories/attribute.py @@ -9,6 +9,8 @@ from models import ( AttributeType, AttributeValue, module_attribute, + Module, + Project, ) from repositories.mixins import * from schemas.attribute import ( @@ -51,6 +53,36 @@ class AttributeRepository( if attribute.is_built_in: raise ForbiddenException("Нельзя менять встроенный атрибут") + async def _get_all_attributes_for_deal(self, project_id) -> list[tuple[Attribute, int]]: + stmt = ( + select(Attribute, Module.id) + .join(Attribute.modules) + .join(Module.projects) + .where( + Module.is_deleted.is_(False), + Project.is_deleted.is_(False), + Project.id == project_id, + ) + .distinct(Attribute.id, Module.id) + ) + result = await self.session.execute(stmt) + return list(result.all()) + + async def create_attributes_for_new_deal( + self, deal_id: int, project_id: int + ) -> None: + attributes = await self._get_all_attributes_for_deal(project_id) + for attribute, module_id in attributes: + if attribute.default_value is None: + continue + value = AttributeValue( + attribute_id=attribute.id, + deal_id=deal_id, + module_id=module_id, + value=attribute.default_value, + ) + self.session.add(value) + async def _get_attribute_module_label( self, module_id: int, attribute_id: int ) -> Optional[AttributeLabel]: diff --git a/repositories/deal.py b/repositories/deal.py index c979396..53a17ac 100644 --- a/repositories/deal.py +++ b/repositories/deal.py @@ -8,6 +8,7 @@ from modules.fulfillment_base.models import ( DealProductService, DealProduct, ) +from repositories import AttributeRepository from repositories.mixins import * from schemas.base import SortDir from schemas.deal import UpdateDealSchema, CreateDealSchema @@ -146,6 +147,15 @@ class DealRepository( result = await self.session.execute(stmt) return list(result.scalars().all()) + async def _prepare_create(self, data: CreateDealSchema) -> dict: + dumped = data.model_dump() + del dumped["project_id"] + return dumped + + async def _after_create(self, obj: Deal, data: CreateDealSchema) -> None: + attr_repo = AttributeRepository(self.session) + await attr_repo.create_attributes_for_new_deal(obj.id, data.project_id) + def _process_get_by_id_stmt(self, stmt: Select) -> Select: return stmt.options(joinedload(Deal.status), joinedload(Deal.board)) diff --git a/schemas/deal.py b/schemas/deal.py index 6c95c20..66a78f9 100644 --- a/schemas/deal.py +++ b/schemas/deal.py @@ -30,6 +30,7 @@ class DealSchema(BaseSchema): class CreateDealSchema(BaseSchema): name: str + project_id: int board_id: int lexorank: str status_id: int diff --git a/services/module.py b/services/module.py index f40cbb8..3a93aee 100644 --- a/services/module.py +++ b/services/module.py @@ -51,8 +51,10 @@ class ModuleService( module_schema = module_attrs_dict.get(module.id) if not module_schema: + module_data = module.__dict__ + del module_data["attributes"] module_schema = ModuleWithAttributesSchema( - **module.__dict__, + **module_data, attributes=[new_attr] if new_attr else [], ) module_attrs_dict[module.id] = module_schema