feat: setting default attributes after deal creating
This commit is contained in:
@ -64,7 +64,7 @@ class Module(BaseModel):
|
|||||||
)
|
)
|
||||||
|
|
||||||
attributes: Mapped[list["Attribute"]] = relationship(
|
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(
|
attribute_values: Mapped[list["AttributeValue"]] = relationship(
|
||||||
|
|||||||
@ -9,6 +9,8 @@ from models import (
|
|||||||
AttributeType,
|
AttributeType,
|
||||||
AttributeValue,
|
AttributeValue,
|
||||||
module_attribute,
|
module_attribute,
|
||||||
|
Module,
|
||||||
|
Project,
|
||||||
)
|
)
|
||||||
from repositories.mixins import *
|
from repositories.mixins import *
|
||||||
from schemas.attribute import (
|
from schemas.attribute import (
|
||||||
@ -51,6 +53,36 @@ class AttributeRepository(
|
|||||||
if attribute.is_built_in:
|
if attribute.is_built_in:
|
||||||
raise ForbiddenException("Нельзя менять встроенный атрибут")
|
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(
|
async def _get_attribute_module_label(
|
||||||
self, module_id: int, attribute_id: int
|
self, module_id: int, attribute_id: int
|
||||||
) -> Optional[AttributeLabel]:
|
) -> Optional[AttributeLabel]:
|
||||||
|
|||||||
@ -8,6 +8,7 @@ from modules.fulfillment_base.models import (
|
|||||||
DealProductService,
|
DealProductService,
|
||||||
DealProduct,
|
DealProduct,
|
||||||
)
|
)
|
||||||
|
from repositories import AttributeRepository
|
||||||
from repositories.mixins import *
|
from repositories.mixins import *
|
||||||
from schemas.base import SortDir
|
from schemas.base import SortDir
|
||||||
from schemas.deal import UpdateDealSchema, CreateDealSchema
|
from schemas.deal import UpdateDealSchema, CreateDealSchema
|
||||||
@ -146,6 +147,15 @@ class DealRepository(
|
|||||||
result = await self.session.execute(stmt)
|
result = await self.session.execute(stmt)
|
||||||
return list(result.scalars().all())
|
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:
|
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
|
||||||
return stmt.options(joinedload(Deal.status), joinedload(Deal.board))
|
return stmt.options(joinedload(Deal.status), joinedload(Deal.board))
|
||||||
|
|
||||||
|
|||||||
@ -30,6 +30,7 @@ class DealSchema(BaseSchema):
|
|||||||
|
|
||||||
class CreateDealSchema(BaseSchema):
|
class CreateDealSchema(BaseSchema):
|
||||||
name: str
|
name: str
|
||||||
|
project_id: int
|
||||||
board_id: int
|
board_id: int
|
||||||
lexorank: str
|
lexorank: str
|
||||||
status_id: int
|
status_id: int
|
||||||
|
|||||||
@ -51,8 +51,10 @@ class ModuleService(
|
|||||||
|
|
||||||
module_schema = module_attrs_dict.get(module.id)
|
module_schema = module_attrs_dict.get(module.id)
|
||||||
if not module_schema:
|
if not module_schema:
|
||||||
|
module_data = module.__dict__
|
||||||
|
del module_data["attributes"]
|
||||||
module_schema = ModuleWithAttributesSchema(
|
module_schema = ModuleWithAttributesSchema(
|
||||||
**module.__dict__,
|
**module_data,
|
||||||
attributes=[new_attr] if new_attr else [],
|
attributes=[new_attr] if new_attr else [],
|
||||||
)
|
)
|
||||||
module_attrs_dict[module.id] = module_schema
|
module_attrs_dict[module.id] = module_schema
|
||||||
|
|||||||
Reference in New Issue
Block a user