82 lines
2.9 KiB
Python

from datetime import datetime, time
from enum import Enum as PyEnum
from typing import Optional
from uuid import UUID
from pydantic import BaseModel, constr
from sqlalchemy import CheckConstraint, Column
from sqlalchemy.dialects.postgresql import JSONB
from sqlmodel import Enum, Field, Relationship, SQLModel
class ShopStatus(PyEnum):
ACTIVE = "active"
INACTIVE = "inactive"
SUSPENDED = "suspended"
class ShopLinkEntry(BaseModel):
name: constr(strip_whitespace=True, min_length=1)
url: constr(strip_whitespace=True, min_length=1)
class ShopLinks(BaseModel):
links: list[ShopLinkEntry]
class ShopBusinessHourEntry(BaseModel):
day: constr(strip_whitespace=True, min_length=1)
open_time: time
close_time: time
class ShopBusinessHours(BaseModel):
hours: list[ShopBusinessHourEntry]
class ShopAddress(SQLModel):
street: str = Field(max_length=255, nullable=False)
city: str = Field(max_length=100, nullable=False)
state: str = Field(max_length=100, nullable=True)
postal_code: str = Field(max_length=20, nullable=False)
country: str = Field(max_length=100, nullable=False)
class Shop(SQLModel, table=True):
__tablename__ = 'shop'
id: Optional[int] = Field(default=None, primary_key=True)
uuid: UUID = Field(nullable=False, unique=True)
owner_id: int = Field(foreign_key='user.id', nullable=False)
name: str = Field(max_length=100, nullable=False, unique=True)
description: str = Field(max_length=500, nullable=False)
created_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
status: ShopStatus = Field(sa_column=Column(Enum(ShopStatus)))
logo: Optional[str] = Field(max_length=100)
contact_email: str = Field(max_length=128, nullable=False, unique=True)
phone_number: str = Field(max_length=15, nullable=False)
address: ShopAddress = Field(sa_column=Column(JSONB, nullable=False, default=lambda: {}))
business_hours: ShopBusinessHours = Field(sa_column=Column(JSONB, nullable=False, default=lambda: {}))
links: ShopLinks = Field(sa_column=Column(JSONB, nullable=False, default=lambda: {}))
currency: str = Field(max_length=3, nullable=False)
owner: Optional["User"] = Relationship(
back_populates='owned_shop',
sa_relationship_kwargs={"foreign_keys": "[Shop.owner_id]"}
)
registered_users: list["User"] = Relationship(
back_populates='registered_shop',
sa_relationship_kwargs={"foreign_keys": "[User.shop_id]"}
)
# __table_args__ = (
# CheckConstraint("business_hours ? 'hours'", name="check_business_hours_keys"),
# CheckConstraint("links ? 'links'", name="check_links_keys"),
# CheckConstraint("address ? 'address'", name="check_address_keys")
# )
__all__ = ["Shop", "ShopBusinessHours", "ShopLinks", "ShopStatus"]