Refactor models and services in A76 module
- Updated GBultoService to use models.Package instead of models.GBulto. - Refactored Part model to use SQLAlchemy 2.0 style with Mapped and mapped_column. - Added timestamps (created_at, updated_at, deleted_at) to various pedimento models. - Improved relationships and foreign key constraints in pedimento models. - Updated PermissionRuleOct and Seal models to use Mapped and mapped_column. - Changed DTO configuration from orm_mode to from_attributes for better compatibility. - Removed obsolete models (models.py and models_ped.py) from the repository.
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
"""
|
||||
Modelos ORM para gestión de clases SCAII y SCAF
|
||||
"""
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text, Numeric, SmallInteger, ForeignKey
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from sqlalchemy import Integer, String, SmallInteger, ForeignKey, PrimaryKeyConstraint, ForeignKeyConstraint, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from core.database import Base
|
||||
import enum
|
||||
|
||||
# Importar modelos relacionados para type hints y relationships
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from api.v1.modules.a76.parts.models import Part
|
||||
@@ -20,42 +16,51 @@ class Class(Base):
|
||||
Modelo para la tabla GClases - Información de clases en sistemas SCAII y SCAF
|
||||
"""
|
||||
__tablename__ = "classes"
|
||||
__table_args__ = {"schema": "a76"}
|
||||
__table_args__ = (
|
||||
PrimaryKeyConstraint('id', name='classes_pkey'),
|
||||
UniqueConstraint('client_key', 'class_code', name='uq_classes_client_key_class_code'),
|
||||
ForeignKeyConstraint(['tenant_id'], ['a76.tenants.id'], name='fk_classes_tenant'),
|
||||
ForeignKeyConstraint(['material_key'], ['public.material_types.key'], name='fk_classes_material_type'),
|
||||
{"schema": "a76"}
|
||||
)
|
||||
|
||||
# Primary key compuesta
|
||||
client_key = Column(Integer, primary_key=True, nullable=False)
|
||||
class_code = Column(String(8), primary_key=True, nullable=False)
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
|
||||
# Unique constraint compuesta
|
||||
client_key: Mapped[int] = mapped_column()
|
||||
class_code: Mapped[str] = mapped_column(String(8))
|
||||
|
||||
tenant_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True)
|
||||
|
||||
# Basic information
|
||||
description_spanish = Column(String(500), nullable=True)
|
||||
description_english = Column(String(500), nullable=True)
|
||||
description_es: Mapped[Optional[str]] = mapped_column(String(500))
|
||||
description_en: Mapped[Optional[str]] = mapped_column(String(500))
|
||||
|
||||
# Material and measurement
|
||||
material_key = Column(String(10), ForeignKey('public.material_types.key'), nullable=True) # CLAVEMAT - homologated from TIPOMAT/TIPOMATEQUIPO
|
||||
unit_of_measure = Column(String(5), nullable=True) # UNIMED - homologated from UNIMEDIDA
|
||||
material_key: Mapped[Optional[str]] = mapped_column(String(10), ForeignKey('public.material_types.key')) # CLAVEMAT - homologated from TIPOMAT/TIPOMATEQUIPO
|
||||
unit_of_measure: Mapped[Optional[str]] = mapped_column(String(5)) # UNIMED - homologated from UNIMEDIDA
|
||||
|
||||
# Tariff fractions
|
||||
fraction = Column(String(10), nullable=True) # Mexican tariff fraction
|
||||
us_fraction = Column(String(16), nullable=True) # FRACCIONAME - US tariff fraction
|
||||
fraction: Mapped[Optional[str]] = mapped_column(String(10)) # Mexican tariff fraction
|
||||
us_fraction: Mapped[Optional[str]] = mapped_column(String(16)) # FRACCIONAME - US tariff fraction
|
||||
|
||||
# Additional classification
|
||||
sub_key = Column(String(5), nullable=True) # CLAVESUB
|
||||
physical_review = Column(SmallInteger, nullable=True) # REVFISICA
|
||||
iva_exempt_fraction = Column(String(4), nullable=True) # FRACCIONEXENTAIVA
|
||||
sub_key: Mapped[Optional[str]] = mapped_column(String(5)) # CLAVESUB
|
||||
physical_review: Mapped[Optional[int]] = mapped_column(SmallInteger) # REVFISICA
|
||||
iva_exempt_fraction: Mapped[Optional[str]] = mapped_column(String(4)) # FRACCIONEXENTAIVA
|
||||
|
||||
# Relationships
|
||||
material_type = relationship("MaterialType", foreign_keys=[material_key])
|
||||
material_type: Mapped[Optional["MaterialType"]] = relationship(foreign_keys=[material_key])
|
||||
|
||||
# Inverse relationship with GParts that have this class
|
||||
parts = relationship(
|
||||
"Part",
|
||||
parts: Mapped[list["Part"]] = relationship(
|
||||
primaryjoin="and_(Class.client_key == Part.client_key, Class.class_code == Part.part_class)",
|
||||
foreign_keys="[Part.client_key, Part.part_class]",
|
||||
viewonly=True,
|
||||
back_populates="part_class_info"
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Class(client_key={self.client_key}, class_code='{self.class_code}', description='{self.description_spanish}')>"
|
||||
def __repr__(self) -> str:
|
||||
return f"<Class(client_key={self.client_key}, class_code='{self.class_code}', description='{self.description_es}')>"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user