-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
140 lines (96 loc) · 2.8 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
from pydantic import BaseModel, BaseSettings, EmailStr, constr
from os import path
import datetime
from typing import Optional, Union, List
from enum import Enum
ENV_PATH = path.join(path.dirname(__file__), "..", ".env")
UVICORN_PREFIX = "uvicorn_"
class Settings(BaseSettings):
# Mongo settings
mongodb_host: str
mongodb_port: Optional[int] = None
mongodb_user: str
mongodb_password: str
mongodb_database: Optional[str] = None
mongodb_driver: str
mongodb_params: Optional[dict] = None
mongodb_useAtlas: bool
# Uvicorn settings
uvicorn_host: str
uvicorn_port: int
uvicorn_reload: bool
# Auth Related
auth_secret_key: str
auth_algorithm: str
auth_token_expiration_minutes: int
class Config:
env_file = ENV_PATH
# Access all of the Uvicorn .env vars
def get_uvicorn_settings(self) -> dict:
return {attr.replace(UVICORN_PREFIX, ''): getattr(self, attr)
for attr in dir(self) if attr.startswith(UVICORN_PREFIX)}
"""
Pydantic Models for Creating, Updating, Reading Users
"""
class CreateUser(BaseModel):
first_name: str
last_name: str
email: EmailStr
username: constr(min_length=5, max_length=50)
password: constr(min_length=5, max_length=50)
class UpdateUser(BaseModel):
email: Optional[EmailStr] = None
username: Optional[constr(min_length=5, max_length=50)] = None
password: Optional[constr(min_length=5, max_length=50)] = None
first_name: Optional[str] = None
last_name: Optional[str] = None
class ReadUser(BaseModel):
userID: str
first_name: str
last_name: str
email: EmailStr
username: constr(min_length=5, max_length=50)
created_at: datetime.datetime
class ReadUsers(BaseModel):
Users: Union[ReadUser, List[ReadUser]]
"""
Pydantic Models for CRUD on Posts
"""
class CreatePost(BaseModel):
title: constr(max_length=50)
content: constr(max_length=250)
class PostDB(CreatePost):
created_at: datetime.datetime
updated_at: Optional[datetime.datetime] = None
userID: str
likes: int = 0
dislikes: int = 0
class UpdatePostDB(BaseModel):
title: Optional[constr(max_length=50)] = None
content: Optional[constr(max_length=250)] = None
"""
Create a Likes/Dislikes Collection
"""
class LikeType(str, Enum):
like = "like"
dislike = "dislike"
class Action(BaseModel):
action: LikeType
class LikeDislike(BaseModel):
userID: str
postID: str
action: LikeType
"""
The below two models would be used when the front-end needs to know
if a logged in user has liked/disliked a set of posts in their feed
"""
class postGetAction(BaseModel):
postIDs: List[str]
"""
Auth related Models
"""
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
userID: str