from apiflask import APIBlueprint from flask import abort from ...base.mode import EnrollmentsMode from ...base.schemas import MessageSchema from ...dependencies import db from ...examination_schedule.models import ExaminationSchedule from ...project_supervisor.models import ProjectSupervisor from ..models import Group, Student, TermOfDefence from ..schemas import ( ExaminationScheduleListSchema, TemporaryStudentSchema, TermOfDefenceStudentListSchema, ) bp = APIBlueprint("enrollments", __name__, url_prefix="/") @bp.post("//enrollments//") @bp.input(TemporaryStudentSchema) @bp.output(MessageSchema) def assign_your_group_to_term_of_defence( examination_schedule_id: int, term_of_defence_id: int, data: dict ) -> dict: # this code will be removed student = Student.query.filter(Student.id == data.get("student_id")).first() if student is None: abort(404, "Not found student!") ################ term_of_defence = ( TermOfDefence.query.filter( TermOfDefence.id == term_of_defence_id, TermOfDefence.examination_schedule_id == examination_schedule_id, ) .join(ExaminationSchedule) .first() ) if term_of_defence is None or (ex := term_of_defence.examination_schedule) is None: abort(400, "Term of defence not found!") if ex.open_enrollments != EnrollmentsMode.OPEN.value: abort(400, "Enrollments is closed! You have been delayed!") group = ( Group.query.join(ProjectSupervisor) .filter(Group.year_group_id == ex.year_group_id) .join(Group.students) .filter_by(index=student.index) .first() ) if group is None or group.project_supervisor is None: abort( 400, "You don't have a group or your group doesn't" " have an assigned project supervisor!", ) defence = TermOfDefence.query.filter( TermOfDefence.group_id == group.id, TermOfDefence.examination_schedule_id == examination_schedule_id, ).first() if defence is not None: abort(400, "Your group has already assigned to any exam date!") td = ( TermOfDefence.query.join(TermOfDefence.members_of_committee) .filter_by(id=group.project_supervisor_id) .first() ) if td is None: abort(400, "Your project supervisor is not in committee!") term_of_defence.group_id = group.id db.session.add(term_of_defence) 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_your_group_from_term_of_defence( examination_schedule_id: int, term_of_defence_id: int, data: dict ) -> dict: # this code will be removed student = Student.query.filter(Student.id == data.get("student_id")).first() if student is None: abort(404, "Not found student!") ################ term_of_defence = ( TermOfDefence.query.join(ExaminationSchedule) .filter(TermOfDefence.id == term_of_defence_id) .filter(TermOfDefence.examination_schedule_id == examination_schedule_id) .first() ) ex = term_of_defence.examination_schedule if term_of_defence is None: abort(404, "Term of defence doesn't exist!") group = ( Group.query.filter(Group.year_group_id == ex.year_group_id) .join(Group.students) .filter_by(index=student.index) .first() ) if group.id != term_of_defence.group_id: abort(400, "You are not assigned to this group!") term_of_defence.group_id = None db.session.add(term_of_defence) db.session.commit() return {"message": "You have just removed the group for this exam date!"} @bp.get("/examination-schedule/year-group//") @bp.input(TemporaryStudentSchema, location="query") @bp.output(ExaminationScheduleListSchema) def list_examination_schedule(year_group_id: int, data: dict) -> dict: # this code will be removed student = Student.query.filter(Student.id == data.get("student_id")).first() if student is None: abort(404, "Not found student!") ################ examination_schedules = ExaminationSchedule.query.filter( ExaminationSchedule.year_group_id == year_group_id ).all() return {"examination_schedules": examination_schedules} @bp.get("/examination-schedule//enrollments/") @bp.input(TemporaryStudentSchema, location="query") @bp.output(TermOfDefenceStudentListSchema) def list_term_of_defences(examination_schedule_id: int, data: dict) -> dict: # this code will be removed student = Student.query.filter(Student.id == data.get("student_id")).first() if student is None: abort(404, "Not found student!") ################ term_of_defences = ( TermOfDefence.query.filter( TermOfDefence.examination_schedule_id == examination_schedule_id ) .join(ExaminationSchedule, isouter=True) .all() ) term_of_defences = list( filter( lambda n: len( [ d.id for d in n.members_of_committee if d.id == student.groups[0].project_supervisor.id ] ) > 0, term_of_defences, ) ) return {"term_of_defences": term_of_defences}