61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import datetime
|
|
|
|
from apiflask import APIBlueprint
|
|
from flask import abort, current_app
|
|
|
|
from ..schemas import MessageSchema, EnrollmentCreateSchema
|
|
from ...examination_schedule.models import Enrollment, Committee, ExaminationSchedule
|
|
from ...dependencies import db
|
|
|
|
bp = APIBlueprint("enrollments", __name__, url_prefix="/enrollments")
|
|
|
|
|
|
@bp.post('/<int:examination_schedule_id>/')
|
|
@bp.input(EnrollmentCreateSchema, location='json')
|
|
@bp.output(MessageSchema)
|
|
def create_enrollments(examination_schedule_id: int, data: dict) -> dict:
|
|
prt = current_app.config["PROJECT_PRESENTATION_TIME"]
|
|
|
|
examination_schedule = db.session.query(ExaminationSchedule).filter(
|
|
ExaminationSchedule.id == examination_schedule_id).first()
|
|
if examination_schedule is None:
|
|
abort(404, "Examination schedule doesn't exist!")
|
|
|
|
start_date = data['start_date']
|
|
end_date = data['end_date']
|
|
if start_date > end_date:
|
|
abort(400, "Invalid dates! End date must be greater than start date!")
|
|
if start_date.date() != end_date.date():
|
|
abort(400, "Invalid dates! Only hours can be different!")
|
|
|
|
delta = end_date - start_date
|
|
delta_in_minutes = delta.total_seconds() / 60
|
|
if delta_in_minutes % prt != 0:
|
|
abort(400, "Invalid format dates!")
|
|
|
|
amount = int(delta_in_minutes // prt)
|
|
enrollments = []
|
|
for i in range(amount):
|
|
sd = start_date + datetime.timedelta(minutes=i * prt)
|
|
ed = start_date + datetime.timedelta(minutes=(i + 1) * prt)
|
|
enrollment = Enrollment(start_date=sd, end_date=ed)
|
|
enrollments.append(enrollment)
|
|
db.session.add_all(enrollments)
|
|
db.session.commit()
|
|
|
|
committees = [Committee(enrollment_id=e.id) for e in enrollments]
|
|
db.session.add_all(committees)
|
|
db.session.commit()
|
|
return {"message": "Enrollments was created!"}
|
|
|
|
|
|
@bp.delete('/<int:enrollment_id>/')
|
|
@bp.output(MessageSchema)
|
|
def delete_enrollment(enrollment_id: int) -> dict:
|
|
enrollment = db.session.query(Enrollment).filter(Enrollment.id == enrollment_id).first()
|
|
if enrollment is None:
|
|
abort(404, "Enrollment doesn't exist!")
|
|
db.session.delete(enrollment)
|
|
db.session.commit()
|
|
return {"message": "Enrollment was deleted!"}
|