from flask import abort from apiflask import APIBlueprint from flask_sqlalchemy import get_debug_queries from ...project_supervisor.models import ProjectSupervisor from ...students.models import Group from ..schemas import ProjectSupervisorSchema, ProjectSupervisorEditSchema, ProjectSupervisorsPaginationSchema, \ ProjectSupervisorCreateSchema, MessageSchema, ProjectSupervisorQuerySchema from ...dependencies import db from ...base.utils import paginate_models bp = APIBlueprint("project_supervisor", __name__, url_prefix="/project_supervisor") @bp.route("/", methods=["GET"]) @bp.input(ProjectSupervisorQuerySchema, location='query') @bp.output(ProjectSupervisorsPaginationSchema) def list_project_supervisors(query: dict) -> dict: fullname = query.get('fullname') order_by_first_name = query.get('order_by_first_name') order_by_last_name = query.get('order_by_last_name') mode = query.get('mode') page = query.get('page') per_page = query.get('per_page') project_supervisor_query = ProjectSupervisor.search_by_fullname_and_mode_and_order_by_first_name_or_last_name( fullname, mode, order_by_first_name, order_by_last_name) data = paginate_models(page, project_supervisor_query, per_page) # print(get_debug_queries()[0]) return { "project_supervisors": data['items'], "max_pages": data['max_pages'] } @bp.route("/", methods=["POST"]) @bp.input(ProjectSupervisorCreateSchema) @bp.output(MessageSchema) def create_project_supervisor(data: dict) -> dict: first_name = data['first_name'] last_name = data['last_name'] project_supervisor = ProjectSupervisor.query.filter_by(first_name=first_name).filter_by(last_name=last_name).first() if project_supervisor is not None: abort(400, "Project Supervisor has already exists!") project_supervisor = ProjectSupervisor(**data) db.session.add(project_supervisor) db.session.commit() return {"message": "Project Supervisor was created!"} @bp.route("//", methods=["GET"]) @bp.output(ProjectSupervisorSchema) def detail_project_supervisor(id: int) -> ProjectSupervisor: project_supervisor = ProjectSupervisor.query.filter_by(id=id).first() if project_supervisor is None: abort(400, f"Project Supervisor with id {id} doesn't exist!") return project_supervisor @bp.route("//", methods=["DELETE"]) @bp.output(MessageSchema, status_code=202) def delete_project_supervisor(id: int) -> dict: project_supervisor = ProjectSupervisor.query.filter_by(id=id).first() if project_supervisor is None: abort(400, f"Project Supervisor with id {id} doesn't exist!") count_groups = db.session.query(db.func.count(ProjectSupervisor.id)).join(Group).\ filter(ProjectSupervisor.id == id).group_by(ProjectSupervisor.id) if count_groups is not None: abort(400, f"Project Supervisor with id {id} has gropus!") db.session.delete(project_supervisor) db.session.commit() return {"message": "Project Supervisor was deleted!"} @bp.route("/", methods=["PUT"]) @bp.input(ProjectSupervisorEditSchema) @bp.output(MessageSchema) def edit_project_supervisor(id: int, data: dict) -> dict: if not data: abort(400, 'You have passed empty data!') project_supervisor_query = ProjectSupervisor.query.filter_by(id=id) project_supervisor = project_supervisor_query.first() if project_supervisor is None: abort(400, f"Project Supervisor with id {id} doesn't exist!") project_supervisor_query.update(data) db.session.commit() return {"message": "Project Supervisor was updated!"}