From 56772a1726324080a135aeff66322a287c101b64 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Tue, 5 Mar 2024 13:44:37 +0100 Subject: [PATCH] Use fastapi status --- backend/routes/projects.py | 8 ++++---- backend/routes/subjects.py | 10 +++++----- backend/routes/users.py | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/backend/routes/projects.py b/backend/routes/projects.py index 88000fe3..e73b6574 100644 --- a/backend/routes/projects.py +++ b/backend/routes/projects.py @@ -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 @@ -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 @@ -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 diff --git a/backend/routes/subjects.py b/backend/routes/subjects.py index a48bf7c8..db602a81 100644 --- a/backend/routes/subjects.py +++ b/backend/routes/subjects.py @@ -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 @@ -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}") @@ -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") @@ -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 diff --git a/backend/routes/users.py b/backend/routes/users.py index 2c243a76..0c08846e 100644 --- a/backend/routes/users.py +++ b/backend/routes/users.py @@ -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 @@ -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] @@ -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())