feat: modules and module-editor pages

This commit is contained in:
2025-10-25 12:11:48 +04:00
parent 62aeebf079
commit 281600c72d
16 changed files with 751 additions and 25 deletions

View File

@ -1,7 +1,8 @@
from .attribute import AttributeRepository as AttributeRepository
from .board import BoardRepository as BoardRepository
from .module import ModuleRepository as ModuleRepository
from .deal import DealRepository as DealRepository
from .deal_group import DealGroupRepository as DealGroupRepository
from .deal_tag import DealTagRepository as DealTagRepository
from .module import ModuleRepository as ModuleRepository
from .project import ProjectRepository as ProjectRepository
from .status import StatusRepository as StatusRepository

73
repositories/attribute.py Normal file
View File

@ -0,0 +1,73 @@
from sqlalchemy.orm import joinedload
from models import Attribute, AttributeLabel, AttributeType
from repositories.mixins import *
from schemas.attribute import CreateAttributeSchema, UpdateAttributeSchema
from utils.exceptions import ForbiddenException
class AttributeRepository(
RepCrudMixin[Attribute, CreateAttributeSchema, UpdateAttributeSchema]
):
session: AsyncSession
entity_class = Attribute
def _process_get_all_stmt(self, stmt: Select) -> Select:
return (
stmt.options(joinedload(Attribute.type))
.where(Attribute.is_deleted.is_(False))
.order_by(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)
async def _before_delete(self, attribute: Attribute) -> None:
if attribute.is_built_in:
raise ForbiddenException("Нельзя менять встроенный атрибут")
async def _get_attribute_module_label(
self, module_id: int, attribute_id: int
) -> Optional[AttributeLabel]:
stmt = select(AttributeLabel).where(
AttributeLabel.attribute_id == attribute_id,
AttributeLabel.module_id == module_id,
)
result = await self.session.execute(stmt)
row = result.one_or_none()
return row[0] if row else None
async def create_or_update_attribute_label(
self, module_id: int, attribute_id: int, label: str
):
attribute_label = await self._get_attribute_module_label(
module_id, attribute_id
)
if attribute_label:
attribute_label.label = label
else:
attribute_label = AttributeLabel(
module_id=module_id,
attribute_id=attribute_id,
label=label,
)
self.session.add(attribute_label)
await self.session.commit()
async def get_attribute_types(self) -> list[AttributeType]:
stmt = select(AttributeType).where(AttributeType.is_deleted.is_(False))
result = await self.session.execute(stmt)
return list(result.scalars().all())

View File

@ -1,18 +1,69 @@
from models import Board, Module
from sqlalchemy import and_
from sqlalchemy.orm import selectinload
from models import Module, Attribute, AttributeLabel, module_attribute
from repositories.mixins import *
from schemas.module import UpdateModuleCommonInfoSchema
class ModuleRepository(
BaseRepository,
RepGetAllMixin[Module],
RepGetByIdMixin[Module],
RepUpdateMixin[Module, UpdateModuleCommonInfoSchema],
RepDeleteMixin[Module]
):
entity_class = Module
def _process_get_all_stmt_with_args(self, stmt: Select, *args) -> Select:
project_id = args[0]
return stmt.where(Board.project_id == project_id).order_by(Board.lexorank)
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
return stmt.options(selectinload(Module.attributes).joinedload(Attribute.type))
async def get_by_ids(self, ids: list[int]) -> list[Module]:
stmt = select(Module).where(Module.id.in_(ids))
modules = await self.session.scalars(stmt)
return modules.all()
@staticmethod
def _get_stmt_modules_with_tuples() -> Select:
return (
select(Module, Attribute, AttributeLabel)
.join(
module_attribute,
Module.id == module_attribute.c.module_id,
isouter=True,
)
.join(
Attribute, module_attribute.c.attribute_id == Attribute.id, isouter=True
)
.join(
AttributeLabel,
and_(
Module.id == AttributeLabel.module_id,
Attribute.id == AttributeLabel.attribute_id,
),
isouter=True,
)
.where(Module.is_deleted.is_(False), Attribute.is_deleted.is_(False))
.order_by(Attribute.id)
)
async def get_with_attributes_as_tuples(
self,
) -> list[tuple[Module, Attribute, AttributeLabel]]:
stmt = self._get_stmt_modules_with_tuples()
return (await self.session.execute(stmt)).unique().all()
async def get_with_attributes_as_tuple_by_id(
self, pk: int
) -> list[tuple[Module, Attribute, AttributeLabel]]:
stmt = self._get_stmt_modules_with_tuples()
stmt = stmt.where(Module.id == pk)
return (await self.session.execute(stmt)).unique().all()
async def add_attribute_to_module(self, module: Module, attribute: Attribute):
module.attributes.append(attribute)
await self.session.commit()
async def delete_attribute_from_module(self, module: Module, attribute: Attribute):
module.attributes.remove(attribute)
await self.session.commit()