feat: setting default attributes after deal creating
This commit is contained in:
@ -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]:
|
||||
|
||||
@ -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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user