From 12e15043e39c163a6d12fb1e13a1ca5541f2f59c Mon Sep 17 00:00:00 2001 From: dominik24c Date: Thu, 27 Oct 2022 16:59:12 +0200 Subject: [PATCH] add the functionality for students to assign and remove to selected exam date --- .../project_supervisor/routes/enrollments.py | 4 +- backend/app/students/query.py | 10 +++ backend/app/students/routes/__init__.py | 4 +- backend/app/students/routes/enrollments.py | 64 +++++++++++++++++++ backend/app/students/schemas.py | 17 +++-- 5 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 backend/app/students/query.py create mode 100644 backend/app/students/routes/enrollments.py diff --git a/backend/app/project_supervisor/routes/enrollments.py b/backend/app/project_supervisor/routes/enrollments.py index a054fa1..9ea7d8c 100644 --- a/backend/app/project_supervisor/routes/enrollments.py +++ b/backend/app/project_supervisor/routes/enrollments.py @@ -1,10 +1,8 @@ -import datetime - from apiflask import APIBlueprint from flask import abort, current_app from ..schemas import MessageSchema, CommitteeCreateSchema, TemporaryProjectSupervisorSchema -from ...examination_schedule.models import Enrollment, Committee, ExaminationSchedule +from ...examination_schedule.models import Committee from ...dependencies import db from ..models import ProjectSupervisor from ..query import get_enrollment_by_enrollment_and_examination_schedule_ids, \ diff --git a/backend/app/students/query.py b/backend/app/students/query.py new file mode 100644 index 0000000..a0f736d --- /dev/null +++ b/backend/app/students/query.py @@ -0,0 +1,10 @@ +import datetime + +from flask import abort +from ..examination_schedule.models import ExaminationSchedule + + +def check_the_enrollments_has_just_started(es: ExaminationSchedule) -> None: + now = datetime.datetime.utcnow() + if es.start_date is None or es.end_date is None or not (es.start_date < now < es.end_date): + abort(403, "Forbidden! Enrollment hasn't started yet.") diff --git a/backend/app/students/routes/__init__.py b/backend/app/students/routes/__init__.py index b3098d7..e34d84d 100644 --- a/backend/app/students/routes/__init__.py +++ b/backend/app/students/routes/__init__.py @@ -1,7 +1,9 @@ from flask import Blueprint +from .enrollments import bp as enrollments_bp from .registrations import bp as registrations_bp bp = Blueprint("students", __name__, url_prefix="/students") -bp.register_blueprint(registrations_bp) \ No newline at end of file +bp.register_blueprint(registrations_bp) +bp.register_blueprint(enrollments_bp) diff --git a/backend/app/students/routes/enrollments.py b/backend/app/students/routes/enrollments.py new file mode 100644 index 0000000..2b6fe9c --- /dev/null +++ b/backend/app/students/routes/enrollments.py @@ -0,0 +1,64 @@ +import datetime + +from apiflask import APIBlueprint +from flask import abort + +from ..schemas import MessageSchema, TemporaryStudentSchema +from ...dependencies import db +from ..models import Student, Group +from ...project_supervisor.models import ProjectSupervisor +from ...project_supervisor.query import get_enrollment_by_enrollment_and_examination_schedule_ids, \ + check_the_project_supervisor_is_in_committee +from ..query import check_the_enrollments_has_just_started + +bp = APIBlueprint("enrollments", __name__, url_prefix="/") + + +@bp.post('//enrollments//') +@bp.input(TemporaryStudentSchema) +@bp.output(MessageSchema) +def assign_group_for_this_exam_date(examination_schedule_id: int, enrollment_id: int, data: dict) -> dict: + # this code will be removed + student = Student.query.filter(Student.index == data['student_index']).first() + if student is None: + abort(404, "Student doesn't exist!") + ################ + + enrollment = get_enrollment_by_enrollment_and_examination_schedule_ids(examination_schedule_id, enrollment_id) + check_the_enrollments_has_just_started(enrollment.examination_schedule) + + st = Student.query.join(Group).join(ProjectSupervisor).filter(Student.index == student.index).first() + if st is None or st.group.project_supervisor is None: + abort(400, "You don't have a group or your group doesn't have an assigned project supervisor!") + + if not check_the_project_supervisor_is_in_committee(enrollment_id, st.group.project_supervisor.id): + abort(400, "Your project supervisor is not in committee!") + + enrollment.group_id = st.group.id + db.session.add(enrollment) + db.session.commit() + return {"message": "You have just assigned the group for this exam date!"} + + +@bp.delete('//enrollments//') +@bp.input(TemporaryStudentSchema) +@bp.output(MessageSchema) +def delete_group_for_this_exam_date(examination_schedule_id: int, enrollment_id: int, data: dict) -> dict: + # this code will be removed + student = Student.query.filter(Student.index == data['student_index']).first() + if student is None: + abort(404, "Student doesn't exist!") + ################ + + enrollment = get_enrollment_by_enrollment_and_examination_schedule_ids(examination_schedule_id, enrollment_id) + + if student.group.id != enrollment.group_id: + abort(400, "You are not assigned to this committee!") + + check_the_enrollments_has_just_started(enrollment.examination_schedule) + + enrollment.group = None + db.session.add(enrollment) + db.session.commit() + + return {"message": "You have just removed the group for this exam date!"} diff --git a/backend/app/students/schemas.py b/backend/app/students/schemas.py index bbbe2ae..768ca22 100644 --- a/backend/app/students/schemas.py +++ b/backend/app/students/schemas.py @@ -1,8 +1,7 @@ -from ..dependencies import ma -from marshmallow import fields +from marshmallow import fields, Schema -class ProjectSupervisorSchema(ma.Schema): +class ProjectSupervisorSchema(Schema): first_name = fields.Str() last_name = fields.Str() email = fields.Str() @@ -10,12 +9,20 @@ class ProjectSupervisorSchema(ma.Schema): available_groups = fields.Integer() -class ProjectSupervisorPaginationSchema(ma.Schema): +class ProjectSupervisorPaginationSchema(Schema): project_supervisors = fields.List(fields.Nested(ProjectSupervisorSchema)) max_pages = fields.Integer() -class ProjectSupervisorQuerySchema(ma.Schema): +class ProjectSupervisorQuerySchema(Schema): page = fields.Integer() per_page = fields.Integer() mode = fields.Boolean() + + +class TemporaryStudentSchema(Schema): + student_index = fields.Integer(required=True) + + +class MessageSchema(Schema): + message = fields.Str()