2022-10-27 16:59:12 +02:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from apiflask import APIBlueprint
|
|
|
|
from flask import abort
|
|
|
|
|
2022-11-16 20:42:40 +01:00
|
|
|
from ..schemas import MessageSchema, TemporaryStudentSchema, ExaminationScheduleListSchema, \
|
|
|
|
TermOfDefenceStudentListSchema
|
2022-10-27 16:59:12 +02:00
|
|
|
from ...dependencies import db
|
2022-11-12 16:18:07 +01:00
|
|
|
from ..models import Student, Group, TermOfDefence
|
|
|
|
from ...examination_schedule.models import ExaminationSchedule
|
2022-10-27 16:59:12 +02:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
|
|
|
|
|
|
|
bp = APIBlueprint("enrollments", __name__, url_prefix="/")
|
|
|
|
|
|
|
|
|
2022-11-16 20:42:40 +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)
|
2022-11-16 20:42:40 +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
|
|
|
|
student = Student.query.filter(Student.index == data['student_index']).first()
|
|
|
|
if student is None:
|
|
|
|
abort(404, "Student doesn't exist!")
|
|
|
|
################
|
2022-11-16 20:42:40 +01:00
|
|
|
term_of_defence = TermOfDefence.query.filter(TermOfDefence.id == term_of_defence_id,
|
|
|
|
TermOfDefence.examination_schedule_id == examination_schedule_id).first()
|
2022-11-17 10:21:36 +01:00
|
|
|
ex = term_of_defence.examination_schedule
|
|
|
|
if term_of_defence is None or ex is None:
|
2022-11-16 20:42:40 +01:00
|
|
|
abort(400, "Term of defence not found!")
|
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
g = Group.query.join(ProjectSupervisor).filter(Group.year_group_id == examination_schedule_id). \
|
|
|
|
join(Group.students).filter_by(index=student.index).first()
|
|
|
|
if g is None or g.project_supervisor is None:
|
2022-11-12 16:18:07 +01:00
|
|
|
abort(400, "You don't have a group or your group doesn't have an assigned project supervisor!")
|
2022-10-27 19:53:39 +02:00
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
defence = TermOfDefence.query.filter(TermOfDefence.group_id == g.id,
|
2022-11-12 16:18:07 +01:00
|
|
|
TermOfDefence.examination_schedule_id == examination_schedule_id).first()
|
|
|
|
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
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
td = TermOfDefence.query.join(TermOfDefence.members_of_committee). \
|
2022-11-16 20:42:40 +01:00
|
|
|
filter_by(id=g.project_supervisor_id).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!")
|
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
term_of_defence.group_id = g.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!"}
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +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)
|
2022-11-12 16:18:07 +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
|
|
|
|
student = Student.query.filter(Student.index == data['student_index']).first()
|
|
|
|
if student is None:
|
|
|
|
abort(404, "Student doesn't exist!")
|
|
|
|
################
|
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
term_of_defence = TermOfDefence.query.join(ExaminationSchedule).filter(TermOfDefence.id == term_of_defence_id). \
|
2022-11-12 16:18:07 +01:00
|
|
|
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
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
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:
|
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
|
|
|
|
|
|
|
|
|
|
|
@bp.get('/examination-schedule/year-group/<int:year_group_id>/')
|
2022-11-16 20:42:40 +01:00
|
|
|
@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
|
|
|
|
student = Student.query.filter(Student.index == data['student_index']).first()
|
|
|
|
if student is None:
|
|
|
|
abort(404, "Student doesn't exist!")
|
|
|
|
################
|
|
|
|
|
|
|
|
# in the future filter after the mode of examination schedule if we will have authorization module
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
examination_schedules = ExaminationSchedule.query. \
|
|
|
|
filter(ExaminationSchedule.year_group_id == year_group_id). \
|
|
|
|
filter(ExaminationSchedule.start_date_for_enrollment_students < now). \
|
|
|
|
filter(ExaminationSchedule.end_date_for_enrollment_students > now).all()
|
|
|
|
return {'examination_schedules': examination_schedules}
|
|
|
|
|
|
|
|
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.get('/examination-schedule/<int:examination_schedule_id>/enrollments/')
|
|
|
|
@bp.input(TemporaryStudentSchema, location='query')
|
|
|
|
@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
|
|
|
|
student = Student.query.filter(Student.index == data['student_index']).first()
|
|
|
|
if student is None:
|
|
|
|
abort(404, "Student doesn't exist!")
|
|
|
|
################
|
|
|
|
# in the future filter after the mode of examination schedule if we will have authorization module
|
|
|
|
now = datetime.datetime.utcnow()
|
2022-11-16 20:42:40 +01:00
|
|
|
term_of_defences = TermOfDefence.query.filter(TermOfDefence.examination_schedule_id == examination_schedule_id). \
|
|
|
|
join(ExaminationSchedule, isouter=True). \
|
2022-11-12 16:18:07 +01:00
|
|
|
filter(ExaminationSchedule.start_date_for_enrollment_students < now). \
|
2022-11-16 20:42:40 +01:00
|
|
|
filter(ExaminationSchedule.end_date_for_enrollment_students > now).all()
|
|
|
|
|
|
|
|
return {'term_of_defences': term_of_defences}
|