generated from gilles/template-webapp
claude code
This commit is contained in:
61
backend/app/models/category.py
Normal file
61
backend/app/models/category.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""Modèle SQLAlchemy pour les catégories d'objets.
|
||||
|
||||
Les catégories permettent de classer les objets par domaine d'utilisation
|
||||
(bricolage, informatique, électronique, cuisine, etc.).
|
||||
"""
|
||||
|
||||
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 Category(Base):
|
||||
"""Catégorie d'objets.
|
||||
|
||||
Attributes:
|
||||
id: Identifiant unique auto-incrémenté
|
||||
name: Nom de la catégorie (ex: "Bricolage", "Informatique")
|
||||
description: Description optionnelle de la catégorie
|
||||
color: Couleur hexadécimale pour l'affichage (ex: "#3b82f6")
|
||||
icon: Nom d'icône optionnel (ex: "wrench", "computer")
|
||||
created_at: Date/heure de création (auto)
|
||||
updated_at: Date/heure de dernière modification (auto)
|
||||
items: Relation vers les objets de cette catégorie
|
||||
"""
|
||||
|
||||
__tablename__ = "categories"
|
||||
|
||||
# Colonnes
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False, index=True)
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
color: Mapped[str | None] = mapped_column(String(7), nullable=True) # Format: #RRGGBB
|
||||
icon: Mapped[str | None] = mapped_column(String(50), nullable=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
|
||||
items: Mapped[list["Item"]] = relationship(
|
||||
"Item", back_populates="category", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""Représentation string de la catégorie."""
|
||||
return f"<Category(id={self.id}, name='{self.name}')>"
|
||||
Reference in New Issue
Block a user