Refactor Part model and related components
- Updated the Part model in models.py to improve readability and maintainability by organizing imports and formatting. - Enhanced relationships in the Part model for better clarity. - Modified main.py to streamline imports for parts and related models. - Adjusted partForm.svelte to correctly reference properties from inv_data and fa_data. - Updated +page.svelte to fix the import path for DataTable and refine data handling. - Changed edit/[[id]]/+page.svelte to enforce type safety for the 'type' variable.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Modelos ORM para gestión de partes/componentes - Anexo 76 (Master Data)
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
@@ -8,19 +9,29 @@ from typing import TYPE_CHECKING, Optional
|
||||
from api.v1.common.base_models import TenantScopedMixin, TimestampMixin
|
||||
from core.database import Base
|
||||
from sqlalchemy import (
|
||||
ForeignKeyConstraint, Integer, Numeric, PrimaryKeyConstraint,
|
||||
String, UniqueConstraint, Boolean, DateTime
|
||||
ForeignKeyConstraint,
|
||||
Integer,
|
||||
Numeric,
|
||||
PrimaryKeyConstraint,
|
||||
String,
|
||||
UniqueConstraint,
|
||||
Boolean,
|
||||
DateTime,
|
||||
)
|
||||
|
||||
# Importante usar relationship y Mapped
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from api.v1.modules.a76.classes.models import Class
|
||||
from api.v1.modules.public.reference_data.currency_types.models import CurrencyType
|
||||
from api.v1.modules.a76.general_catalogs.units_of_measure.models import UnitOfMeasure
|
||||
from api.v1.modules.a76.general_catalogs.units_of_measure.models import (
|
||||
UnitOfMeasure,
|
||||
)
|
||||
from api.v1.modules.a24.fa.fa_parts.models import FaPart
|
||||
from api.v1.modules.a24.inv.inv_parts.models import InvPart
|
||||
|
||||
|
||||
class Part(Base, TenantScopedMixin, TimestampMixin):
|
||||
__tablename__ = "parts"
|
||||
__table_args__ = (
|
||||
@@ -30,14 +41,21 @@ class Part(Base, TenantScopedMixin, TimestampMixin):
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["unit_of_measure", "tenant_id", "company_id"],
|
||||
["a76.units_of_measure.code", "a76.units_of_measure.tenant_id",
|
||||
"a76.units_of_measure.company_id"],
|
||||
[
|
||||
"a76.units_of_measure.code",
|
||||
"a76.units_of_measure.tenant_id",
|
||||
"a76.units_of_measure.company_id",
|
||||
],
|
||||
),
|
||||
# Puente hacia la tabla de clases
|
||||
ForeignKeyConstraint(
|
||||
["part_class", "tenant_id", "company_id"],
|
||||
["a76.classes.class_code", "a76.classes.tenant_id", "a76.classes.company_id"],
|
||||
name="fk_parts_class"
|
||||
[
|
||||
"a76.classes.class_code",
|
||||
"a76.classes.tenant_id",
|
||||
"a76.classes.company_id",
|
||||
],
|
||||
name="fk_parts_class",
|
||||
),
|
||||
UniqueConstraint(
|
||||
"tenant_id", "company_id", "part_number", name="client_part_ukey"
|
||||
@@ -54,7 +72,7 @@ class Part(Base, TenantScopedMixin, TimestampMixin):
|
||||
description_english: Mapped[Optional[str]] = mapped_column(String(500))
|
||||
part_class: Mapped[Optional[str]] = mapped_column(String(8))
|
||||
unit_of_measure: Mapped[Optional[str]] = mapped_column(String(5))
|
||||
|
||||
|
||||
unit_cost: Mapped[Optional[Decimal]] = mapped_column(Numeric(23, 8))
|
||||
currency_type: Mapped[Optional[str]] = mapped_column(String(2))
|
||||
currency_key: Mapped[Optional[str]] = mapped_column(String(3))
|
||||
@@ -73,31 +91,33 @@ class Part(Base, TenantScopedMixin, TimestampMixin):
|
||||
|
||||
is_active: Mapped[Optional[bool]] = mapped_column(Boolean, default=True)
|
||||
part_photo: Mapped[Optional[str]] = mapped_column(String(255))
|
||||
|
||||
|
||||
creation_date: Mapped[Optional[int]] = mapped_column()
|
||||
modification_date: Mapped[Optional[int]] = mapped_column()
|
||||
modification_date_iso: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
||||
|
||||
# --- RELACIONES CORREGIDAS ---
|
||||
currency: Mapped[Optional["CurrencyType"]] = relationship(
|
||||
foreign_keys="[Part.currency_key]"
|
||||
)
|
||||
# --- RELACIONES ---
|
||||
currency: Mapped[Optional["CurrencyType"]] = relationship("CurrencyType")
|
||||
unit_of_measure_info: Mapped[Optional["UnitOfMeasure"]] = relationship(
|
||||
foreign_keys="[Part.unit_of_measure, Part.tenant_id, Part.company_id]"
|
||||
"UnitOfMeasure"
|
||||
)
|
||||
part_class_info: Mapped[Optional["Class"]] = relationship(
|
||||
"Class",
|
||||
back_populates="parts",
|
||||
foreign_keys="[Part.part_class, Part.tenant_id, Part.company_id]"
|
||||
"Class", back_populates="parts", overlaps="unit_of_measure_info"
|
||||
)
|
||||
|
||||
# Extensiones Anexo 24
|
||||
fa_data: Mapped[Optional["FaPart"]] = relationship(
|
||||
"FaPart", back_populates="master_info", uselist=False, cascade="all, delete-orphan"
|
||||
"FaPart",
|
||||
back_populates="master_info",
|
||||
uselist=False,
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
inv_data: Mapped[Optional["InvPart"]] = relationship(
|
||||
"InvPart", back_populates="master_info", uselist=False, cascade="all, delete-orphan"
|
||||
"InvPart",
|
||||
back_populates="master_info",
|
||||
uselist=False,
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Part(id={self.id}, part_number='{self.part_number}')>"
|
||||
return f"<Part(id={self.id}, part_number='{self.part_number}')>"
|
||||
|
||||
Reference in New Issue
Block a user