2022-06-12 23:09:34 +02:00
|
|
|
from flask import abort
|
|
|
|
from apiflask import APIBlueprint
|
|
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor, YearGroupProjectSupervisors
|
|
|
|
from ...students.models import Group, YearGroup
|
2022-06-12 23:09:34 +02:00
|
|
|
from ..schemas import ProjectSupervisorSchema, ProjectSupervisorEditSchema, ProjectSupervisorsPaginationSchema, \
|
2022-11-12 16:18:07 +01:00
|
|
|
ProjectSupervisorCreateSchema, MessageSchema, ProjectSupervisorQuerySchema, ProjectSupervisorYearGroupSchema
|
2022-06-13 18:34:39 +02:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.get("/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@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(
|
2022-11-12 16:18:07 +01:00
|
|
|
None, fullname, order_by_first_name, order_by_last_name)
|
2022-06-12 23:09:34 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.get("/<int:year_group_id>/")
|
|
|
|
@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("/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@bp.input(ProjectSupervisorCreateSchema)
|
2023-01-06 22:35:47 +01:00
|
|
|
@bp.output(MessageSchema, status_code=201)
|
2022-06-12 23:09:34 +02:00
|
|
|
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-11-12 16:18:07 +01:00
|
|
|
|
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()
|
|
|
|
|
2022-12-16 03:28:27 +01:00
|
|
|
return {"message": "Project Supervisor was created!", "id": project_supervisor.id}
|
2022-06-12 23:09:34 +02:00
|
|
|
|
|
|
|
|
2023-01-06 22:35:47 +01:00
|
|
|
@bp.get("/<int:id>/detail/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@bp.output(ProjectSupervisorSchema)
|
|
|
|
def detail_project_supervisor(id: int) -> ProjectSupervisor:
|
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(id=id).first()
|
|
|
|
if project_supervisor is None:
|
2023-01-06 22:35:47 +01:00
|
|
|
abort(404, 'Not found project supervisor!')
|
2022-06-12 23:09:34 +02:00
|
|
|
return project_supervisor
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.delete("/<int:id>/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@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
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
count_groups = db.session.query(db.func.count(ProjectSupervisor.id)).join(Group). \
|
2022-06-14 00:44:12 +02:00
|
|
|
filter(ProjectSupervisor.id == id).group_by(ProjectSupervisor.id)
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2022-06-14 00:44:12 +02:00
|
|
|
if count_groups is not None:
|
2022-11-17 10:21:36 +01:00
|
|
|
abort(400, "Project Supervisor has at least one group!")
|
2022-06-12 23:09:34 +02:00
|
|
|
|
|
|
|
db.session.delete(project_supervisor)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Project Supervisor was deleted!"}
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.put("/<int:id>/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@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!')
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2022-06-12 23:09:34 +02:00
|
|
|
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-11-12 16:18:07 +01:00
|
|
|
|
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!"}
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.post("/<int:id>/year-group/<int:year_group_id>")
|
|
|
|
@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("/<int:id>/year-group/<int:year_group_id>")
|
|
|
|
@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("/<int:id>/year-group/<int:year_group_id>")
|
|
|
|
@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!"}
|