11import datetime
22import logging
3- from typing import Any , Literal
3+ from typing import Any , Union
44
5- from fastapi import APIRouter , Depends , UploadFile , File , Query , HTTPException
5+ from fastapi import APIRouter , Depends , UploadFile , File
66from fastapi_sqlalchemy import db
7- from calendar_backend .models .db import Lecturer
8- from calendar_backend .models .db import CommentLecturer as DbCommentLecturer
9- from calendar_backend .models .db import Photo as DbPhoto
107
11- from calendar_backend .settings import get_settings
8+ from calendar_backend .exceptions import ObjectNotFound
129from calendar_backend .methods import utils , auth
10+ from calendar_backend .models .db import CommentLecturer as DbCommentLecturer
11+ from calendar_backend .models .db import Lecturer
12+ from calendar_backend .models .db import Photo as DbPhoto
1313from calendar_backend .routes .models import (
1414 LecturerEvents ,
1515 GetListLecturer ,
1818 LecturerPatch ,
1919 Photo ,
2020 LecturerPhotos ,
21- CommentLecturer , LecturerCommentPost , LecturerCommentPatch , LecturerComments
21+ CommentLecturer ,
22+ LecturerCommentPost ,
23+ LecturerCommentPatch ,
24+ LecturerComments ,
2225)
23- from calendar_backend .exceptions import ObjectNotFound
26+ from calendar_backend .settings import get_settings
2427
2528lecturer_router = APIRouter (prefix = "/timetable/lecturer" , tags = ["Lecturer" ])
2629settings = get_settings ()
2730logger = logging .getLogger (__name__ )
2831
2932
30- @lecturer_router .get ("/{id}" , response_model = LecturerEvents )
33+ @lecturer_router .get ("/{id}" , response_model = Union [ LecturerEvents , LecturerGet ] )
3134async def http_get_lecturer_by_id (
3235 id : int , start : datetime .date | None = None , end : datetime .date | None = None
33- ) -> LecturerEvents :
36+ ) -> LecturerEvents | LecturerGet :
3437 lecturer = Lecturer .get (id , session = db .session )
3538 lecturer .avatar_link = lecturer .avatar .link if lecturer .avatar else None
3639 result = LecturerEvents .from_orm (lecturer )
3740 if start and end :
38- result .events = await utils .get_lecturer_lessons_in_daterange (lecturer , start , end )
39- return result
41+ result .events = await utils .get_lecturer_lessons_in_daterange (lecturer , start , end , db .session )
42+ return LecturerEvents .from_orm (result )
43+ return LecturerGet .from_orm (result )
4044
4145
4246@lecturer_router .get ("/" , response_model = GetListLecturer )
@@ -85,32 +89,30 @@ async def http_upload_photo(lecturer_id: int, photo: UploadFile = File(...)) ->
8589
8690
8791@lecturer_router .get ("/{lecturer_id}/photo" , response_model = LecturerPhotos )
88- async def http_get_lecturer_photos (lecturer_id : int , limit : int = 10 ,
89- offset : int = 0 ) -> LecturerPhotos :
92+ async def http_get_lecturer_photos (lecturer_id : int , limit : int = 10 , offset : int = 0 ) -> LecturerPhotos :
9093 res = DbPhoto .get_all (session = db .session ).filter (DbPhoto .lecturer_id == lecturer_id )
9194 if limit :
9295 cnt , res = res .count (), res .offset (offset ).limit (limit ).all ()
9396 else :
9497 cnt , res = res .count (), res .offset (offset ).all ()
95- return LecturerPhotos (** {
96- "items" : [row .link for row in res ],
97- "limit" : limit ,
98- "offset" : offset ,
99- "total" : cnt
100- })
98+ return LecturerPhotos (** {"items" : [row .link for row in res ], "limit" : limit , "offset" : offset , "total" : cnt })
10199
102100
103101@lecturer_router .post ("/{lecturer_id}/comment/" , response_model = CommentLecturer )
104102async def http_comment_lecturer (lecturer_id : int , comment : LecturerCommentPost ) -> CommentLecturer :
105- return CommentLecturer .from_orm (DbCommentLecturer .create (lecturer_id = lecturer_id , session = db .session , ** comment .dict ()))
103+ return CommentLecturer .from_orm (
104+ DbCommentLecturer .create (lecturer_id = lecturer_id , session = db .session , ** comment .dict ())
105+ )
106106
107107
108108@lecturer_router .patch ("/{lecturer_id}/comment/{id}" , response_model = CommentLecturer )
109109async def http_update_comment_lecturer (id : int , lecturer_id : int , comment_inp : LecturerCommentPatch ) -> CommentLecturer :
110110 comment = DbCommentLecturer .get (id = id , session = db .session )
111111 if comment .lecturer_id != lecturer_id :
112112 raise ObjectNotFound (DbCommentLecturer , id )
113- return CommentLecturer .from_orm (DbCommentLecturer .update (id , session = db .session , ** comment_inp .dict (exclude_unset = True )))
113+ return CommentLecturer .from_orm (
114+ DbCommentLecturer .update (id , session = db .session , ** comment_inp .dict (exclude_unset = True ))
115+ )
114116
115117
116118@lecturer_router .post ("/{id}/avatar" , response_model = LecturerGet )
@@ -149,12 +151,7 @@ async def http_get_all_lecturer_comments(lecturer_id: int, limit: int = 10, offs
149151 cnt , res = res .count (), res .offset (offset ).limit (limit ).all ()
150152 else :
151153 cnt , res = res .count (), res .offset (offset ).all ()
152- return LecturerComments (** {
153- "items" : res ,
154- "limit" : limit ,
155- "offset" : offset ,
156- "total" : cnt
157- })
154+ return LecturerComments (** {"items" : res , "limit" : limit , "offset" : offset , "total" : cnt })
158155
159156
160157@lecturer_router .get ("/{lecturer_id}/photo/{id}" , response_model = Photo )
@@ -163,4 +160,3 @@ async def get_photo(id: int, lecturer_id: int) -> Photo:
163160 if photo .lecturer_id != lecturer_id :
164161 raise ObjectNotFound (DbPhoto , id )
165162 return Photo .from_orm (photo )
166-
0 commit comments