from flask import abort from apiflask import APIBlueprint from flask_sqlalchemy import get_debug_queries from ...project_supervisor.models import ProjectSupervisor, YearGroupProjectSupervisors from ...students.models import Group, YearGroup from ..schemas import ProjectSupervisorSchema, ProjectSupervisorEditSchema, ProjectSupervisorsPaginationSchema, \ ProjectSupervisorCreateSchema, MessageSchema, ProjectSupervisorQuerySchema, ProjectSupervisorYearGroupSchema from ...dependencies import db from ...base.utils import paginate_models bp = APIBlueprint("project_supervisor", __name__, url_prefix="/project_supervisor") @bp.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') 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( None, fullname, 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.get("//") @bp.input(ProjectSupervisorQuerySchema, location='query') @bp.output(ProjectSupervisorsPaginationSchema) def list_project_supervisors_by_year_group(year_group_id: int, 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') 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( year_group_id, fullname, 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.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!", "id": project_supervisor.id} @bp.get("//detail") @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.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, "Project Supervisor has at least one group!") db.session.delete(project_supervisor) db.session.commit() return {"message": "Project Supervisor was deleted!"} @bp.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!"} @bp.post("//year-group/") @bp.input(ProjectSupervisorYearGroupSchema) @bp.output(MessageSchema) def add_project_supervisor_to_year_group(id: int, year_group_id: int, data: dict) -> dict: if not data: abort(400, 'You have passed empty data!') limit_group = data.get('limit_group') project_supervisor = ProjectSupervisor.query.filter(ProjectSupervisor.id == id).first() if project_supervisor is None: abort(400, f"Project Supervisor with id {id} doesn't exist!") year_group = YearGroup.query.filter(YearGroup.id == year_group_id).first() if year_group is None: abort(400, "Year group doesn't exist!") ygps = YearGroupProjectSupervisors.query.filter(YearGroupProjectSupervisors.project_supervisor_id == id). \ filter(YearGroupProjectSupervisors.year_group_id == year_group_id).first() if ygps is not None: abort(400, "Project supervisor is assigned to this year group!") ygps = YearGroupProjectSupervisors(year_group_id=year_group_id, project_supervisor_id=id, limit_group=limit_group) db.session.add(ygps) db.session.commit() return {"message": "Project Supervisor was added to year group!"} @bp.delete("//year-group/") @bp.output(MessageSchema) def delete_project_supervisor_to_year_group(id: int, year_group_id: int) -> dict: project_supervisor = ProjectSupervisor.query.filter(ProjectSupervisor.id == id). \ filter(YearGroup.id == year_group_id).first() if project_supervisor is None: abort(400, "Project Supervisor doesn't exist!") ygps = YearGroupProjectSupervisors.query.filter(YearGroupProjectSupervisors.project_supervisor_id == id). \ filter(YearGroupProjectSupervisors.year_group_id == year_group_id).first() db.session.delete(project_supervisor) db.session.delete(ygps) db.session.commit() return {"message": "Project Supervisor was removed from this year group!"} @bp.put("//year-group/") @bp.input(ProjectSupervisorYearGroupSchema) @bp.output(MessageSchema) def update_limit_of_group_for_project_supervisor(id: int, year_group_id: int, data: dict) -> dict: project_supervisor = ProjectSupervisor.query.filter(ProjectSupervisor.id == id). \ filter(YearGroup.id == year_group_id).first() if project_supervisor is None: abort(400, "Project Supervisor doesn't exist!") ygps = YearGroupProjectSupervisors.query.filter(YearGroupProjectSupervisors.project_supervisor_id == id). \ filter(YearGroupProjectSupervisors.year_group_id == year_group_id) ygps.update(data) db.session.commit() return {"message": "Limit of group was changed!"}