-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
73 lines (57 loc) · 1.78 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
from pydantic import BaseModel, Field
from bson import ObjectId
from typing import List, Optional
# This class is using for "_id"-like attributes in Mongo. ObjectId(...) representation
class PyObjectId(ObjectId):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
if not ObjectId.is_valid(v):
raise ValueError('Invalid objectid')
return ObjectId(v)
@classmethod
def __modify_schema__(cls, field_schema):
field_schema.update(type='string')
# This class is using for representing User collection in Mongo.
class User(BaseModel):
id: Optional[PyObjectId] = Field(alias='_id')
name: str
email: str
class Config:
arbitrary_types_allowed = True
json_encoders = {
ObjectId: str
}
# This class is using for representing Food collection in Mongo.
class Food(BaseModel):
id: Optional[PyObjectId] = Field(alias='_id')
restaurant: Optional[PyObjectId]
name: str
category: Optional[PyObjectId]
unit_price: float
count: Optional[int]
class Config:
arbitrary_types_allowed = True
json_encoders = {
ObjectId: str
}
# This class is using for representing Order collection in Mongo.
class Order(BaseModel):
id: Optional[PyObjectId] = Field(alias='_id')
user: User
foods: List[Food]
user_note: Optional[str] = None
order_date: Optional[str] = None
complete_date: Optional[str] = None
inserted_id: Optional[PyObjectId] = None
def __init__(self):
super().__init__()
def __init__(self, *a, **kw):
super().__init__(*a, **kw)
class Config:
arbitrary_types_allowed = True
json_encoders = {
ObjectId: str
}