feat: deal attributes with select and options
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
from .attr_select import AttrSelectRepository as AttrSelectRepository
|
||||
from .attribute import AttributeRepository as AttributeRepository
|
||||
from .board import BoardRepository as BoardRepository
|
||||
from .deal import DealRepository as DealRepository
|
||||
|
||||
19
repositories/attr_select.py
Normal file
19
repositories/attr_select.py
Normal file
@ -0,0 +1,19 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models import AttributeSelect, AttributeOption
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import RepGetAllMixin
|
||||
|
||||
|
||||
class AttrSelectRepository(BaseRepository, RepGetAllMixin[AttributeSelect]):
|
||||
session: AsyncSession
|
||||
entity_class = AttributeSelect
|
||||
|
||||
async def get_options(self, select_id: int) -> list[AttributeOption]:
|
||||
stmt = select(AttributeOption).where(
|
||||
AttributeOption.select_id == select_id,
|
||||
AttributeOption.is_deleted.is_(False),
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
@ -31,29 +31,24 @@ class AttributeRepository(
|
||||
return (
|
||||
stmt.options(joinedload(Attribute.type))
|
||||
.where(Attribute.is_deleted.is_(False))
|
||||
.order_by(Attribute.id)
|
||||
.order_by(Attribute.is_built_in.desc(), Attribute.id)
|
||||
)
|
||||
|
||||
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
|
||||
return stmt.options(joinedload(Attribute.type))
|
||||
|
||||
async def _get_attribute_type_by_id(self, type_id: int) -> AttributeType:
|
||||
stmt = select(AttributeType).where(AttributeType.id == type_id)
|
||||
result = (await self.session.execute(stmt)).one_or_none()
|
||||
if result is None:
|
||||
raise ObjectNotFoundException("Тип аттрибута не найден")
|
||||
return result[0]
|
||||
|
||||
async def update(self, attr: Attribute, data: UpdateAttributeSchema) -> Attribute:
|
||||
if data.type:
|
||||
data.type = await self._get_attribute_type_by_id(data.type.id)
|
||||
return await self._apply_update_data_to_model(attr, data, True)
|
||||
return await self._apply_update_data_to_model(
|
||||
attr, data, with_commit=True, set_if_value_is_not_none=False
|
||||
)
|
||||
|
||||
async def _before_delete(self, attribute: Attribute) -> None:
|
||||
if attribute.is_built_in:
|
||||
raise ForbiddenException("Нельзя менять встроенный атрибут")
|
||||
|
||||
async def _get_all_attributes_for_deal(self, project_id) -> list[tuple[Attribute, int]]:
|
||||
async def _get_all_attributes_for_deal(
|
||||
self, project_id
|
||||
) -> list[tuple[Attribute, int]]:
|
||||
stmt = (
|
||||
select(Attribute, Module.id)
|
||||
.join(Attribute.modules)
|
||||
@ -148,9 +143,8 @@ class AttributeRepository(
|
||||
AttributeLabel.module_id == module_id,
|
||||
),
|
||||
)
|
||||
.where(
|
||||
Attribute.is_deleted.is_(False),
|
||||
)
|
||||
.where(Attribute.is_deleted.is_(False))
|
||||
.options(joinedload(Attribute.select))
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.all())
|
||||
|
||||
@ -65,13 +65,14 @@ class RepUpdateMixin(Generic[EntityType, UpdateSchemaType], RepBaseMixin[EntityT
|
||||
data: UpdateSchemaType,
|
||||
with_commit: Optional[bool] = False,
|
||||
fields: Optional[list[str]] = None,
|
||||
set_if_value_is_not_none: Optional[bool] = True,
|
||||
) -> EntityType:
|
||||
if fields is None:
|
||||
fields = data.model_dump().keys()
|
||||
|
||||
for field in fields:
|
||||
value = getattr(data, field)
|
||||
if value is not None:
|
||||
if not set_if_value_is_not_none or value is not None:
|
||||
setattr(model, field, value)
|
||||
|
||||
if with_commit:
|
||||
|
||||
Reference in New Issue
Block a user