Files
home_stock/backend/app/models/item.py
2026-02-01 01:45:51 +01:00

132 lines
5.1 KiB
Python

"""Modèle SQLAlchemy pour les objets de l'inventaire.
Les objets (items) sont l'entité centrale de l'application.
Chaque objet appartient à une catégorie et est situé à un emplacement.
"""
from datetime import date, datetime
from decimal import Decimal
from typing import TYPE_CHECKING
from sqlalchemy import Date, DateTime, Enum, ForeignKey, Integer, JSON, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.sql import func
from app.core.database import Base
if TYPE_CHECKING:
from app.models.category import Category
from app.models.document import Document
from app.models.location import Location
from app.models.shop import Shop
import enum
class ItemStatus(str, enum.Enum):
"""Statut d'un objet."""
IN_STOCK = "in_stock" # En stock (non utilisé)
IN_USE = "in_use" # En cours d'utilisation
INTEGRATED = "integrated" # Intégré dans un autre objet
BROKEN = "broken" # Cassé/HS
SOLD = "sold" # Vendu
LENT = "lent" # Prêté
class Item(Base):
"""Objet de l'inventaire domestique.
Attributes:
id: Identifiant unique auto-incrémenté
name: Nom de l'objet (ex: "Perceuse sans fil Bosch")
description: Description détaillée optionnelle
quantity: Quantité en stock (défaut: 1)
price: Prix d'achat optionnel
purchase_date: Date d'achat optionnelle
status: Statut de l'objet (in_stock/in_use/broken/sold/lent)
brand: Marque optionnelle (ex: "Bosch", "Samsung")
model: Modèle optionnel (ex: "PSR 18 LI-2")
serial_number: Numéro de série optionnel
notes: Notes libres
category_id: ID de la catégorie (FK)
location_id: ID de l'emplacement (FK)
created_at: Date/heure de création (auto)
updated_at: Date/heure de dernière modification (auto)
category: Relation vers la catégorie
location: Relation vers l'emplacement
documents: Relation vers les documents (photos, notices, factures)
"""
__tablename__ = "items"
# Colonnes principales
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
quantity: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
status: Mapped[ItemStatus] = mapped_column(
Enum(ItemStatus, native_enum=False, length=20),
nullable=False,
default=ItemStatus.IN_STOCK,
)
# Informations produit
brand: Mapped[str | None] = mapped_column(String(100), nullable=True)
model: Mapped[str | None] = mapped_column(String(100), nullable=True)
serial_number: Mapped[str | None] = mapped_column(String(100), nullable=True, unique=True)
url: Mapped[str | None] = mapped_column(String(500), nullable=True) # Lien vers page produit
# Informations achat
price: Mapped[Decimal | None] = mapped_column(Numeric(10, 2), nullable=True)
purchase_date: Mapped[date | None] = mapped_column(Date, nullable=True)
# Caractéristiques techniques (clé-valeur, ex: {"RAM": "16 Go", "CPU": "i7"})
characteristics: Mapped[dict | None] = mapped_column(JSON, nullable=True, default=None)
# Notes
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
# Foreign Keys
category_id: Mapped[int] = mapped_column(
Integer, ForeignKey("categories.id", ondelete="RESTRICT"), nullable=False, index=True
)
location_id: Mapped[int] = mapped_column(
Integer, ForeignKey("locations.id", ondelete="RESTRICT"), nullable=False, index=True
)
parent_item_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("items.id", ondelete="SET NULL"), nullable=True, index=True
)
shop_id: Mapped[int | None] = mapped_column(
Integer, ForeignKey("shops.id", ondelete="SET NULL"), nullable=True, index=True
)
# Timestamps
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
onupdate=func.now(),
nullable=False,
)
# Relations
category: Mapped["Category"] = relationship("Category", back_populates="items")
location: Mapped["Location"] = relationship("Location", back_populates="items")
documents: Mapped[list["Document"]] = relationship(
"Document", back_populates="item", cascade="all, delete-orphan"
)
shop: Mapped["Shop | None"] = relationship("Shop", back_populates="items")
parent_item: Mapped["Item | None"] = relationship(
"Item", remote_side=[id], foreign_keys=[parent_item_id], back_populates="children"
)
children: Mapped[list["Item"]] = relationship(
"Item", back_populates="parent_item", foreign_keys=[parent_item_id]
)
def __repr__(self) -> str:
"""Représentation string de l'objet."""
return f"<Item(id={self.id}, name='{self.name}', status={self.status.value})>"