From 294ce1d5c0409796f3dcf5f387ac95c9a5f02ee3 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Tue, 12 Mar 2024 10:43:24 +0100 Subject: [PATCH] Add route for creating group --- backend/domain/logic/project.py | 11 ++++++++++- .../routes/dependencies/role_dependencies.py | 17 ++++++++++++++--- backend/routes/project.py | 15 +++++++++++++-- backend/routes/subject.py | 4 ++-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/backend/domain/logic/project.py b/backend/domain/logic/project.py index 36e1f5fa..2e3b6967 100644 --- a/backend/domain/logic/project.py +++ b/backend/domain/logic/project.py @@ -2,7 +2,7 @@ from sqlalchemy.orm import Session -from db.models.models import Project, Student, Subject +from db.models.models import Project, Student, Subject, Teacher from domain.logic.basic_operations import get, get_all from domain.models.ProjectDataclass import ProjectDataclass @@ -57,3 +57,12 @@ def get_projects_of_student(session: Session, user_id: int) -> list[ProjectDatac for i in subjects: projects += i.projects return [project.to_domain_model() for project in projects] + + +def get_projects_of_teacher(session: Session, user_id: int) -> list[ProjectDataclass]: + teacher = get(session, Teacher, ident=user_id) + subjects = teacher.subjects + projects = [] + for i in subjects: + projects += i.projects + return [project.to_domain_model() for project in projects] diff --git a/backend/routes/dependencies/role_dependencies.py b/backend/routes/dependencies/role_dependencies.py index 785af22b..78ddfbd5 100644 --- a/backend/routes/dependencies/role_dependencies.py +++ b/backend/routes/dependencies/role_dependencies.py @@ -3,7 +3,7 @@ from db.sessions import get_session from domain.logic.admin import get_admin, is_user_admin -from domain.logic.project import get_project +from domain.logic.project import get_project, get_projects_of_teacher from domain.logic.student import get_student, is_user_student from domain.logic.subject import get_subjects_of_student, get_subjects_of_teacher, is_user_authorized_for_subject from domain.logic.teacher import get_teacher, is_user_teacher @@ -62,7 +62,7 @@ def ensure_user_authorized_for_project( raise NoAccessToSubjectError -def get_authenticated_student_for_subject( +def ensure_student_authorized_for_subject( subject_id: int, session: Session = Depends(get_session), student: StudentDataclass = Depends(get_authenticated_student), @@ -73,7 +73,7 @@ def get_authenticated_student_for_subject( return student -def get_authenticated_teacher_for_subject( +def ensure_teacher_authorized_for_subject( subject_id: int, session: Session = Depends(get_session), teacher: TeacherDataclass = Depends(get_authenticated_teacher), @@ -82,3 +82,14 @@ def get_authenticated_teacher_for_subject( if subject_id not in [subject.id for subject in subjects_of_teacher]: raise NoAccessToSubjectError return teacher + + +def ensure_teacher_authorized_for_project( + project_id: int, + session: Session = Depends(get_session), + teacher: TeacherDataclass = Depends(get_authenticated_teacher), +) -> TeacherDataclass: + projects_of_teacher = get_projects_of_teacher(session, teacher.id) + if project_id not in [project.id for project in projects_of_teacher]: + raise NoAccessToSubjectError + return teacher diff --git a/backend/routes/project.py b/backend/routes/project.py index e4e84d65..79e58d2f 100644 --- a/backend/routes/project.py +++ b/backend/routes/project.py @@ -2,11 +2,14 @@ from sqlalchemy.orm import Session from db.sessions import get_session -from domain.logic.group import get_groups_of_project +from domain.logic.group import create_group, get_groups_of_project from domain.logic.project import get_project from domain.models.GroupDataclass import GroupDataclass from domain.models.ProjectDataclass import ProjectDataclass -from routes.dependencies.role_dependencies import ensure_user_authorized_for_project +from routes.dependencies.role_dependencies import ( + ensure_teacher_authorized_for_project, + ensure_user_authorized_for_project, +) project_router = APIRouter() @@ -26,3 +29,11 @@ def project_get_groups( session: Session = Depends(get_session), ) -> list[GroupDataclass]: return get_groups_of_project(session, project_id) + + +@project_router.post("/projects/{project_id}/groups", dependencies=[Depends(ensure_teacher_authorized_for_project)]) +def project_create_group( + project_id: int, + session: Session = Depends(get_session), +) -> GroupDataclass: + return create_group(session, project_id) diff --git a/backend/routes/subject.py b/backend/routes/subject.py index 525e893c..41857dc1 100644 --- a/backend/routes/subject.py +++ b/backend/routes/subject.py @@ -7,8 +7,8 @@ from domain.models.ProjectDataclass import ProjectDataclass, ProjectInput from domain.models.SubjectDataclass import SubjectDataclass from routes.dependencies.role_dependencies import ( + ensure_teacher_authorized_for_subject, ensure_user_authorized_for_subject, - get_authenticated_teacher_for_subject, get_authenticated_user, ) @@ -25,7 +25,7 @@ def get_subject_projects(subject_id: int, session: Session = Depends(get_session return get_projects_of_subject(session, subject_id) -@subject_router.post("/subjects/{subject_id}/projects", dependencies=[Depends(get_authenticated_teacher_for_subject)]) +@subject_router.post("/subjects/{subject_id}/projects", dependencies=[Depends(ensure_teacher_authorized_for_subject)]) def new_project( subject_id: int, project: ProjectInput,