from apiflask import APIBlueprint from flask import abort from ..schemas import ExaminationScheduleSchema, ExaminationScheduleUpdateSchema, MessageSchema, \ ExaminationSchedulesQuerySchema, ExaminationSchedulesPaginationSchema from ...examination_schedule.models import ExaminationSchedule from ...dependencies import db from ...base.utils import paginate_models bp = APIBlueprint("examination_schedule", __name__, url_prefix="/examination_schedule") @bp.get('/') @bp.input(ExaminationSchedulesQuerySchema, location='query') @bp.output(ExaminationSchedulesPaginationSchema) def list_examination_schedule(query: dict) -> dict: page = query.get('page') per_page = query.get('per_page') data = paginate_models(page, ExaminationSchedule.query, per_page) return {'examination_schedules': data['items'], 'max_pages': data['max_pages']} @bp.post('/') @bp.input(ExaminationScheduleSchema) @bp.output(MessageSchema) def create_examination_schedule(data: dict) -> dict: examination_schedule = ExaminationSchedule(**data) db.session.add(examination_schedule) db.session.commit() return {"message": "Examination schedule was created!"} @bp.put('//') @bp.input(ExaminationScheduleSchema) @bp.output(MessageSchema) def update_title_examination_schedule(id: int, data: dict) -> dict: examination_schedule_query = db.session.query(ExaminationSchedule).filter(ExaminationSchedule.id == id) 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!"} @bp.delete('//') @bp.output(MessageSchema) def delete_examination_schedule(id: int) -> dict: examination_schedule = db.session.query(ExaminationSchedule).filter(ExaminationSchedule.id == id).first() 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!"} @bp.put('//date') @bp.input(ExaminationScheduleUpdateSchema) @bp.output(MessageSchema) def set_date_of_examination_schedule(id: int, data: dict) -> dict: examination_schedule_query = db.session.query(ExaminationSchedule).filter(ExaminationSchedule.id == id) examination_schedule = examination_schedule_query.first() if examination_schedule is None: abort(404, "Examination schedule doesn't exist!") if data['start_date'] > data['end_date']: abort(400, "Invalid data! End date must be greater than start date!") examination_schedule_query.update(data) db.session.commit() return {"message": "You set date of examination schedule!"}