-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
149 lines (104 loc) · 4.25 KB
/
models.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from datetime import datetime
from typing import List
from pydantic import BaseModel
from tortoise import Model, fields
from tortoise.contrib.pydantic import pydantic_model_creator
# Tortoise ORM models
class User(Model):
id = fields.IntField(pk=True, index=True)
username = fields.CharField(max_length=20, null=False, unique=True)
email = fields.CharField(max_length=200, null=False, unique=True)
password = fields.CharField(max_length=100, null=False)
is_verified = fields.BooleanField(default=False)
join_date = fields.DatetimeField(default=datetime.utcnow)
# not used for one seller
# class Business(Model):
# id = fields.IntField(pk=True, index=True)
# name = fields.CharField(max_length=100, null=False, unique=True)
# city = fields.CharField(max_length=100, null=False, default="Unspecified")
# region = fields.CharField(max_length=100, null=False, default="Unspecified")
# description = fields.TextField(null=True)
# logo = fields.CharField(max_length=200, null=False, default="defaultBusiness.png")
# owner = fields.ForeignKeyField('models.User', related_name='business')
class Product(Model):
id = fields.IntField(pk=True, index=True)
name = fields.CharField(max_length=100, null=False, index=True)
image = fields.CharField(max_length=200, null=False, default="defaultProduct.png")
original_price = fields.DecimalField(max_digits=12, decimal_places=2)
category = fields.ForeignKeyField('models.Category', related_name='products')
# new_price = fields.DecimalField(max_digits=12, decimal_places=2)
# percentage_discount = fields.IntField()
# offer_expiration_data = fields.DateField(default=datetime.utcnow)
# date_published = fields.DatetimeField(default=datetime.utcnow)
# business = fields.ForeignKeyField('models.Business', related_name='products')
class Category(Model):
id = fields.IntField(pk=True, index=True)
name = fields.CharField(max_length=30, null=False, unique=True)
products: fields.ReverseRelation[Product]
# Pydantic models
user_pydantic = pydantic_model_creator(User, name="User", exclude=("is_verified",))
user_pydantic_in = pydantic_model_creator(User, name="UserIn", exclude_readonly=True, exclude=("is_verified",
"join_date"))
user_pydantic_out = pydantic_model_creator(User, name="UserOut", exclude=("password",))
# business_pydantic = pydantic_model_creator(Business, name="Business")
# business_pydantic_in = pydantic_model_creator(Business, name="BusinessIn", exclude=("id", "logo",))
product_pydantic = pydantic_model_creator(Product, name="Product")
product_pydantic_in = pydantic_model_creator(Product, name="ProductIn", exclude_readonly=True)
category_pydantic = pydantic_model_creator(Category, name="Category")
category_pydantic_in = pydantic_model_creator(Category, name="CategoryIn", exclude_readonly=True)
# Response models
# Products
class AllProductsResponse(BaseModel):
status: str
products: List[product_pydantic]
class SingleProductResponse(BaseModel):
status: str
data: product_pydantic
class AddProductResponse(BaseModel):
status: str
message: str
product: product_pydantic
class DeleteProductResponse(BaseModel):
status: str
message: str
class UpdateProductResponse(BaseModel):
status: str
data: product_pydantic
# Users
class TokenResponse(BaseModel):
access_token: str
token_type: str
class RegistrationResponse(BaseModel):
status: str
message: str
class AllUsersResponse(BaseModel):
status: str
users: List[user_pydantic_out]
class SingleUserResponse(BaseModel):
status: str
data: dict
# Upload files
class UploadProfilePicResponse(BaseModel):
status: str
message: str
url: str
class UploadProductPicResponse(BaseModel):
status: str
message: str
url: str
# Businesses
# class UpdateBusinessResponse(BaseModel):
# status: str
# data: business_pydantic
# Health check
class HealthCheckResponse(BaseModel):
status: str
message: str
# Categories
class AddCategoryResponse(BaseModel):
status: str
message: str
category: category_pydantic
class AllCategoriesResponse(BaseModel):
status: str
categories: List[category_pydantic]