-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
115 lines (92 loc) · 2.91 KB
/
user.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
import re
from typing import List
import uuid
from pydantic import BaseModel, EmailStr, Field, field_validator
class Token(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), alias="_id")
user_id: str
token: str
class Config:
populate_by_name = True
json_schema_extra = {
"example": {
"user_id": "sample",
"token": "sample",
}
}
class UserVotes(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), alias="_id")
user_id: str
list_of_posts_upvotes: List[str] = []
list_of_posts_downvotes: List[str] = []
list_of_comments_upvotes: List[str] = []
list_of_comments_downvotes: List[str] = []
class Config:
populate_by_name = True
json_schema_extra = {
"example": {
"user_id": "sample",
"list_of_posts_upvotes": ["sample"],
"list_of_posts_downvotes": ["sample"],
"list_of_comments_upvotes": ["sample"],
"list_of_comments_downvotes": ["sample"],
}
}
def check_pwd(pwd):
re_for_pwd: re.Pattern[str] = re.compile(r"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{5,}$")
if not re_for_pwd.match(pwd):
raise ValueError(
"Invalid password - must contain at least 1 letter and 1 number and"
"be at least 5 characters long"
)
return pwd
class UpdateUser(BaseModel):
email_address: EmailStr = None
password: str = None
username: str = None
avatar: int = None
@field_validator("password")
@classmethod
def regex_match(cls, pwd: str) -> str:
return check_pwd(pwd)
class RegisterUser(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), alias="_id")
email_address: EmailStr
password: str
username: str
avatar: int
@field_validator("password")
@classmethod
def regex_match(cls, pwd: str) -> str:
return check_pwd(pwd)
class Config:
populate_by_name = True
json_schema_extra = {
"example": {
"email_address": "[email protected]",
"password": "password",
"username": "john_doe",
"avatar": 1,
}
}
class LoginUser(BaseModel):
email_address: EmailStr
password: str
class Config:
populate_by_name = True
json_schema_extra = {
"example": {
"email_address": "[email protected]",
"password": "password",
}
}
class Preferences(BaseModel):
id: str = Field(default_factory=lambda: str(uuid.uuid4()), alias="_id")
preferences: List[str]
class Config:
populate_by_name = True
json_schema_extra = {
"example": {
"preferences": ["sports", "food", "mastering the art of getting bored"]
}
}