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

Commit

Permalink
Add route for creating group
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 12, 2024
1 parent 8c6cdc1 commit 294ce1d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
11 changes: 10 additions & 1 deletion backend/domain/logic/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
17 changes: 14 additions & 3 deletions backend/routes/dependencies/role_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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
15 changes: 13 additions & 2 deletions backend/routes/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
4 changes: 2 additions & 2 deletions backend/routes/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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,
Expand Down

0 comments on commit 294ce1d

Please sign in to comment.