system-pri/backend/app/students/routes/enrollments.py

70 lines
2.9 KiB
Python

import datetime
from apiflask import APIBlueprint
from flask import abort
from ..schemas import MessageSchema, TemporaryStudentSchema
from ...dependencies import db
from ...examination_schedule.models import Enrollment
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('/<int:examination_schedule_id>/enrollments/<int:enrollment_id>/')
@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!")
################
st = Student.query.join(Group).join(ProjectSupervisor).filter(Student.index == student.index).first()
enrollment = db.session.query(Enrollment.id).filter(Enrollment.group_id == st.group.id).first()
if enrollment is not None:
abort(400, "Your group has already assigned to any exam date!")
enrollment = get_enrollment_by_enrollment_and_examination_schedule_ids(examination_schedule_id, enrollment_id)
check_the_enrollments_has_just_started(enrollment.examination_schedule)
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('/<int:examination_schedule_id>/enrollments/<int:enrollment_id>/')
@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!"}