2022-06-12 23:09:34 +02:00
|
|
|
from flask import abort
|
|
|
|
from apiflask import APIBlueprint
|
|
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
|
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
2022-06-13 18:34:39 +02:00
|
|
|
from ...students.models import Group
|
2022-06-12 23:09:34 +02:00
|
|
|
from ..schemas import ProjectSupervisorSchema, ProjectSupervisorEditSchema, ProjectSupervisorsPaginationSchema, \
|
2022-06-13 18:34:39 +02:00
|
|
|
ProjectSupervisorCreateSchema, MessageSchema, ProjectSupervisorQuerySchema
|
|
|
|
from ...dependencies import db
|
2022-06-12 23:09:34 +02:00
|
|
|
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)
|
|
|
|
|
2022-06-13 18:04:30 +02:00
|
|
|
data = paginate_models(page, project_supervisor_query, per_page)
|
2022-06-12 23:09:34 +02:00
|
|
|
# print(get_debug_queries()[0])
|
|
|
|
return {
|
2022-06-13 18:04:30 +02:00
|
|
|
"project_supervisors": data['items'],
|
|
|
|
"max_pages": data['max_pages']
|
2022-06-12 23:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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']
|
2022-06-13 18:41:28 +02:00
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(first_name=first_name).filter_by(last_name=last_name).first()
|
2022-06-12 23:09:34 +02:00
|
|
|
if project_supervisor is not None:
|
|
|
|
abort(400, "Project Supervisor has already exists!")
|
|
|
|
|
2022-06-13 18:34:39 +02:00
|
|
|
project_supervisor = ProjectSupervisor(**data)
|
2022-06-12 23:09:34 +02:00
|
|
|
|
|
|
|
db.session.add(project_supervisor)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {"message": "Project Supervisor was created!"}
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/<int:id>/", 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("/<int:id>/", 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!")
|
2022-06-13 18:34:39 +02:00
|
|
|
|
|
|
|
count_groups = db.session.query(db.func.count(ProjectSupervisor.id)).join(Group).\
|
|
|
|
filter(ProjectSupervisor.id == id).group_by(ProjectSupervisor.id).scalar()
|
|
|
|
if count_groups > 0:
|
2022-06-12 23:09:34 +02:00
|
|
|
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("/<int:id>", 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!")
|
|
|
|
|
2022-06-13 18:34:39 +02:00
|
|
|
project_supervisor_query.update(data)
|
2022-06-12 23:09:34 +02:00
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Project Supervisor was updated!"}
|