-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.py
140 lines (84 loc) · 2.9 KB
/
schemas.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
"""Schemas for request body data validation. Works both for input and output."""
from typing import List
from pydantic import BaseModel, HttpUrl, Field
class UserInArticleView(BaseModel):
"""What fields will be in nested sent_to_user list."""
# id: int
telegram_id: int
class Config:
"""Enable ORM mode."""
orm_mode = True
class ArticleBase(BaseModel):
"""Base serializer for an article."""
id: int
text: str = Field(..., min_length=50, max_length=1024)
image_url: HttpUrl = Field(..., title="Image URL")
language_code: str = Field("ru", max_length=3, min_length=2)
sent_to_user: List[UserInArticleView] = []
class Config:
"""Enable ORM mode for all child methods."""
orm_mode = True
class ArticleResponse(ArticleBase):
sent_to_user: List[int] = []
class ArticleCreate(BaseModel):
"""Serializer for creating an article."""
text: str = Field(..., min_length=50, max_length=1024)
image_url: HttpUrl = Field(..., title="Image URL")
language_code: str = Field("ru", max_length=3, min_length=2)
class Config:
"""Enable ORM mode for all child methods."""
orm_mode = True
class ArticleSentView(BaseModel):
"""What fields will be in nested sent_articles list."""
id: int
class Config:
"""Enable ORM mode."""
orm_mode = True
class UserBase(BaseModel):
"""Base serializer for a user."""
telegram_id: int = Field(..., ge=0)
username: str = Field(..., max_length=50)
pet_name: str = Field(..., max_length=50)
language_code: str = Field(..., max_length=3, min_length=2)
active: bool = True
# sent_articles: List[ArticleBase] = []
class Config:
"""Enable ORM mode for all child methods."""
orm_mode = True
class UserInfo(BaseModel):
"""Get all users ids."""
id: int
telegram_id: int
language_code: str
class Config:
"""Enable ORM mode for all child methods."""
orm_mode = True
class UserEnable(BaseModel):
"""Enable or disable user receiving messages."""
telegram_id: int | None = None
username: str | None = None
pet_name: str | None = None
language_code: str | None = None
active: bool
class Config:
"""Enable ORM mode for all child methods."""
orm_mode = True
class UserArticlesSentView(UserBase):
"""What fields will be in nested sent_articles list."""
sent_articles: List[ArticleSentView] = []
class Config:
"""Enable ORM mode."""
orm_mode = True
class UserCreate(UserBase):
"""Serializer for creating a user."""
pass
class UserEdit(UserBase):
"""Serializer for editing a user."""
id: int
class SetSent(BaseModel):
"""Set article as sent for user."""
user_id: int = Field(..., ge=0)
article_id: int = Field(..., ge=0)
class Config:
"""Enable ORM mode."""
orm_mode = True