system-pri/backend/app/coordinator/routes/project_supervisor.py

124 lines
5.2 KiB
Python

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, YearGroup
from ..schemas import ProjectSupervisorSchema, ProjectSupervisorEditSchema, ProjectSupervisorsPaginationSchema, \
ProjectSupervisorCreateSchema, MessageWithIdSchema, ProjectSupervisorQuerySchema
from ...base.schemas import MessageSchema
from ...dependencies import db
from ...base.utils import paginate_models
bp = APIBlueprint("project_supervisor", __name__, url_prefix="/project_supervisor")
@bp.get("/<int:year_group_id>/")
@bp.input(ProjectSupervisorQuerySchema, location='query')
@bp.output(ProjectSupervisorsPaginationSchema)
def list_project_supervisors(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("/<int:year_group_id>/")
@bp.input(ProjectSupervisorCreateSchema)
@bp.output(MessageWithIdSchema, status_code=201)
def create_project_supervisor(year_group_id: int, data: dict) -> dict:
year_group = YearGroup.query.filter(YearGroup.id == year_group_id).first()
if year_group is None:
abort(404, "Not found year group!")
email = data['email']
project_supervisor = ProjectSupervisor.query.filter(ProjectSupervisor.email == email).first()
if project_supervisor is not None:
abort(400, "Project Supervisor has already exists!")
project_supervisor = ProjectSupervisor(**data, year_group_id=year_group_id)
db.session.add(project_supervisor)
db.session.commit()
return {"message": "Project Supervisor was created!", "id": project_supervisor.id}
@bp.get("/<int:project_supervisor_id>/detail/")
@bp.output(ProjectSupervisorSchema)
def detail_project_supervisor(project_supervisor_id: int) -> ProjectSupervisor:
project_supervisor = ProjectSupervisor.query.filter_by(id=project_supervisor_id).first()
if project_supervisor is None:
abort(404, 'Not found project supervisor!')
return project_supervisor
@bp.delete("/<int:project_supervisor_id>/")
@bp.output(MessageSchema)
def delete_project_supervisor(project_supervisor_id: int) -> dict:
project_supervisor = ProjectSupervisor.query.filter_by(id=project_supervisor_id).first()
if project_supervisor is None:
abort(404, "Not found project supervisor!")
count_groups = len(Group.query.filter(Group.project_supervisor_id == project_supervisor.id).all())
if count_groups > 0:
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("/<int:project_supervisor_id>/")
@bp.input(ProjectSupervisorEditSchema)
@bp.output(MessageSchema)
def edit_project_supervisor(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=project_supervisor_id)
project_supervisor = project_supervisor_query.first()
if project_supervisor is None:
abort(404, f"Not found project supervisor!")
project_supervisor_query.update(data)
db.session.commit()
return {"message": "Project Supervisor was updated!"}
@bp.post("/copy-project-supervisors-from-last-year-group/<int:year_group_id>/")
@bp.output(MessageSchema, status_code=201)
def copy_project_supervisors_from_last_year_group(year_group_id: int) -> dict:
year_group = YearGroup.query.filter(YearGroup.id == year_group_id).first()
if year_group is None:
abort(404, "Not found year group!")
last_year_group = YearGroup.query.filter(YearGroup.mode == year_group.mode). \
filter(YearGroup.id != year_group_id).filter(YearGroup.created_at < year_group.created_at). \
order_by(db.desc(YearGroup.created_at)).first()
if last_year_group is None:
abort(400, "The latest year group doesn't exist!")
project_supervisors = ProjectSupervisor.query.filter(ProjectSupervisor.year_group_id == last_year_group.id).all()
current_project_supervisors_email_in_new_year_group = [ps.email for ps in ProjectSupervisor.query.filter(
ProjectSupervisor.year_group_id == year_group_id).all()]
for ps in project_supervisors:
if ps.email not in current_project_supervisors_email_in_new_year_group:
new_ps = ProjectSupervisor(first_name=ps.first_name, last_name=ps.last_name, email=ps.email,
limit_group=ps.limit_group, year_group_id=year_group_id)
db.session.add(new_ps)
db.session.commit()
return {"message": "Project Supervisors was added!"}