From 4d165556bafd335e35d7ca39f8f91568dc726c2f Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Mon, 10 Mar 2025 14:39:49 +0700 Subject: [PATCH] add system and subsystem --- .vscode/settings.json | 2 +- src/overhaul/service.py | 3 +++ src/overhaul_activity/service.py | 8 ++++++-- src/overhaul_scope/service.py | 1 + src/scope_equipment/model.py | 9 ++++++++- src/scope_equipment/schema.py | 8 ++++++-- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d03d5c4..e2ce41d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -121,7 +121,7 @@ "errorLens.enabled": false, "[python]": { "editor.formatOnSave": false, - "editor.defaultFormatter": "charliermarsh.ruff", + "editor.defaultFormatter": "mikoz.black-py", "editor.formatOnType": false }, "python.languageServer": "Jedi", diff --git a/src/overhaul/service.py b/src/overhaul/service.py index 189f514..c8e79d0 100644 --- a/src/overhaul/service.py +++ b/src/overhaul/service.py @@ -107,6 +107,9 @@ def get_overhaul_system_components(): } + + + # async def get(*, db_session: DbSession, scope_id: str) -> Optional[Scope]: # """Returns a document based on the given document id.""" # query = Select(Scope).filter(Scope.id == scope_id) diff --git a/src/overhaul_activity/service.py b/src/overhaul_activity/service.py index 9f65128..8b0696f 100644 --- a/src/overhaul_activity/service.py +++ b/src/overhaul_activity/service.py @@ -13,6 +13,7 @@ from src.database.service import CommonParameters, search_filter_sort_paginate from src.overhaul_activity.utils import get_material_cost, get_service_cost from src.overhaul_scope.model import OverhaulScope from src.overhaul_scope.service import get as get_session +from src.scope_equipment.model import MasterEquipment from .model import OverhaulActivity from .schema import (OverhaulActivityCreate, OverhaulActivityRead, @@ -36,6 +37,7 @@ async def get( return result.scalar() + async def get_all( *, common: CommonParameters, @@ -46,7 +48,7 @@ async def get_all( query = ( Select(OverhaulActivity) .where(OverhaulActivity.overhaul_scope_id == overhaul_session_id) - .options(joinedload(OverhaulActivity.equipment)) + .options(joinedload(OverhaulActivity.equipment).options(joinedload(MasterEquipment.parent).options(joinedload(MasterEquipment.parent)))) .options(selectinload(OverhaulActivity.overhaul_scope)) ) @@ -62,6 +64,8 @@ async def get_all( results = await search_filter_sort_paginate(model=query, **common) + ##raise Exception(results['items'][0].equipment.parent.__dict__) + return results @@ -69,7 +73,7 @@ async def get_all_by_session_id(*, db_session: DbSession, overhaul_session_id): query = ( Select(OverhaulActivity) .where(OverhaulActivity.overhaul_scope_id == overhaul_session_id) - .options(joinedload(OverhaulActivity.equipment)) + .options(joinedload(OverhaulActivity.equipment).options(MasterEquipment.parent).options(MasterEquipment.parent)) .options(selectinload(OverhaulActivity.overhaul_scope)) ) diff --git a/src/overhaul_scope/service.py b/src/overhaul_scope/service.py index 41c8074..3556700 100644 --- a/src/overhaul_scope/service.py +++ b/src/overhaul_scope/service.py @@ -147,6 +147,7 @@ async def get_overview_overhaul(*, db_session: DbSession): # Use first() instead of scalar_one_or_none() ongoing_result = ongoing_result.first() + if ongoing_result: ongoing_overhaul, equipment_count = ongoing_result # Unpack the result tuple return { diff --git a/src/scope_equipment/model.py b/src/scope_equipment/model.py index 0d502f8..087e4b4 100644 --- a/src/scope_equipment/model.py +++ b/src/scope_equipment/model.py @@ -27,7 +27,13 @@ class ScopeEquipment(Base, DefaultMixin): class MasterEquipment(Base, DefaultMixin): __tablename__ = "ms_equipment_master" - parent_id = Column(UUID(as_uuid=True), nullable=True) + id = Column(UUID(as_uuid=True), primary_key=True, index=True) + parent_id = Column( + UUID(as_uuid=True), + ForeignKey("ms_equipment_master.id", ondelete="CASCADE"), + nullable=True, + ) + assetnum = Column(String, nullable=True) system_tag = Column(String, nullable=True) location_tag = Column(String, nullable=True) @@ -37,6 +43,7 @@ class MasterEquipment(Base, DefaultMixin): ) equipment_tree = relationship("MasterEquipmentTree", backref="master_equipments") + parent = relationship("MasterEquipment", remote_side=[id], lazy="selectin") class MasterEquipmentTree(Base, DefaultMixin): diff --git a/src/scope_equipment/schema.py b/src/scope_equipment/schema.py index fec2884..1a8bde8 100644 --- a/src/scope_equipment/schema.py +++ b/src/scope_equipment/schema.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import List, Optional +from typing import List, Optional, ForwardRef from uuid import UUID from pydantic import Field, computed_field, field_validator, validator @@ -40,11 +40,15 @@ class ScopeEquipmentRead(ScopeEquipmentBase): class ScopeEquipmentPagination(Pagination): items: List[ScopeEquipmentRead] = [] +EquipmentMasterReadRef = ForwardRef("MasterEquipmentRead") + class MasterEquipmentRead(DefultBase): assetnum: Optional[str] = Field(None, title="Asset Number") - location_tag: str + location_tag: Optional[str] = Field(None, title="Location Tag") name: str + parent_id: Optional[UUID] + parent: Optional[EquipmentMasterReadRef] = Field(None) # type: ignore class MasterEquipmentPagination(Pagination):