2022-10-27 16:59:12 +02:00
|
|
|
from apiflask import APIBlueprint
|
|
|
|
from flask import abort
|
|
|
|
|
2023-01-14 18:09:04 +01:00
|
|
|
from ...base.mode import EnrollmentsMode
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...base.schemas import MessageSchema
|
2022-10-27 16:59:12 +02:00
|
|
|
from ...dependencies import db
|
2022-11-12 16:18:07 +01:00
|
|
|
from ...examination_schedule.models import ExaminationSchedule
|
2022-10-27 16:59:12 +02:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
2023-01-14 17:38:03 +01:00
|
|
|
from ..models import Group, Student, TermOfDefence
|
|
|
|
from ..schemas import (
|
|
|
|
ExaminationScheduleListSchema,
|
|
|
|
TemporaryStudentSchema,
|
|
|
|
TermOfDefenceStudentListSchema,
|
|
|
|
)
|
2022-10-27 16:59:12 +02:00
|
|
|
|
|
|
|
bp = APIBlueprint("enrollments", __name__, url_prefix="/")
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post("/<int:examination_schedule_id>/enrollments/<int:term_of_defence_id>/")
|
2022-10-27 16:59:12 +02:00
|
|
|
@bp.input(TemporaryStudentSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def assign_your_group_to_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int, data: dict
|
|
|
|
) -> dict:
|
2022-10-27 16:59:12 +02:00
|
|
|
# this code will be removed
|
2023-01-14 17:38:03 +01:00
|
|
|
student = Student.query.filter(Student.id == data.get("student_id")).first()
|
2022-10-27 16:59:12 +02:00
|
|
|
if student is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-10-27 16:59:12 +02:00
|
|
|
################
|
2023-01-14 17:38:03 +01:00
|
|
|
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:
|
2022-11-16 20:42:40 +01:00
|
|
|
abort(400, "Term of defence not found!")
|
|
|
|
|
2023-01-14 18:09:04 +01:00
|
|
|
if ex.open_enrollments != EnrollmentsMode.OPEN.value:
|
|
|
|
abort(400, "Enrollments is closed! You have been delayed!")
|
|
|
|
|
|
|
|
group = (
|
2023-01-14 17:38:03 +01:00
|
|
|
Group.query.join(ProjectSupervisor)
|
|
|
|
.filter(Group.year_group_id == ex.year_group_id)
|
|
|
|
.join(Group.students)
|
|
|
|
.filter_by(index=student.index)
|
|
|
|
.first()
|
|
|
|
)
|
2023-01-14 18:09:04 +01:00
|
|
|
if group is None or group.project_supervisor is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(
|
|
|
|
400,
|
|
|
|
"You don't have a group or your group doesn't"
|
|
|
|
" have an assigned project supervisor!",
|
|
|
|
)
|
|
|
|
|
|
|
|
defence = TermOfDefence.query.filter(
|
2023-01-14 18:09:04 +01:00
|
|
|
TermOfDefence.group_id == group.id,
|
2023-01-14 17:38:03 +01:00
|
|
|
TermOfDefence.examination_schedule_id == examination_schedule_id,
|
|
|
|
).first()
|
2022-11-12 16:18:07 +01:00
|
|
|
if defence is not None:
|
2022-10-27 19:53:39 +02:00
|
|
|
abort(400, "Your group has already assigned to any exam date!")
|
2022-10-27 16:59:12 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
td = (
|
|
|
|
TermOfDefence.query.join(TermOfDefence.members_of_committee)
|
2023-01-14 18:09:04 +01:00
|
|
|
.filter_by(id=group.project_supervisor_id)
|
2023-01-14 17:38:03 +01:00
|
|
|
.first()
|
|
|
|
)
|
2022-10-27 16:59:12 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
if td is None:
|
2022-10-27 16:59:12 +02:00
|
|
|
abort(400, "Your project supervisor is not in committee!")
|
|
|
|
|
2023-01-14 18:09:04 +01:00
|
|
|
term_of_defence.group_id = group.id
|
2022-11-16 20:42:40 +01:00
|
|
|
db.session.add(term_of_defence)
|
2022-10-27 16:59:12 +02:00
|
|
|
db.session.commit()
|
|
|
|
return {"message": "You have just assigned the group for this exam date!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.delete("/<int:examination_schedule_id>/enrollments/<int:term_of_defence_id>/")
|
2022-10-27 16:59:12 +02:00
|
|
|
@bp.input(TemporaryStudentSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def delete_your_group_from_term_of_defence(
|
|
|
|
examination_schedule_id: int, term_of_defence_id: int, data: dict
|
|
|
|
) -> dict:
|
2022-10-27 16:59:12 +02:00
|
|
|
# this code will be removed
|
2023-01-14 17:38:03 +01:00
|
|
|
student = Student.query.filter(Student.id == data.get("student_id")).first()
|
2022-10-27 16:59:12 +02:00
|
|
|
if student is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-10-27 16:59:12 +02:00
|
|
|
################
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
term_of_defence = (
|
|
|
|
TermOfDefence.query.join(ExaminationSchedule)
|
|
|
|
.filter(TermOfDefence.id == term_of_defence_id)
|
|
|
|
.filter(TermOfDefence.examination_schedule_id == examination_schedule_id)
|
|
|
|
.first()
|
|
|
|
)
|
2022-10-27 16:59:12 +02:00
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
ex = term_of_defence.examination_schedule
|
2022-11-12 16:18:07 +01:00
|
|
|
if term_of_defence is None:
|
|
|
|
abort(404, "Term of defence doesn't exist!")
|
2022-10-27 16:59:12 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
group = (
|
|
|
|
Group.query.filter(Group.year_group_id == ex.year_group_id)
|
|
|
|
.join(Group.students)
|
|
|
|
.filter_by(index=student.index)
|
|
|
|
.first()
|
|
|
|
)
|
2022-11-17 10:21:36 +01:00
|
|
|
if group.id != term_of_defence.group_id:
|
2022-11-12 16:18:07 +01:00
|
|
|
abort(400, "You are not assigned to this group!")
|
2022-10-27 16:59:12 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
term_of_defence.group_id = None
|
|
|
|
db.session.add(term_of_defence)
|
2022-10-27 16:59:12 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {"message": "You have just removed the group for this exam date!"}
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/examination-schedule/year-group/<int:year_group_id>/")
|
|
|
|
@bp.input(TemporaryStudentSchema, location="query")
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.output(ExaminationScheduleListSchema)
|
2022-11-16 20:42:40 +01:00
|
|
|
def list_examination_schedule(year_group_id: int, data: dict) -> dict:
|
2022-11-12 16:18:07 +01:00
|
|
|
# this code will be removed
|
2023-01-14 17:38:03 +01:00
|
|
|
student = Student.query.filter(Student.id == data.get("student_id")).first()
|
2022-11-12 16:18:07 +01:00
|
|
|
if student is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-11-12 16:18:07 +01:00
|
|
|
################
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
examination_schedules = ExaminationSchedule.query.filter(
|
|
|
|
ExaminationSchedule.year_group_id == year_group_id
|
|
|
|
).all()
|
|
|
|
return {"examination_schedules": examination_schedules}
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/examination-schedule/<int:examination_schedule_id>/enrollments/")
|
|
|
|
@bp.input(TemporaryStudentSchema, location="query")
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.output(TermOfDefenceStudentListSchema)
|
|
|
|
def list_term_of_defences(examination_schedule_id: int, data: dict) -> dict:
|
2022-11-12 16:18:07 +01:00
|
|
|
# this code will be removed
|
2023-01-14 17:38:03 +01:00
|
|
|
student = Student.query.filter(Student.id == data.get("student_id")).first()
|
2022-11-12 16:18:07 +01:00
|
|
|
if student is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found student!")
|
2022-11-12 16:18:07 +01:00
|
|
|
################
|
2023-01-14 17:38:03 +01:00
|
|
|
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}
|