102 lines
3.7 KiB
Python
102 lines
3.7 KiB
Python
from flask import abort
|
|
from apiflask import APIBlueprint
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
|
from ..schemas import ProjectSupervisorSchema, ProjectSupervisorEditSchema, ProjectSupervisorsPaginationSchema, \
|
|
ProjectSupervisorCreateSchema, MessageSchema, FileSchema, ProjectSupervisorQuerySchema
|
|
from ...dependencies import db, ma
|
|
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']
|
|
email = data['email']
|
|
limit_group = data['limit_group']
|
|
mode = data['mode']
|
|
|
|
project_supervisor = ProjectSupervisor.query.filter(first_name=first_name).filter(last_name=last_name).first()
|
|
if project_supervisor is not None:
|
|
abort(400, "Project Supervisor has already exists!")
|
|
|
|
project_supervisor = ProjectSupervisor(first_name=first_name,
|
|
last_name = last_name,
|
|
email = email,
|
|
limit_group = limit_group,
|
|
mode = mode)
|
|
|
|
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!")
|
|
|
|
if project_supervisor.count_groups > 0:
|
|
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!")
|
|
|
|
project_supervisor_query.update(project_supervisor)
|
|
db.session.commit()
|
|
return {"message": "Project Supervisor was updated!"}
|