11import os
2- from typing import Optional
32
43from fastapi import APIRouter , HTTPException , JSONResponse , Request , Response
54
6- import auth .crud
75import database
86import exambank .crud
7+ from auth .utils import logged_in_or_raise
98from exambank .watermark import apply_watermark , create_watermark , raster_pdf
109from permission .types import ExamBankAccess
1110from utils import path_in_dir
1716 tags = ["exam-bank" ],
1817)
1918
20- # TODO: update endpoints to use crud functions
19+ # TODO: update endpoints to use crud functions -> don't use crud actually; refactor to do that later
2120
2221@router .get (
2322 "/list/exams"
@@ -34,17 +33,6 @@ async def all_exams(
3433 exam_list = exambank .crud .all_exams (db_session , course_id_starts_with )
3534 return JSONResponse ([exam .serializable_dict () for exam in exam_list ])
3635
37- @router .get (
38- "/list/courses"
39- )
40- async def all_courses (
41- _request : Request ,
42- _db_session : database .DBSession ,
43- ):
44- # TODO: replace this with a table eventually
45- courses = [f .name for f in os .scandir (f"{ EXAM_BANK_DIR } " ) if f .is_dir ()]
46- return JSONResponse (courses )
47-
4836@router .get (
4937 "/get/{exam_id}"
5038)
@@ -53,17 +41,8 @@ async def get_exam(
5341 db_session : database .DBSession ,
5442 exam_id : int ,
5543):
56- session_id = request .cookies .get ("session_id" , None )
57- if session_id is None :
58- raise HTTPException (status_code = 401 )
59-
60- computing_id = await auth .crud .get_computing_id (db_session , session_id )
61- if computing_id is None :
62- raise HTTPException (status_code = 401 )
63-
64- # TODO: clean this checking into one function & one computing_id check
65- if not await ExamBankAccess .has_permission (request ):
66- raise HTTPException (status_code = 401 , detail = "user must have exam bank access permission" )
44+ _ , session_computing_id = await logged_in_or_raise (request , db_session )
45+ await ExamBankAccess .has_permission_or_raise (request , errmsg = "user must have exam bank access permission" )
6746
6847 # number exams with an exam_id pkey
6948 # TODO: store resource locations in a db table & simply look them up
@@ -77,10 +56,9 @@ async def get_exam(
7756 raise HTTPException (status_code = 500 , detail = "Found dangerous pdf path, exiting" )
7857
7958 # TODO: test this works nicely
80- watermark = create_watermark (computing_id , 20 )
59+ watermark = create_watermark (session_computing_id , 20 )
8160 watermarked_pdf = apply_watermark (exam_path , watermark )
8261 image_bytes = raster_pdf (watermarked_pdf )
8362
84- headers = { "Content-Disposition" : f'inline; filename="{ meta .course_id } _{ exam_id } _{ computing_id } .pdf"' }
63+ headers = { "Content-Disposition" : f'inline; filename="{ meta .course_id } _{ exam_id } _{ session_computing_id } .pdf"' }
8564 return Response (content = image_bytes , headers = headers , media_type = "application/pdf" )
86-
0 commit comments