2022-11-12 16:18:07 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
2022-10-27 15:59:02 +02:00
|
|
|
from apiflask import APIBlueprint
|
2022-11-12 16:18:07 +01:00
|
|
|
from flask import abort
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
from ..schemas import MessageSchema, TimeAvailabilityCreateSchema, TemporaryProjectSupervisorSchema, \
|
2022-11-16 20:42:40 +01:00
|
|
|
ListOfFreeTimesSchema, ListOfTermOfDefenceSchema
|
2022-10-27 15:59:02 +02:00
|
|
|
from ...dependencies import db
|
|
|
|
from ..models import ProjectSupervisor
|
2022-11-12 16:18:07 +01:00
|
|
|
from ...examination_schedule.models import ExaminationSchedule, TemporaryAvailability, TermOfDefence
|
2022-10-27 15:59:02 +02:00
|
|
|
|
|
|
|
bp = APIBlueprint("enrollments", __name__, url_prefix="/")
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.post('/<int:examination_schedule_id>/enrollments/')
|
|
|
|
@bp.input(TimeAvailabilityCreateSchema)
|
2022-10-27 15:59:02 +02:00
|
|
|
@bp.output(MessageSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def set_your_free_time_to_examination_schedule(examination_schedule_id: int, data: dict) -> dict:
|
2022-10-27 15:59:02 +02:00
|
|
|
# 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!")
|
|
|
|
################
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
es = ExaminationSchedule.query.filter(ExaminationSchedule.id == examination_schedule_id).first()
|
|
|
|
if es is None:
|
|
|
|
abort(404, "Examination schedule doesn't exist!")
|
|
|
|
|
|
|
|
sd = data['start_date']
|
|
|
|
ed = data['end_date']
|
|
|
|
if sd > ed:
|
|
|
|
abort(400, "Invalid data! End date must be greater than start date!")
|
|
|
|
|
2022-11-16 20:42:40 +01:00
|
|
|
print(es.start_date.timestamp() >= sd.timestamp())
|
|
|
|
print(es.end_date.timestamp() <= ed.timestamp())
|
|
|
|
if not (es.start_date.timestamp() <= sd.timestamp() and es.end_date.timestamp() >= ed.timestamp()):
|
2022-11-12 16:18:07 +01:00
|
|
|
abort(400, "Invalid date ranges!")
|
|
|
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
if es.start_date_for_enrollment_students is not None and \
|
|
|
|
es.start_date_for_enrollment_students.timestamp() < now.timestamp():
|
|
|
|
abort(403, "Enrollment has started! You cannot set your free time!")
|
|
|
|
|
|
|
|
ta_query = TemporaryAvailability.query.filter(
|
|
|
|
TemporaryAvailability.examination_schedule_id == examination_schedule_id). \
|
|
|
|
filter(TemporaryAvailability.project_supervisor_id == project_supervisor.id)
|
|
|
|
if ta_query.first() is not None:
|
|
|
|
# implement logic
|
|
|
|
pass
|
|
|
|
|
2022-11-16 20:42:40 +01:00
|
|
|
ta = TemporaryAvailability(**data, examination_schedule_id=examination_schedule_id)
|
2022-11-12 16:18:07 +01:00
|
|
|
db.session.add(ta)
|
|
|
|
db.session.commit()
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
return {"message": "You have just assigned your free time!"}
|
2022-10-27 15:59:02 +02:00
|
|
|
|
|
|
|
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.delete('/<int:examination_schedule_id>/enrollments/<int:temporary_availability_id>/')
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.input(TemporaryProjectSupervisorSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2022-11-16 20:42:40 +01:00
|
|
|
def delete_your_free_time_from_examination_schedule(examination_schedule_id: int, temporary_availability_id: int,
|
|
|
|
data: dict) -> dict:
|
2022-11-12 16:18:07 +01:00
|
|
|
# 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!")
|
|
|
|
################
|
|
|
|
ta = TemporaryAvailability.query.filter(TemporaryAvailability.examination_schedule_id == examination_schedule_id,
|
|
|
|
TemporaryAvailability.project_supervisor_id == project_supervisor.id,
|
2022-11-16 20:42:40 +01:00
|
|
|
TemporaryAvailability.id == temporary_availability_id).first()
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
if ta is None:
|
|
|
|
abort(404, "Your free time doesn't exist!")
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
db.session.delete(ta)
|
2022-10-27 15:59:02 +02:00
|
|
|
db.session.commit()
|
2022-11-12 16:18:07 +01:00
|
|
|
return {"message": "You have just removed your free time!"}
|
2022-10-27 15:59:02 +02:00
|
|
|
|
|
|
|
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.get('/<int:examination_schedule_id>/temporary-availabilities/')
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.input(TemporaryProjectSupervisorSchema, location='query')
|
|
|
|
@bp.output(ListOfFreeTimesSchema)
|
|
|
|
def list_enrollments_for_project_supervisor(examination_schedule_id: int, data: dict) -> dict:
|
2022-10-27 15:59:02 +02:00
|
|
|
# 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!")
|
|
|
|
################
|
2022-11-12 16:18:07 +01:00
|
|
|
es = ExaminationSchedule.query.filter(ExaminationSchedule.id == examination_schedule_id,
|
|
|
|
ExaminationSchedule.year_group_id).first()
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
if es is None:
|
|
|
|
abort(404, "Examination schedule doesn't exist!")
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
now = datetime.utcnow()
|
2022-11-16 20:42:40 +01:00
|
|
|
start_date = es.start_date_for_enrollment_students
|
|
|
|
if start_date is not None and start_date.timestamp() < now.timestamp():
|
2022-11-12 16:18:07 +01:00
|
|
|
abort(403, "Forbidden! Enrollment has just started! You cannot assign to the exam committees!")
|
2022-10-27 15:59:02 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
# list of your term of defences first enrollment
|
|
|
|
ta = TemporaryAvailability.query.filter(TemporaryAvailability.examination_schedule_id == examination_schedule_id,
|
|
|
|
TemporaryAvailability.project_supervisor_id == project_supervisor.id).all()
|
2022-11-16 20:42:40 +01:00
|
|
|
return {"free_times": ta}
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.get('/<int:examination_schedule_id>/term-of-defences/')
|
|
|
|
@bp.input(TemporaryProjectSupervisorSchema, location='query')
|
2022-11-16 20:42:40 +01:00
|
|
|
@bp.output(ListOfTermOfDefenceSchema)
|
|
|
|
def list_created_term_of_defences_by_coordinator_for_project_supervisor(examination_schedule_id: int,
|
|
|
|
data: dict) -> dict:
|
2022-11-12 16:18:07 +01:00
|
|
|
# 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!")
|
|
|
|
################
|
|
|
|
es = ExaminationSchedule.query.filter(ExaminationSchedule.id == examination_schedule_id,
|
|
|
|
ExaminationSchedule.year_group_id).first()
|
|
|
|
if es is None:
|
|
|
|
abort(404, "Examination schedule doesn't exist!")
|
|
|
|
|
|
|
|
# list of your free times first enrollment
|
|
|
|
td = TermOfDefence.query.join(TermOfDefence.members_of_committee). \
|
|
|
|
filter(TermOfDefence.examination_schedule_id == examination_schedule_id). \
|
2022-11-16 20:42:40 +01:00
|
|
|
filter_by(id=project_supervisor.id).all()
|
|
|
|
return {"term_of_defences": td}
|