-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
83 lines (55 loc) · 2.63 KB
/
models.py
File metadata and controls
83 lines (55 loc) · 2.63 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from datetime import datetime
from typing import Optional
import uuid
from sqlalchemy import Integer, String, ForeignKey, UUID
from sqlalchemy.orm import Mapped, relationship
from sqlalchemy.orm import mapped_column
from database import Base
# https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html
class BaseInfoMixin:
id: Mapped[int] = mapped_column(primary_key=True)
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
notes: Mapped[Optional[str]]
class User(BaseInfoMixin, Base):
__tablename__ = "users"
name: Mapped[str] = mapped_column(String(50), index=True)
email: Mapped[str] = mapped_column(String(150), unique=True, index=True)
hashed_password: Mapped[str]
user_uuid: Mapped[uuid.UUID] = mapped_column(default=uuid.uuid4)
is_active: Mapped[bool] = mapped_column(default=True)
verified_at: Mapped[bool] = mapped_column(default=False)
is_admin: Mapped[bool] = mapped_column(default=False, nullable=True)
tokens = relationship('UserRefreshToken', back_populates='user')
orders = relationship('Order', back_populates='user')
def __repr__(self) -> str:
return f'User {self.name} -> #{self.id}'
class UserRefreshToken(BaseInfoMixin, Base):
__tablename__ = 'refresh_tokens'
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
refresh_key: Mapped[str]
expires_at: Mapped[datetime]
user = relationship('User', back_populates='tokens')
class Product(BaseInfoMixin, Base):
__tablename__ = 'products'
title: Mapped[str]
price: Mapped[float]
image_url: Mapped[str] = mapped_column(default='', nullable=True)
image_file: Mapped[str] = mapped_column(default='', nullable=True)
products = relationship('OrderProduct', back_populates='product')
def __str__(self):
return f'Product {self.title} - #{self.id}'
class Order(BaseInfoMixin, Base):
__tablename__ = 'orders'
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
is_closed: Mapped[bool] = mapped_column(default=False)
user = relationship('User', back_populates='orders', lazy=False)
class OrderProduct(BaseInfoMixin, Base):
__tablename__ = 'order_products'
order_id: Mapped[int] = mapped_column(ForeignKey('orders.id'))
product_id: Mapped[int] = mapped_column(ForeignKey('products.id'))
price: Mapped[float] = mapped_column(default=0.0)
quantity: Mapped[int] = mapped_column(default=0)
product = relationship('Product', back_populates='products')
def __str__(self):
return f'OrderProduct {self.product.title} - #{self.id}, {self.quantity} >> {self.price} = {self.quantity * self.price}'
__repr__ = __str__