feat: modules creation
This commit is contained in:
@ -1,17 +1,16 @@
|
||||
from sqlalchemy import and_
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import and_, or_
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from models import Module, Attribute, AttributeLabel, module_attribute
|
||||
from models import Module, Attribute, AttributeLabel, module_attribute, ModuleTab
|
||||
from models.module import DeviceType
|
||||
from repositories.mixins import *
|
||||
from schemas.module import UpdateModuleCommonInfoSchema
|
||||
from schemas.module import UpdateModuleCommonInfoSchema, CreateModuleSchema
|
||||
|
||||
|
||||
class ModuleRepository(
|
||||
BaseRepository,
|
||||
RepGetAllMixin[Module],
|
||||
RepGetByIdMixin[Module],
|
||||
RepUpdateMixin[Module, UpdateModuleCommonInfoSchema],
|
||||
RepDeleteMixin[Module]
|
||||
RepCrudMixin[Module, CreateModuleSchema, UpdateModuleCommonInfoSchema]
|
||||
):
|
||||
entity_class = Module
|
||||
|
||||
@ -43,7 +42,10 @@ class ModuleRepository(
|
||||
),
|
||||
isouter=True,
|
||||
)
|
||||
.where(Module.is_deleted.is_(False), Attribute.is_deleted.is_(False))
|
||||
.where(
|
||||
Module.is_deleted.is_(False),
|
||||
or_(Attribute.id.is_(None), Attribute.is_deleted.is_(False)),
|
||||
)
|
||||
.order_by(Attribute.id)
|
||||
)
|
||||
|
||||
@ -60,6 +62,36 @@ class ModuleRepository(
|
||||
stmt = stmt.where(Module.id == pk)
|
||||
return (await self.session.execute(stmt)).unique().all()
|
||||
|
||||
async def _prepare_create(self, data: CreateSchemaType) -> dict:
|
||||
dump = data.model_dump()
|
||||
dump["key"] = str(uuid.uuid4())
|
||||
return dump
|
||||
|
||||
async def _after_create(self, module: Module, _) -> None:
|
||||
tab = ModuleTab(
|
||||
key=module.key,
|
||||
label=module.label,
|
||||
icon_name=None,
|
||||
module_id=module.id,
|
||||
device=DeviceType.BOTH,
|
||||
)
|
||||
self.session.add(tab)
|
||||
|
||||
async def get_module_tabs_by_module_id(self, module_id: int) -> list[ModuleTab]:
|
||||
stmt = select(ModuleTab).where(ModuleTab.module_id == module_id)
|
||||
result = await self.session.scalars(stmt)
|
||||
return list(result.all())
|
||||
|
||||
async def update(
|
||||
self, module: Module, data: UpdateModuleCommonInfoSchema
|
||||
) -> Module:
|
||||
tabs = await self.get_module_tabs_by_module_id(module.id)
|
||||
for tab in tabs:
|
||||
tab.label = data.label
|
||||
self.session.add(tab)
|
||||
|
||||
return await self._apply_update_data_to_model(module, data, True)
|
||||
|
||||
async def add_attribute_to_module(self, module: Module, attribute: Attribute):
|
||||
module.attributes.append(attribute)
|
||||
await self.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user