generated from gilles/template-webapp
import ali
This commit is contained in:
@@ -7,6 +7,7 @@ from app.models.category import Category
|
||||
from app.models.document import Document, DocumentType
|
||||
from app.models.item import Item, ItemStatus
|
||||
from app.models.location import Location, LocationType
|
||||
from app.models.shop import Shop
|
||||
|
||||
__all__ = [
|
||||
"Category",
|
||||
@@ -16,4 +17,5 @@ __all__ = [
|
||||
"ItemStatus",
|
||||
"Document",
|
||||
"DocumentType",
|
||||
"Shop",
|
||||
]
|
||||
|
||||
@@ -8,7 +8,7 @@ from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Date, DateTime, Enum, ForeignKey, Integer, Numeric, String, Text
|
||||
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
|
||||
|
||||
@@ -18,6 +18,7 @@ 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
|
||||
|
||||
@@ -27,6 +28,7 @@ class ItemStatus(str, enum.Enum):
|
||||
|
||||
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é
|
||||
@@ -79,6 +81,9 @@ class Item(Base):
|
||||
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)
|
||||
|
||||
@@ -89,6 +94,12 @@ class Item(Base):
|
||||
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(
|
||||
@@ -107,6 +118,13 @@ class Item(Base):
|
||||
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."""
|
||||
|
||||
56
backend/app/models/shop.py
Normal file
56
backend/app/models/shop.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""Modèle SQLAlchemy pour les boutiques/magasins.
|
||||
|
||||
Les boutiques représentent les lieux d'achat des objets.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import DateTime, 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.item import Item
|
||||
|
||||
|
||||
class Shop(Base):
|
||||
"""Boutique ou magasin.
|
||||
|
||||
Attributes:
|
||||
id: Identifiant unique auto-incrémenté
|
||||
name: Nom de la boutique (ex: "Amazon", "Leroy Merlin")
|
||||
description: Description optionnelle
|
||||
url: URL du site web de la boutique
|
||||
address: Adresse physique optionnelle
|
||||
created_at: Date/heure de création (auto)
|
||||
updated_at: Date/heure de dernière modification (auto)
|
||||
items: Relation vers les objets achetés dans cette boutique
|
||||
"""
|
||||
|
||||
__tablename__ = "shops"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(200), unique=True, nullable=False, index=True)
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
url: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
address: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
items: Mapped[list["Item"]] = relationship(
|
||||
"Item", back_populates="shop"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Shop(id={self.id}, name='{self.name}')>"
|
||||
Reference in New Issue
Block a user