Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Use fastapi status
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 5, 2024
1 parent 4319bce commit 56772a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions backend/routes/projects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, status

from db.errors.database_errors import ItemNotFoundError
from domain.logic.SubjectLogic import is_user_authorized_for_subject
Expand All @@ -23,7 +23,7 @@ def get_subjects(teacher: bool = False) -> list[ProjectDataclass]:
for i in subjects:
projects += project_dao.get_projects_of_subject(i.id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err
return projects


Expand All @@ -35,8 +35,8 @@ def get_project(project_id: int) -> ProjectDataclass:
try:
project = project_dao.get(project_id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err
subject = subject_dao.get(project.subject_id)
if not is_user_authorized_for_subject(subject, user, get_dao_provider()):
raise HTTPException(status_code=403)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
return project
10 changes: 5 additions & 5 deletions backend/routes/subjects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, status

from db.errors.database_errors import ItemNotFoundError
from domain.logic.SubjectLogic import is_user_authorized_for_subject
Expand All @@ -19,7 +19,7 @@ def get_subjects(teacher: bool = False) -> list[SubjectDataclass]:
return subject_dao.get_subjects_of_teacher(user.id)
return subject_dao.get_subjects_of_student(user.id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err


@subjects_router.get("/subjects/{subject_id}")
Expand All @@ -28,7 +28,7 @@ def get_subject(subject_id: int) -> SubjectDataclass:
try:
return subject_dao.get(subject_id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err


@subjects_router.get("/subjects/{subject_id}/projects")
Expand All @@ -38,7 +38,7 @@ def get_subject_projects(subject_id: int) -> list[ProjectDataclass]:
try:
subject = subject_dao.get(subject_id)
if not is_user_authorized_for_subject(subject, get_authenticated_user(), get_dao_provider()):
raise HTTPException(status_code=403)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
return project_dao.get_projects_of_subject(subject_id)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err
8 changes: 4 additions & 4 deletions backend/routes/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, status

from db.errors.database_errors import ItemNotFoundError
from domain.logic.UserLogic import convert_user
Expand All @@ -18,7 +18,7 @@ def get_current_user() -> APIUser:
@users_router.get("/users")
def get_users() -> list[APIUser]:
if not is_user_admin():
raise HTTPException(status_code=403)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
user_dao = get_dao_provider().get_user_dao()
users = user_dao.get_all()
return [convert_user(user, get_dao_provider()) for user in users]
Expand All @@ -27,10 +27,10 @@ def get_users() -> list[APIUser]:
@users_router.get("/users/{uid}")
def get_user(uid: int) -> APIUser:
if not is_user_admin():
raise HTTPException(status_code=403)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
user_dao = get_dao_provider().get_user_dao()
try:
user = user_dao.get(uid)
except ItemNotFoundError as err:
raise HTTPException(status_code=404) from err
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) from err
return convert_user(user, get_dao_provider())

0 comments on commit 56772a1

Please sign in to comment.