52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import Boolean, Integer, Text, Uuid, text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..db.base_class import reg
|
|
from .modifier_category_product import ModifierCategoryProduct
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from .modifier import Modifier
|
|
from .product import Product
|
|
|
|
|
|
@reg.mapped_as_dataclass(unsafe_hash=True)
|
|
class ModifierCategory:
|
|
__tablename__ = "modifier_categories"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, server_default=text("gen_random_uuid()"))
|
|
name: Mapped[str] = mapped_column(Text, nullable=False, unique=True)
|
|
minimum: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
maximum: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
|
sort_order: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
|
|
products: Mapped[list[Product]] = relationship(
|
|
secondary=ModifierCategoryProduct.__table__, # type: ignore[attr-defined]
|
|
back_populates="modifier_categories",
|
|
)
|
|
modifiers: Mapped[list[Modifier]] = relationship(back_populates="modifier_category")
|
|
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
minimum: int,
|
|
maximum: int | None = None,
|
|
is_active: bool = True,
|
|
sort_order: int = 0,
|
|
id_: uuid.UUID | None = None,
|
|
):
|
|
if id_ is not None:
|
|
self.id = id_
|
|
self.name = name
|
|
self.minimum = minimum
|
|
self.maximum = maximum
|
|
self.is_active = is_active
|
|
self.sort_order = sort_order
|