aboutsummaryrefslogtreecommitdiffstats
path: root/thallium-backend/src/orm/templates.py
blob: cffd5ca30fcd5d868b01e6652eac5c6305173dfc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from datetime import datetime
from decimal import Decimal

from sqlalchemy import Column, DateTime, ForeignKey, Integer, Table
from sqlalchemy.orm import Mapped, mapped_column, relationship

from .base import AuditBase, Base

template_variant_association = Table(
    "template_variant",
    Base.metadata,
    Column("template_id", Integer, ForeignKey("templates.template_id", ondelete="CASCADE")),
    Column("variant_id", Integer, ForeignKey("variants.variant_id", ondelete="CASCADE")),
)


class Template(AuditBase, Base):
    """An authenticated user of the service."""

    __tablename__ = "templates"

    template_id: Mapped[int] = mapped_column(primary_key=True)
    title: Mapped[str]
    product_id: Mapped[int]
    mockup_file_url: Mapped[str]
    last_synced: Mapped[datetime] = mapped_column(DateTime(timezone=True))

    variants: Mapped[list["Variant"]] = relationship(
        "Variant",
        secondary=template_variant_association,
        back_populates="templates",
        lazy=True,
    )

    def loggify(self) -> str:
        """Human readable repr for logging."""
        return f"{self.__class__.__name__} {self.title} ({self.template_id})"


class Variant(AuditBase, Base):
    """An authenticated user of the service."""

    __tablename__ = "variants"

    variant_id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    size: Mapped[str]
    colour: Mapped[str]
    colour_code: Mapped[str]
    price: Mapped[Decimal]
    last_synced: Mapped[datetime] = mapped_column(DateTime(timezone=True))

    templates: Mapped[list["Template"]] = relationship(
        "Template",
        secondary=template_variant_association,
        back_populates="variants",
        lazy=True,
    )

    def loggify(self) -> str:
        """Human readable repr for logging."""
        return f"{self.__class__.__name__} {self.name} ({self.variant_id})"