2022-11-02 23:21:08 +01:00
|
|
|
import datetime
|
|
|
|
|
2022-10-26 12:41:22 +02:00
|
|
|
from apiflask import APIBlueprint
|
2023-01-14 17:38:03 +01:00
|
|
|
from flask import Response, abort, current_app, make_response
|
2022-10-26 12:41:22 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...base.mode import EnrollmentsMode
|
|
|
|
from ...base.schemas import MessageSchema
|
2022-11-02 23:21:08 +01:00
|
|
|
from ...base.utils import paginate_models
|
|
|
|
from ...dependencies import db
|
2022-11-17 20:33:20 +01:00
|
|
|
from ...examination_schedule.models import ExaminationSchedule, TermOfDefence
|
2022-11-02 23:21:08 +01:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...students.models import Group, YearGroup
|
|
|
|
from ..query.enrollments import set_enrollments_mode
|
|
|
|
from ..schemas.examination_schedule import (
|
|
|
|
ExaminationScheduleSchema,
|
|
|
|
ExaminationSchedulesPaginationSchema,
|
|
|
|
ExaminationSchedulesQuerySchema,
|
|
|
|
)
|
|
|
|
from ..utils import generate_examination_schedule_pdf_file, get_duration_time
|
2022-10-26 12:41:22 +02:00
|
|
|
|
|
|
|
bp = APIBlueprint("examination_schedule", __name__, url_prefix="/examination_schedule")
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/<int:year_group_id>/")
|
|
|
|
@bp.input(ExaminationSchedulesQuerySchema, location="query")
|
2022-10-26 12:41:22 +02:00
|
|
|
@bp.output(ExaminationSchedulesPaginationSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def list_examination_schedule(year_group_id: int, query: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
page = query.get("page")
|
|
|
|
per_page = query.get("per_page")
|
|
|
|
es_query = ExaminationSchedule.query.filter(
|
|
|
|
ExaminationSchedule.year_group_id == year_group_id
|
|
|
|
)
|
2022-11-12 16:18:07 +01:00
|
|
|
data = paginate_models(page, es_query, per_page)
|
2023-01-14 17:38:03 +01:00
|
|
|
return {"examination_schedules": data["items"], "max_pages": data["max_pages"]}
|
2022-10-26 12:41:22 +02:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post("/<int:year_group_id>/")
|
2022-10-26 12:41:22 +02:00
|
|
|
@bp.input(ExaminationScheduleSchema)
|
2023-01-10 00:17:25 +01:00
|
|
|
@bp.output(MessageSchema, status_code=201)
|
2022-11-12 16:18:07 +01:00
|
|
|
def create_examination_schedule(year_group_id: int, data: dict) -> dict:
|
|
|
|
yg = YearGroup.query.filter(YearGroup.id == year_group_id).first()
|
|
|
|
if yg is None:
|
|
|
|
abort(404, "Year group doesn't exist!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
if data.get("start_date") > data.get("end_date"):
|
2022-11-12 16:18:07 +01:00
|
|
|
abort(400, "Invalid data! End date must be greater than start date!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
duration_time = get_duration_time(yg.mode)
|
2022-11-16 20:42:40 +01:00
|
|
|
if duration_time is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(400, "Invalid duration time!")
|
2022-11-16 20:42:40 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
examination_schedule = ExaminationSchedule(
|
|
|
|
**data, year_group_id=year_group_id, duration_time=duration_time
|
|
|
|
)
|
2022-10-26 12:41:22 +02:00
|
|
|
db.session.add(examination_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Examination schedule was created!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.put("/<int:examination_schedule_id>/")
|
2022-10-26 12:41:22 +02:00
|
|
|
@bp.input(ExaminationScheduleSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def update_examination_schedule(examination_schedule_id: int, data: dict) -> dict:
|
|
|
|
examination_schedule_query = db.session.query(ExaminationSchedule).filter(
|
|
|
|
ExaminationSchedule.id == examination_schedule_id
|
|
|
|
)
|
2022-10-26 12:41:22 +02:00
|
|
|
examination_schedule = examination_schedule_query.first()
|
|
|
|
|
|
|
|
if examination_schedule is None:
|
|
|
|
abort(404, "Examination schedule doesn't exist!")
|
|
|
|
examination_schedule_query.update(data)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Examination schedule was updated!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.delete("/<int:examination_schedule_id>/")
|
2022-10-26 15:11:50 +02:00
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def delete_examination_schedule(examination_schedule_id: int) -> dict:
|
|
|
|
examination_schedule = (
|
|
|
|
db.session.query(ExaminationSchedule)
|
|
|
|
.filter(ExaminationSchedule.id == examination_schedule_id)
|
|
|
|
.first()
|
|
|
|
)
|
2022-10-26 15:11:50 +02:00
|
|
|
if examination_schedule is None:
|
|
|
|
abort(404, "Examination schedule doesn't exist!")
|
|
|
|
db.session.delete(examination_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Examination schedule was deleted!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.put("/<int:examination_schedule_id>/open-enrollments/")
|
2022-10-26 12:41:22 +02:00
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 17:38:03 +01:00
|
|
|
def open_enrollments(examination_schedule_id: int) -> dict:
|
|
|
|
return set_enrollments_mode(examination_schedule_id, EnrollmentsMode.OPEN, "open")
|
2022-10-26 12:41:22 +02:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.put("/<int:examination_schedule_id>/close-enrollments/")
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def close_enrollments(examination_schedule_id: int) -> dict:
|
|
|
|
return set_enrollments_mode(examination_schedule_id, EnrollmentsMode.CLOSE, "close")
|
2022-11-02 23:21:08 +01:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post("/<int:examination_schedule_id>/download/")
|
2022-11-02 23:21:08 +01:00
|
|
|
def download_examination_schedule(examination_schedule_id: int) -> Response:
|
2023-01-14 17:38:03 +01:00
|
|
|
examination_schedule = (
|
|
|
|
db.session.query(ExaminationSchedule)
|
|
|
|
.filter(ExaminationSchedule.id == examination_schedule_id)
|
|
|
|
.first()
|
|
|
|
)
|
2022-11-02 23:21:08 +01:00
|
|
|
|
|
|
|
if examination_schedule is None:
|
|
|
|
abort(404, "Examination schedule doesn't exist!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
distinct_dates = (
|
|
|
|
db.session.query(db.func.Date(TermOfDefence.start_date)).distinct().all()
|
|
|
|
)
|
2022-11-17 20:33:20 +01:00
|
|
|
|
|
|
|
nested_term_of_defences = []
|
2022-11-02 23:21:08 +01:00
|
|
|
for d in distinct_dates:
|
|
|
|
date_tmp = datetime.datetime.strptime(d[0], "%Y-%m-%d").date()
|
2023-01-14 17:38:03 +01:00
|
|
|
term_of_defences = (
|
|
|
|
db.session.query(TermOfDefence)
|
|
|
|
.join(Group, isouter=True)
|
|
|
|
.join(ProjectSupervisor, isouter=True)
|
|
|
|
.filter(TermOfDefence.examination_schedule_id == examination_schedule_id)
|
|
|
|
.filter(TermOfDefence.group_id.isnot(None))
|
|
|
|
.filter(db.func.Date(TermOfDefence.start_date) == date_tmp)
|
|
|
|
.all()
|
|
|
|
)
|
2022-11-17 20:33:20 +01:00
|
|
|
if len(term_of_defences) > 0:
|
|
|
|
nested_term_of_defences.append(term_of_defences)
|
|
|
|
# print(nested_term_of_defences)
|
2023-01-14 17:38:03 +01:00
|
|
|
base_dir = current_app.config.get("BASE_DIR")
|
|
|
|
pdf = generate_examination_schedule_pdf_file(
|
|
|
|
examination_schedule.title, nested_term_of_defences, base_dir
|
|
|
|
)
|
2022-11-02 23:21:08 +01:00
|
|
|
title = examination_schedule.title.replace("-", "_").split()
|
|
|
|
filename = "_".join(title)
|
|
|
|
|
|
|
|
response = make_response(pdf)
|
2023-01-14 17:38:03 +01:00
|
|
|
response.headers["Content-Type"] = "application/pdf"
|
|
|
|
response.headers["Content-Disposition"] = "attachment; filename=%s.pdf" % filename
|
2022-11-02 23:21:08 +01:00
|
|
|
return response
|