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 ...dependencies import db from ..models import ProjectSupervisor from ..query import get_enrollment_by_enrollment_and_examination_schedule_ids, \ check_the_project_supervisor_is_in_committee, check_the_enrollments_has_just_started bp = APIBlueprint("enrollments", __name__, url_prefix="/") @bp.post('//enrollments//') @bp.input(CommitteeCreateSchema) @bp.output(MessageSchema) def assign_yourself_to_committee(examination_schedule_id: int, enrollment_id: int, data: dict) -> dict: # this code will be removed project_supervisor = db.session.query(ProjectSupervisor).filter( ProjectSupervisor.id == data['project_supervisor_id']).first() if project_supervisor is None: abort(404, "ProjectSupervisor doesn't exist!") ################ limit_of_committtee = current_app.config['LIMIT_PERSONS_PER_COMMITTEE'] enrollment = get_enrollment_by_enrollment_and_examination_schedule_ids(examination_schedule_id, enrollment_id) check_the_enrollments_has_just_started(enrollment.examination_schedule.start_date, "assign") size_of_committee = db.session.query(Committee).join(Committee.members). \ filter(Committee.enrollment_id == enrollment.id).count() if size_of_committee >= limit_of_committtee: abort(400, "The committee is full!") if check_the_project_supervisor_is_in_committee(enrollment.id, project_supervisor.id): abort(400, "You have already in this committee!") enrollment.committee.members.append(project_supervisor) db.session.add(enrollment) db.session.commit() return {"message": "You have just assigned yourself to committee!"} @bp.delete('//enrollments//') @bp.input(TemporaryProjectSupervisorSchema) @bp.output(MessageSchema) def delete_yourself_from_committee(examination_schedule_id: int, enrollment_id: int, data: dict) -> dict: # this code will be removed project_supervisor = db.session.query(ProjectSupervisor).filter(ProjectSupervisor.id == data['id']).first() if project_supervisor is None: abort(404, "ProjectSupervisor 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.start_date, "delete") if not check_the_project_supervisor_is_in_committee(enrollment.id, project_supervisor.id): abort(400, "You are not assigned to this committee!") enrollment.committee.members.remove(project_supervisor) db.session.commit() return {"message": "You have just removed from committee!"}