102 lines
4.0 KiB
Python
102 lines
4.0 KiB
Python
from datetime import datetime
|
|
from decimal import Decimal
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
DateTime,
|
|
ForeignKey,
|
|
Integer,
|
|
Numeric,
|
|
String,
|
|
Text,
|
|
func,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class Category(Base):
|
|
__tablename__ = "categories"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
slug: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
name: Mapped[str] = mapped_column(String(120))
|
|
description: Mapped[str] = mapped_column(Text, default="")
|
|
sort_order: Mapped[int] = mapped_column(default=0)
|
|
|
|
|
|
class Product(Base):
|
|
__tablename__ = "products"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
slug: Mapped[str] = mapped_column(String(120), unique=True, index=True)
|
|
title: Mapped[str] = mapped_column(String(200))
|
|
short_description: Mapped[str] = mapped_column(String(300))
|
|
description: Mapped[str] = mapped_column(Text, default="")
|
|
price: Mapped[Decimal] = mapped_column(Numeric(10, 2))
|
|
currency: Mapped[str] = mapped_column(String(3), default="RUB")
|
|
category_slug: Mapped[str] = mapped_column(String(64), index=True)
|
|
image_gradient: Mapped[str] = mapped_column(String(120), default="mint")
|
|
is_featured: Mapped[bool] = mapped_column(Boolean, default=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
delivery_note: Mapped[str] = mapped_column(String(200), default="Мгновенная выдача")
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now()
|
|
)
|
|
|
|
keys: Mapped[list["ProductKey"]] = relationship(back_populates="product")
|
|
|
|
|
|
class ProductKey(Base):
|
|
__tablename__ = "product_keys"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"), index=True)
|
|
code: Mapped[str] = mapped_column(Text)
|
|
is_sold: Mapped[bool] = mapped_column(Boolean, default=False, index=True)
|
|
order_id: Mapped[int | None] = mapped_column(ForeignKey("orders.id"), nullable=True)
|
|
sold_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now()
|
|
)
|
|
|
|
product: Mapped["Product"] = relationship(back_populates="keys")
|
|
order: Mapped["Order | None"] = relationship(back_populates="keys")
|
|
|
|
|
|
class Order(Base):
|
|
__tablename__ = "orders"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
public_id: Mapped[str] = mapped_column(String(32), unique=True, index=True)
|
|
email: Mapped[str] = mapped_column(String(255), index=True)
|
|
status: Mapped[str] = mapped_column(String(32), default="pending", index=True)
|
|
total: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=Decimal("0.00"))
|
|
customer_note: Mapped[str] = mapped_column(Text, default="")
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now()
|
|
)
|
|
paid_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
delivered_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
|
|
items: Mapped[list["OrderItem"]] = relationship(back_populates="order")
|
|
keys: Mapped[list["ProductKey"]] = relationship(back_populates="order")
|
|
|
|
|
|
class OrderItem(Base):
|
|
__tablename__ = "order_items"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"), index=True)
|
|
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"), index=True)
|
|
title: Mapped[str] = mapped_column(String(200))
|
|
unit_price: Mapped[Decimal] = mapped_column(Numeric(10, 2))
|
|
quantity: Mapped[int] = mapped_column(Integer, default=1)
|
|
delivered_codes: Mapped[str] = mapped_column(Text, default="")
|
|
|
|
order: Mapped["Order"] = relationship(back_populates="items")
|
|
product: Mapped["Product"] = relationship()
|