11import logging
2- from typing import Any
2+ from typing import Any , Literal
33
44from auth_lib .fastapi import UnionAuth
55from fastapi import APIRouter , Depends
66from fastapi_sqlalchemy import db
7+ from sqlalchemy .orm import Query , joinedload
78
89from calendar_backend .exceptions import ObjectNotFound
10+ from calendar_backend .methods .image import get_photo_webpath
911from calendar_backend .models .db import ApproveStatuses , Lecturer
1012from calendar_backend .models .db import Photo as DbPhoto
1113from calendar_backend .routes .models import GetListLecturer , LecturerGet , LecturerPatch , LecturerPost
2325@router .get ("/{id}" , response_model = LecturerGet )
2426async def get_lecturer_by_id (id : int ) -> LecturerGet :
2527 lecturer = Lecturer .get (id , session = db .session )
28+ result = LecturerGet .from_orm (Lecturer .get (id , session = db .session ))
2629 if lecturer .avatar_id :
27- lecturer .avatar_link = lecturer .avatar .link
28- return LecturerGet . from_orm ( Lecturer . get ( id , session = db . session ))
30+ result .avatar_link = get_photo_webpath ( lecturer .avatar .link )
31+ return result
2932
3033
3134@lecturer_router .get ("/" , response_model = GetListLecturer ) # DEPRICATED TODO: Drop 2023-04-01
@@ -34,15 +37,26 @@ async def get_lecturers(
3437 query : str = "" ,
3538 limit : int = 10 ,
3639 offset : int = 0 ,
40+ order_by : Literal ['first_name' , 'last_name' ] | None = None ,
3741) -> dict [str , Any ]:
38- res = Lecturer .get_all (session = db .session ).filter (Lecturer .search (query ))
42+ query : Query = Lecturer .get_all (session = db .session ).filter (Lecturer .search (query ))
43+ query = query .options (joinedload (Lecturer .avatar )) # Сразу загружаем аватарки
44+ if order_by :
45+ query = query .order_by (order_by )
46+ query = query .order_by ('id' )
3947 if limit :
40- cnt , res = res .count (), res .offset (offset ).limit (limit ). all ( )
48+ cnt , query = query .count (), query .offset (offset ).limit (limit )
4149 else :
42- cnt , res = res .count (), res .offset (offset ).all ()
43- for row in res :
44- row .avatar_link = row .avatar .link if row .avatar else None
45- result = [LecturerGet .from_orm (row ) for row in res ]
50+ cnt , query = query .count (), query .offset (offset )
51+ query = query .all ()
52+ logger .debug (query )
53+
54+ result = []
55+ for row in query :
56+ row_get = LecturerGet .from_orm (row )
57+ if row .avatar :
58+ row_get .avatar_link = get_photo_webpath (row .avatar .link )
59+ result .append (row_get )
4660 return {
4761 "items" : result ,
4862 "limit" : limit ,
@@ -71,7 +85,7 @@ async def patch_lecturer(
7185 if photo .lecturer_id != id or photo .approve_status != ApproveStatuses .APPROVED :
7286 raise ObjectNotFound (DbPhoto , lecturer_inp .avatar_id )
7387 lecturer_upd = Lecturer .update (
74- id , session = db .session , ** lecturer_inp .dict (exclude_unset = True ), avatar_link = photo .link
88+ id , session = db .session , ** lecturer_inp .dict (exclude_unset = True ), avatar_link = get_photo_webpath ( photo .link )
7589 )
7690 else :
7791 lecturer_upd = Lecturer .update (id , session = db .session , ** lecturer_inp .dict (exclude_unset = True ))
0 commit comments