2022-10-26 15:11:50 +02:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from apiflask import APIBlueprint
|
2023-01-14 17:38:03 +01:00
|
|
|
from flask import abort
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
from ...base.schemas import MessageSchema
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...dependencies import db
|
|
|
|
from ...examination_schedule.models import TemporaryAvailability, TermOfDefence
|
|
|
|
from ..query.enrollments import (
|
|
|
|
check_the_term_of_defence_not_exists_in_chosen_date_range,
|
|
|
|
get_and_check_the_project_supervisors_exists_in_db,
|
|
|
|
get_examination_schedule_by_id,
|
|
|
|
get_term_of_defence_by_id_and_examination_schedule_id,
|
|
|
|
set_new_group_to_term_of_defence,
|
|
|
|
validate_enrollments_date,
|
|
|
|
)
|
|
|
|
from ..schemas.enrollments import (
|
|
|
|
AssignedGroupToTermOfDefenceListSchema,
|
|
|
|
TemporaryAvailabilityListSchema,
|
|
|
|
TermOfDefenceSchema,
|
|
|
|
)
|
|
|
|
from ..schemas.groups import GroupIdSchema
|
2022-11-17 18:39:45 +01:00
|
|
|
from ..utils import generate_range_dates
|
2022-10-26 15:11:50 +02:00
|
|
|
|
|
|
|
bp = APIBlueprint("enrollments", __name__, url_prefix="/enrollments")
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post("/<int:examination_schedule_id>/add")
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.input(TermOfDefenceSchema)
|
2022-10-26 15:11:50 +02:00
|
|
|
@bp.output(MessageSchema)
|
2022-11-16 20:42:40 +01:00
|
|
|
def create_term_of_defence(examination_schedule_id: int, data: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
chairman_of_committee_id = data.pop("chairman_of_committee")
|
|
|
|
project_supervisors_ids = data.pop("project_supervisors")
|
|
|
|
if chairman_of_committee_id not in project_supervisors_ids:
|
|
|
|
abort(400, "Invalid id of chairman committee!")
|
2022-11-16 20:42:40 +01:00
|
|
|
|
2022-11-19 20:43:05 +01:00
|
|
|
ex = get_examination_schedule_by_id(examination_schedule_id)
|
2022-10-26 15:11:50 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisors = get_and_check_the_project_supervisors_exists_in_db(
|
|
|
|
ex.year_group_id, project_supervisors_ids
|
|
|
|
)
|
|
|
|
validate_enrollments_date(ex.start_date, ex.end_date, ex.duration_time, data)
|
|
|
|
check_the_term_of_defence_not_exists_in_chosen_date_range(
|
|
|
|
examination_schedule_id, data
|
|
|
|
)
|
|
|
|
td = TermOfDefence(
|
|
|
|
**data,
|
|
|
|
examination_schedule_id=examination_schedule_id,
|
|
|
|
chairman_of_committee=chairman_of_committee_id
|
|
|
|
)
|
2022-11-16 20:42:40 +01:00
|
|
|
db.session.add(td)
|
|
|
|
db.session.commit()
|
|
|
|
for p in project_supervisors:
|
|
|
|
td.members_of_committee.append(p)
|
2022-10-26 15:11:50 +02:00
|
|
|
db.session.commit()
|
2022-11-16 20:42:40 +01:00
|
|
|
return {"message": "Term of defence was created!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post("/<int:examination_schedule_id>/add-term-of-defences/")
|
2023-01-06 22:35:47 +01:00
|
|
|
@bp.input(TermOfDefenceSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def create_many_term_of_defences(examination_schedule_id: int, data: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
chairman_of_committee_id = data.pop("chairman_of_committee")
|
|
|
|
project_supervisors_ids = data.pop("project_supervisors")
|
|
|
|
if chairman_of_committee_id not in project_supervisors_ids:
|
|
|
|
abort(400, "Invalid id of chairman committee!")
|
2023-01-06 22:35:47 +01:00
|
|
|
|
|
|
|
ex = get_examination_schedule_by_id(examination_schedule_id)
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisors = get_and_check_the_project_supervisors_exists_in_db(
|
|
|
|
ex.year_group_id, project_supervisors_ids
|
|
|
|
)
|
|
|
|
validate_enrollments_date(ex.start_date, ex.end_date, ex.duration_time, data)
|
|
|
|
check_the_term_of_defence_not_exists_in_chosen_date_range(
|
|
|
|
examination_schedule_id, data
|
|
|
|
)
|
2023-01-06 22:35:47 +01:00
|
|
|
|
|
|
|
# create many here
|
2023-01-14 17:38:03 +01:00
|
|
|
start_date = data.get("start_date")
|
|
|
|
end_date = data.get("end_date")
|
2023-01-06 22:35:47 +01:00
|
|
|
dates = generate_range_dates(start_date, end_date, ex.duration_time)
|
|
|
|
for start_date in dates:
|
|
|
|
end_date = start_date + datetime.timedelta(minutes=ex.duration_time)
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
td = TermOfDefence(
|
|
|
|
start_date=start_date,
|
|
|
|
end_date=end_date,
|
|
|
|
examination_schedule_id=examination_schedule_id,
|
|
|
|
chairman_of_committee=chairman_of_committee_id,
|
|
|
|
)
|
2023-01-06 22:35:47 +01:00
|
|
|
td.members_of_committee = project_supervisors
|
|
|
|
db.session.add(td)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Term of defences was created!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.put("/<int:examination_schedule_id>/update/<int:term_of_defence_id>/")
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.input(TermOfDefenceSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def update_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int, data: dict
|
|
|
|
) -> dict:
|
|
|
|
chairman_of_committee_id = data.pop("chairman_of_committee")
|
|
|
|
project_supervisors_ids = data.pop("project_supervisors")
|
|
|
|
if chairman_of_committee_id not in project_supervisors_ids:
|
|
|
|
abort(400, "Invalid id of chairman committee!")
|
|
|
|
|
|
|
|
td_query = TermOfDefence.query.filter(
|
|
|
|
TermOfDefence.id == term_of_defence_id,
|
|
|
|
TermOfDefence.examination_schedule_id == examination_schedule_id,
|
|
|
|
)
|
2022-11-16 20:42:40 +01:00
|
|
|
td = td_query.first()
|
|
|
|
if td is None:
|
|
|
|
abort(404, "Not found term of defence!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
ex = get_examination_schedule_by_id(examination_schedule_id)
|
|
|
|
|
|
|
|
project_supervisors = get_and_check_the_project_supervisors_exists_in_db(
|
|
|
|
ex.year_group_id, project_supervisors_ids
|
|
|
|
)
|
|
|
|
validate_enrollments_date(ex.start_date, ex.end_date, ex.duration_time, data)
|
|
|
|
check_the_term_of_defence_not_exists_in_chosen_date_range(
|
|
|
|
examination_schedule_id, data
|
|
|
|
)
|
2022-11-16 20:42:40 +01:00
|
|
|
|
|
|
|
td_query.update(data)
|
|
|
|
td.members_of_committee = []
|
2023-01-14 17:38:03 +01:00
|
|
|
td.chairman_of_committee = chairman_of_committee_id
|
2022-10-26 15:11:50 +02:00
|
|
|
db.session.commit()
|
2022-11-16 20:42:40 +01:00
|
|
|
for p in project_supervisors:
|
|
|
|
td.members_of_committee.append(p)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {"message": "Term of defence was updated!"}
|
2022-10-26 15:11:50 +02:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.delete("/<int:examination_schedule_id>/delete/<int:term_of_defence_id>/")
|
2022-10-26 15:11:50 +02:00
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def delete_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int
|
|
|
|
) -> dict:
|
|
|
|
td = get_term_of_defence_by_id_and_examination_schedule_id(
|
|
|
|
examination_schedule_id, term_of_defence_id
|
|
|
|
)
|
2022-11-16 20:42:40 +01:00
|
|
|
db.session.delete(td)
|
2022-10-26 15:11:50 +02:00
|
|
|
db.session.commit()
|
2022-11-16 20:42:40 +01:00
|
|
|
return {"message": "Term of defence was deleted!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/<int:examination_schedule_id>/term-of-defences/")
|
2023-01-06 17:25:21 +01:00
|
|
|
@bp.output(AssignedGroupToTermOfDefenceListSchema)
|
2022-11-16 20:42:40 +01:00
|
|
|
def list_of_term_of_defences(examination_schedule_id: int) -> dict:
|
2022-11-19 20:43:05 +01:00
|
|
|
get_examination_schedule_by_id(examination_schedule_id)
|
2022-11-16 20:42:40 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
td = (
|
|
|
|
TermOfDefence.query.join(TermOfDefence.members_of_committee, isouter=True)
|
|
|
|
.filter(TermOfDefence.examination_schedule_id == examination_schedule_id)
|
|
|
|
.all()
|
|
|
|
)
|
2022-11-16 20:42:40 +01:00
|
|
|
return {"term_of_defences": td}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/<int:examination_schedule_id>/temporary-availabilities/")
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.output(TemporaryAvailabilityListSchema)
|
|
|
|
def list_of_temporary_availability(examination_schedule_id: int) -> dict:
|
2022-11-19 20:43:05 +01:00
|
|
|
get_examination_schedule_by_id(examination_schedule_id)
|
2022-11-16 20:42:40 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
td = (
|
|
|
|
TemporaryAvailability.query.filter(
|
|
|
|
TemporaryAvailability.examination_schedule_id == examination_schedule_id
|
|
|
|
)
|
|
|
|
.join(TemporaryAvailability.project_supervisor)
|
|
|
|
.all()
|
|
|
|
)
|
2022-11-16 20:42:40 +01:00
|
|
|
return {"temporary_availabilities": td}
|
2022-11-19 20:43:05 +01:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/<int:examination_schedule_id>/assigned-group-to-term-of-defences/")
|
2022-11-19 20:43:05 +01:00
|
|
|
@bp.output(AssignedGroupToTermOfDefenceListSchema)
|
|
|
|
def list_of_assigned_group_to_term_of_defences(examination_schedule_id: int) -> dict:
|
|
|
|
get_examination_schedule_by_id(examination_schedule_id)
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
td = (
|
|
|
|
TermOfDefence.query.join(TermOfDefence.members_of_committee, isouter=True)
|
|
|
|
.join(TermOfDefence.group)
|
|
|
|
.filter(TermOfDefence.examination_schedule_id == examination_schedule_id)
|
|
|
|
.filter(TermOfDefence.group_id.isnot(None))
|
|
|
|
.all()
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
return {"term_of_defences": td}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post(
|
|
|
|
"/<int:examination_schedule_id>/term-of-defence/<int:term_of_defence_id>/group/"
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
@bp.input(GroupIdSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def add_group_to_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int, data: dict
|
|
|
|
) -> dict:
|
|
|
|
set_new_group_to_term_of_defence(
|
|
|
|
examination_schedule_id, term_of_defence_id, data.get("group_id")
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Group was added to term of defences!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.delete(
|
|
|
|
"/<int:examination_schedule_id>/term-of-defence/<int:term_of_defence_id>/group/"
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def delete_group_to_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int
|
|
|
|
) -> dict:
|
|
|
|
td = get_term_of_defence_by_id_and_examination_schedule_id(
|
|
|
|
examination_schedule_id, term_of_defence_id
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
td.group_id = None
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Group was deleted from term of defences!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.put(
|
|
|
|
"/<int:examination_schedule_id>/term-of-defence/<int:term_of_defence_id>/group/"
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
@bp.input(GroupIdSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def update_group_for_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int, data: dict
|
|
|
|
) -> dict:
|
|
|
|
set_new_group_to_term_of_defence(
|
|
|
|
examination_schedule_id, term_of_defence_id, data.get("group_id")
|
|
|
|
)
|
2022-11-19 20:43:05 +01:00
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Group for term of defence was updated!"}
|