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 675fd1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 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

0 comments on commit 675fd1d

Please sign in to comment.