This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_db_requests.py
258 lines (218 loc) · 9.17 KB
/
valid_db_requests.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# pyright: reportUnknownMemberType=false
from config import LOGGER_CONFIG
from extra.custom_logger import CustomizeLogger
from fastapi import HTTPException
from models import database
from sqlalchemy.orm.session import Session
logger = CustomizeLogger.make_logger(LOGGER_CONFIG)
def get_school_by_name(session: Session, name: str) -> database.School:
logger.debug(f"Searching school with name {name}")
school = session.query(database.School).filter_by(name=name).first()
if school is None:
logger.debug(
f"Raised an exception because school with name {name} does not exist"
)
raise HTTPException(
status_code=404,
detail=f"School with name {name} does not exist",
)
logger.debug(f"Successfully found school with name {name}")
return school
def get_school_by_id(session: Session, uid: int) -> database.School:
logger.debug(f"Searching school with id {uid}")
school = session.query(database.School).filter_by(id=uid).first()
if school is None:
logger.debug(
f"Raised an exception because school with id {uid} does not exists"
)
raise HTTPException(
status_code=404,
detail=f"School with id {uid} does not exist",
)
logger.debug(f"Successfully found school with id {uid}")
return school
def get_tag_by_id(session: Session, uid: int) -> database.Tag:
logger.debug(f"Searching tag with id {uid}")
tag = session.query(database.Tag).filter_by(id=uid).first()
if tag is None:
logger.debug(f"Raised an exception because tag with id {uid} does not exists")
raise HTTPException(
status_code=404,
detail=f"Tag with id {uid} does not exist",
)
logger.debug(f"Successfully found tag with id {uid}")
return tag
def get_corpus_by_id(session: Session, uid: int) -> database.Corpus:
logger.debug(f"Searchin corpus with id {uid}")
corpus = session.query(database.Corpus).filter_by(id=uid).first()
if corpus is None:
logger.debug(
f"Raised an exception because corpus with id {uid} does not exists"
)
raise HTTPException(
status_code=404,
detail=f"Corpus with id {uid} does not exists",
)
logger.debug(f"Successfully found corpus with id {uid}")
return corpus
def get_teacher_by_id(session: Session, uid: int) -> database.Teacher:
logger.debug(f"Searching for teacher with id {uid}")
teacher = session.query(database.Teacher).filter_by(id=uid).first()
if teacher is None:
logger.debug("Raised an exception because teacher with id {id} does not exists")
raise HTTPException(
status_code=404,
detail=f"Teacher with id {uid} does not exists",
)
logger.debug(f"Successfully found teacher with id {uid}")
return teacher
def get_teacher_by_name(
session: Session, name: str, school_id: int
) -> database.Teacher:
logger.debug(f"Searching for teacher with name {name}")
teacher = (
session.query(database.Teacher)
.filter_by(name=name, school_id=school_id)
.first()
)
if teacher is None:
logger.debug(
f"Raised an exception because teacher with name {name} does not exists"
)
raise HTTPException(
status_code=404, detail=f"Teacher with name {name} does not exists"
)
logger.debug(f"Successfully found teacher with name {name}")
return teacher
def get_lesson_number_by_id(session: Session, uid: int) -> database.Lesson_number:
logger.debug(f"Searching for lesson_number with id {uid}")
lesson_number = session.query(database.Lesson_number).filter_by(id=uid).first()
if lesson_number is None:
logger.debug(
f"Raised an exception because lesson number with id {uid} does not exists"
)
raise HTTPException(
status_code=404,
detail=f"Lesson number with id {uid} does not exists",
)
logger.debug(f"Successfully found lesson number with id {uid}")
return lesson_number
def get_subclass_by_id(session: Session, uid: int) -> database.Subclass:
logger.debug(f"Searching for subclass with id {uid}")
subclass = session.query(database.Subclass).filter_by(id=uid).first()
if subclass is None:
logger.debug(
f"Raised an exception because subclass with id {uid} does not exists"
)
raise HTTPException(
status_code=404, detail=f"Subclass with id {uid} does not exists"
)
logger.debug(f"Successfully found subclass with id {uid}")
return subclass
def get_subclass_by_params(
session: Session,
school_id: int,
educational_level: int,
identificator: str,
additional_identificator: str,
) -> database.Subclass:
logger.debug(
f"Searching for subclass with params {educational_level=} {identificator=} {additional_identificator=} in school with {school_id=}"
)
subclass = (
session.query(database.Subclass)
.filter_by(
school_id=school_id,
educational_level=educational_level,
identificator=identificator,
additional_identificator=additional_identificator,
)
.first()
)
if subclass is None:
logger.debug(
f"Raised an exception because subclass with params {educational_level=} {identificator=} {additional_identificator=} does not exists in school with {school_id=}"
)
raise HTTPException(
status_code=404,
detail=f"Subclass with params {educational_level=} {identificator=} {additional_identificator=} does not exists in school with id {school_id}",
)
logger.debug(
f"Successfully found subclass with params {educational_level=} {identificator=} {additional_identificator=} in school with id {school_id}"
)
return subclass
def get_cabinet_by_id(session: Session, uid: int) -> database.Cabinet:
logger.debug(f"Searching for cabinter with id {uid}")
cabinet = session.query(database.Cabinet).filter_by(id=uid).first()
if cabinet is None:
logger.debug(
f"Raised an exception because cabinet with id {uid} does not exists"
)
raise HTTPException(
status_code=404, detail=f"Cabinet with id {uid} does not exists"
)
logger.debug(f"Successfully found cabinet with id {uid}")
return cabinet
def check_unique_account_by_telegram_id(session: Session, telegram_id: int):
logger.debug(f"Searching account with telegram id {telegram_id}")
account = session.query(database.Account).filter_by(telegram_id=telegram_id).first()
if account is not None:
logger.debug(
f"Raised an exception because account with telegram id {telegram_id} is already exists"
)
raise HTTPException(
status_code=409,
detail=f"Account with telegram id {telegram_id} is already exists",
)
logger.debug(f"Account with telegram id {telegram_id} does not exists")
def get_lesson_by_id(session: Session, uid: int) -> database.Lesson:
logger.debug(f"Searching lesson with id {uid}")
lesson = session.query(database.Lesson).filter_by(id=uid).first()
if lesson is None:
logger.debug(
f"Raised an exception because lesson with id {uid} does not exists"
)
raise HTTPException(
status_code=404, detail=f"Lesson with id {uid} does not exists"
)
logger.debug(f"Successfully found lesson with id {uid}")
return lesson
def get_account_by_telegram_id(session: Session, telegram_id: int) -> database.Account:
logger.debug(f"Searching account with telegram id {telegram_id}")
account = session.query(database.Account).filter_by(telegram_id=telegram_id).first()
if account is None:
logger.debug(
f"Raised an exception because account with telegram id {telegram_id} does not exists"
)
raise HTTPException(
status_code=404,
detail=f"Account with telegram id {telegram_id} does not exists",
)
logger.debug(f"Successfully found account with telegram id {telegram_id}")
return account
def get_role_by_id(session: Session, uid: int) -> database.Role:
logger.debug(f"Searching role with id {uid}")
role = session.query(database.Role).filter_by(id=uid).first()
if role is None:
logger.debug(
f"Raised an exception because account with role id {uid} does not exists"
)
raise HTTPException(
status_code=404,
detail=f"Role with role id {uid} does not exists",
)
logger.debug(f"Successfully found role with id {uid}")
return role
def get_tag_by_label(session: Session, label: str) -> database.Tag:
logger.debug(f"Searching tag with label {label}")
tag = session.query(database.Tag).filter_by(label=label).first()
if tag is None:
logger.debug(
f"Raised an exception because tag with role label {label} does not exists"
)
raise HTTPException(
status_code=404,
detail=f"Tag with label {label} does not exists",
)
logger.debug(f"Successfully found tag with label {label}")
return tag