66from fastapi .exceptions import ValidationException
77from fastapi_filter import FilterDepends
88from fastapi_sqlalchemy import db
9- from sqlalchemy import and_
10-
119from rating_api .exceptions import AlreadyExists , ObjectNotFound
12- from rating_api .models import Comment , Lecturer , LecturerUserComment , ReviewStatus
10+ from rating_api .models import (Comment , Lecturer , LecturerUserComment ,
11+ ReviewStatus )
1312from rating_api .schemas .base import StatusResponseModel
14- from rating_api .schemas .models import (
15- CommentGet ,
16- LecturerGet ,
17- LecturerGetAll ,
18- LecturerPatch ,
19- LecturerPost ,
20- LecturersFilter ,
21- LecturerUpdateRatingPatch ,
22- LecturerWithRank ,
23- )
24-
13+ from rating_api .schemas .models import (CommentGet , LecturerGet , LecturerGetAll ,
14+ LecturerPatch , LecturerPost ,
15+ LecturersFilter ,
16+ LecturerUpdateRatingPatch ,
17+ LecturerWithRank )
18+ from sqlalchemy import and_
2519
2620lecturer = APIRouter (prefix = "/lecturer" , tags = ["Lecturer" ])
2721
2822
2923@lecturer .post ("" , response_model = LecturerGet )
3024async def create_lecturer (
3125 lecturer_info : LecturerPost ,
32- _ = Depends (UnionAuth (scopes = ["rating.lecturer.create" ], allow_none = False , auto_error = True )),
26+ _ = Depends (
27+ UnionAuth (scopes = ["rating.lecturer.create" ], allow_none = False , auto_error = True )
28+ ),
3329) -> LecturerGet :
3430 """
3531 Scopes: `["rating.lecturer.create"]`
3632
3733 Создает преподавателя в базе данных RatingAPI
3834 """
3935 get_lecturer : Lecturer = (
40- Lecturer .query (session = db .session ).filter (Lecturer .timetable_id == lecturer_info .timetable_id ).one_or_none ()
36+ Lecturer .query (session = db .session )
37+ .filter (Lecturer .timetable_id == lecturer_info .timetable_id )
38+ .one_or_none ()
4139 )
4240 if get_lecturer is None :
43- new_lecturer : Lecturer = Lecturer .create (session = db .session , ** lecturer_info .model_dump ())
41+ new_lecturer : Lecturer = Lecturer .create (
42+ session = db .session , ** lecturer_info .model_dump ()
43+ )
4444 db .session .commit ()
4545 return LecturerGet .model_validate (new_lecturer )
4646 raise AlreadyExists (Lecturer , lecturer_info .timetable_id )
@@ -49,7 +49,11 @@ async def create_lecturer(
4949@lecturer .patch ("/import_rating" , response_model = LecturerUpdateRatingPatch )
5050async def update_lecturer_rating (
5151 lecturer_rank_info : list [LecturerWithRank ],
52- _ = Depends (UnionAuth (scopes = ["rating.lecturer.update_rating" ], allow_none = False , auto_error = True )),
52+ _ = Depends (
53+ UnionAuth (
54+ scopes = ["rating.lecturer.update_rating" ], allow_none = False , auto_error = True
55+ )
56+ ),
5357) -> LecturerUpdateRatingPatch :
5458 """
5559 Scopes: `["rating.lecturer.update_rating"]`
@@ -71,12 +75,18 @@ async def update_lecturer_rating(
7175 success_fl = False
7276
7377 lecturer_rank_dumped = lecturer_rank .model_dump ()
74- lecturer_rank_dumped ["rank_update_ts" ] = datetime .datetime .now (tz = datetime .timezone .utc )
78+ lecturer_rank_dumped ["rank_update_ts" ] = datetime .datetime .now (
79+ tz = datetime .timezone .utc
80+ )
7581
7682 lecturer_id = lecturer_rank_dumped .pop ("id" )
7783
7884 if Lecturer .get (id = lecturer_id , session = db .session ):
79- updated_lecturers .append (Lecturer .update (id = lecturer_id , session = db .session , ** lecturer_rank_dumped ))
85+ updated_lecturers .append (
86+ Lecturer .update (
87+ id = lecturer_id , session = db .session , ** lecturer_rank_dumped
88+ )
89+ )
8090 else :
8191 success_fl = False
8292
@@ -93,15 +103,21 @@ async def update_lecturer_rating(
93103
94104@lecturer .get ("/timetable-id/{timetable_id}" , response_model = LecturerGet )
95105async def get_lecturer_by_timetable_id (timetable_id : int ) -> LecturerGet :
96- lecturer : Lecturer = Lecturer .query (session = db .session ).filter (LEcturer .timetable_id == timetable_id ).one_or_none ()
106+ lecturer : Lecturer = (
107+ Lecturer .query (session = db .session )
108+ .filter (Lecturer .timetable_id == timetable_id )
109+ .one_or_none ()
110+ )
97111 if lecturer is None :
98112 raise ObjectNotFound (Lecturer , timetable_id )
99113 result = LecturerGet .model_validate (lecturer )
100114 return result
101115
102116
103117@lecturer .get ("/{id}" , response_model = LecturerGet )
104- async def get_lecturer (id : int , info : list [Literal ["comments" ]] = Query (default = [])) -> LecturerGet :
118+ async def get_lecturer (
119+ id : int , info : list [Literal ["comments" ]] = Query (default = [])
120+ ) -> LecturerGet :
105121 """
106122 Scopes: `["rating.lecturer.read"]`
107123
@@ -111,7 +127,9 @@ async def get_lecturer(id: int, info: list[Literal["comments"]] = Query(default=
111127 Если передано `'comments'`, то возвращаются одобренные комментарии к преподавателю.
112128 Subject лектора возвращшается либо из базы данных, либо из любого аппрувнутого комментария
113129 """
114- lecturer : Lecturer = Lecturer .query (session = db .session ).filter (Lecturer .id == id ).one_or_none ()
130+ lecturer : Lecturer = (
131+ Lecturer .query (session = db .session ).filter (Lecturer .id == id ).one_or_none ()
132+ )
115133 if lecturer is None :
116134 raise ObjectNotFound (Lecturer , id )
117135 result = LecturerGet .model_validate (lecturer )
@@ -123,7 +141,9 @@ async def get_lecturer(id: int, info: list[Literal["comments"]] = Query(default=
123141 if comment .review_status is ReviewStatus .APPROVED
124142 ]
125143 if "comments" in info and approved_comments :
126- result .comments = sorted (approved_comments , key = lambda comment : comment .create_ts , reverse = True )
144+ result .comments = sorted (
145+ approved_comments , key = lambda comment : comment .create_ts , reverse = True
146+ )
127147 if approved_comments :
128148 result .subjects = list ({comment .subject for comment in approved_comments })
129149 return result
@@ -168,7 +188,9 @@ async def get_lecturers(
168188 больше, чем переданный 'mark'.
169189 """
170190 lecturers_query = lecturer_filter .filter (
171- Lecturer .query (session = db .session ).outerjoin (Lecturer .comments ).group_by (Lecturer .id )
191+ Lecturer .query (session = db .session )
192+ .outerjoin (Lecturer .comments )
193+ .group_by (Lecturer .id )
172194 )
173195 lecturers_query = lecturer_filter .sort (lecturers_query )
174196 lecturers = lecturers_query .offset (offset ).limit (limit ).all ()
@@ -187,26 +209,34 @@ async def get_lecturers(
187209 if (
188210 mark is not None
189211 and approved_comments
190- and sum (comment .mark_general for comment in approved_comments ) / len (approved_comments ) <= mark
212+ and sum (comment .mark_general for comment in approved_comments )
213+ / len (approved_comments )
214+ <= mark
191215 ):
192216 continue
193217 if "comments" in info and approved_comments :
194218 lecturer_to_result .comments = sorted (
195- approved_comments , key = lambda comment : comment .create_ts , reverse = True
219+ approved_comments ,
220+ key = lambda comment : comment .create_ts ,
221+ reverse = True ,
196222 )
197223 if approved_comments :
198- lecturer_to_result .subjects = list ({comment .subject for comment in approved_comments })
224+ lecturer_to_result .subjects = list (
225+ {comment .subject for comment in approved_comments }
226+ )
199227 result .lecturers .append (lecturer_to_result )
200228 if len (result .lecturers ) == 0 :
201- raise ObjectNotFound (Lecturer , ' all' )
229+ raise ObjectNotFound (Lecturer , " all" )
202230 return result
203231
204232
205233@lecturer .patch ("/{id}" , response_model = LecturerGet )
206234async def update_lecturer (
207235 id : int ,
208236 lecturer_info : LecturerPatch ,
209- _ = Depends (UnionAuth (scopes = ["rating.lecturer.update" ], allow_none = False , auto_error = True )),
237+ _ = Depends (
238+ UnionAuth (scopes = ["rating.lecturer.update" ], allow_none = False , auto_error = True )
239+ ),
210240) -> LecturerGet :
211241 """
212242 Scopes: `["rating.lecturer.update"]`
@@ -217,22 +247,31 @@ async def update_lecturer(
217247
218248 check_timetable_id = (
219249 Lecturer .query (session = db .session )
220- .filter (and_ (Lecturer .timetable_id == lecturer_info .timetable_id , Lecturer .id != id ))
250+ .filter (
251+ and_ (Lecturer .timetable_id == lecturer_info .timetable_id , Lecturer .id != id )
252+ )
221253 .one_or_none ()
222254 )
223255 if check_timetable_id :
224256 raise AlreadyExists (Lecturer , lecturer_info .timetable_id )
225257
226258 result = LecturerGet .model_validate (
227- Lecturer .update (lecturer .id , ** lecturer_info .model_dump (exclude_unset = True ), session = db .session )
259+ Lecturer .update (
260+ lecturer .id ,
261+ ** lecturer_info .model_dump (exclude_unset = True ),
262+ session = db .session
263+ )
228264 )
229265 result .comments = None
230266 return result
231267
232268
233269@lecturer .delete ("/{id}" , response_model = StatusResponseModel )
234270async def delete_lecturer (
235- id : int , _ = Depends (UnionAuth (scopes = ["rating.lecturer.delete" ], allow_none = False , auto_error = True ))
271+ id : int ,
272+ _ = Depends (
273+ UnionAuth (scopes = ["rating.lecturer.delete" ], allow_none = False , auto_error = True )
274+ ),
236275):
237276 """
238277 Scopes: `["rating.lecturer.delete"]`
@@ -243,11 +282,15 @@ async def delete_lecturer(
243282 for comment in check_lecturer .comments :
244283 Comment .delete (id = comment .uuid , session = db .session )
245284
246- lecturer_user_comments = LecturerUserComment .query (session = db .session ).filter (LecturerUserComment .lecturer_id == id )
285+ lecturer_user_comments = LecturerUserComment .query (session = db .session ).filter (
286+ LecturerUserComment .lecturer_id == id
287+ )
247288 for lecturer_user_comment in lecturer_user_comments :
248289 LecturerUserComment .delete (lecturer_user_comment .id , session = db .session )
249290
250291 Lecturer .delete (session = db .session , id = id )
251292 return StatusResponseModel (
252- status = "Success" , message = "Lecturer has been deleted" , ru = "Преподаватель удален из RatingAPI"
293+ status = "Success" ,
294+ message = "Lecturer has been deleted" ,
295+ ru = "Преподаватель удален из RatingAPI" ,
253296 )
0 commit comments