2022-06-12 23:09:34 +02:00
|
|
|
from apiflask import APIBlueprint
|
2023-01-14 17:38:03 +01:00
|
|
|
from flask import abort
|
2022-06-12 23:09:34 +02:00
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
from ...base.schemas import MessageSchema
|
2022-06-12 23:09:34 +02:00
|
|
|
from ...base.utils import paginate_models
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...dependencies import db
|
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
|
|
|
from ...students.models import Group, YearGroup
|
|
|
|
from ..schemas.project_supervisor import (
|
|
|
|
ProjectSupervisorCreateSchema,
|
|
|
|
ProjectSupervisorEditSchema,
|
|
|
|
ProjectSupervisorQuerySchema,
|
|
|
|
ProjectSupervisorSchema,
|
|
|
|
ProjectSupervisorsPaginationSchema,
|
|
|
|
)
|
|
|
|
from ..schemas.students import MessageWithIdSchema
|
2022-06-12 23:09:34 +02:00
|
|
|
|
|
|
|
bp = APIBlueprint("project_supervisor", __name__, url_prefix="/project_supervisor")
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.get("/<int:year_group_id>/")
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.input(ProjectSupervisorQuerySchema, location="query")
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.output(ProjectSupervisorsPaginationSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def list_project_supervisors(year_group_id: int, query: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
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")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisor_query = ProjectSupervisor.search_by_fullname(
|
|
|
|
year_group_id, fullname, order_by_first_name, order_by_last_name
|
|
|
|
)
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
data = paginate_models(page, project_supervisor_query, per_page)
|
|
|
|
# print(get_debug_queries()[0])
|
2023-01-14 17:38:03 +01:00
|
|
|
return {"project_supervisors": data["items"], "max_pages": data["max_pages"]}
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.post("/<int:year_group_id>/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@bp.input(ProjectSupervisorCreateSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
@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!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
email = data["email"]
|
|
|
|
project_supervisor = ProjectSupervisor.query.filter(
|
|
|
|
ProjectSupervisor.email == email,
|
|
|
|
ProjectSupervisor.year_group_id == year_group_id,
|
|
|
|
).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
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
project_supervisor = ProjectSupervisor(**data, year_group_id=year_group_id)
|
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-14 01:37:31 +01:00
|
|
|
@bp.get("/<int:project_supervisor_id>/detail/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@bp.output(ProjectSupervisorSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def detail_project_supervisor(project_supervisor_id: int) -> ProjectSupervisor:
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(
|
|
|
|
id=project_supervisor_id
|
|
|
|
).first()
|
2022-06-12 23:09:34 +02:00
|
|
|
if project_supervisor is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found project supervisor!")
|
2022-06-12 23:09:34 +02:00
|
|
|
return project_supervisor
|
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.delete("/<int:project_supervisor_id>/")
|
2023-01-08 17:15:02 +01:00
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def delete_project_supervisor(project_supervisor_id: int) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(
|
|
|
|
id=project_supervisor_id
|
|
|
|
).first()
|
2022-06-12 23:09:34 +02:00
|
|
|
if project_supervisor is None:
|
2023-01-08 17:15:02 +01:00
|
|
|
abort(404, "Not found project supervisor!")
|
2022-06-13 18:34:39 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
count_groups = len(
|
|
|
|
Group.query.filter(Group.project_supervisor_id == project_supervisor.id).all()
|
|
|
|
)
|
2023-01-14 01:37:31 +01:00
|
|
|
if count_groups > 0:
|
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!"}
|
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.put("/<int:project_supervisor_id>/")
|
2022-06-12 23:09:34 +02:00
|
|
|
@bp.input(ProjectSupervisorEditSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def edit_project_supervisor(project_supervisor_id: int, data: dict) -> dict:
|
2022-06-12 23:09:34 +02:00
|
|
|
if not data:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(400, "You have passed empty data!")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisor_query = ProjectSupervisor.query.filter_by(
|
|
|
|
id=project_supervisor_id
|
|
|
|
)
|
2022-06-12 23:09:34 +02:00
|
|
|
project_supervisor = project_supervisor_query.first()
|
|
|
|
|
|
|
|
if project_supervisor is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found project supervisor!")
|
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
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.post("/copy-project-supervisors-from-last-year-group/<int:year_group_id>/")
|
2023-01-08 17:15:02 +01:00
|
|
|
@bp.output(MessageSchema, status_code=201)
|
2023-01-14 01:37:31 +01:00
|
|
|
def copy_project_supervisors_from_last_year_group(year_group_id: int) -> dict:
|
2022-11-12 16:18:07 +01:00
|
|
|
year_group = YearGroup.query.filter(YearGroup.id == year_group_id).first()
|
|
|
|
if year_group is None:
|
2023-01-08 17:15:02 +01:00
|
|
|
abort(404, "Not found year group!")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
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()
|
|
|
|
)
|
2023-01-14 01:37:31 +01:00
|
|
|
if last_year_group is None:
|
|
|
|
abort(400, "The latest year group doesn't exist!")
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
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()
|
|
|
|
]
|
2023-01-14 01:37:31 +01:00
|
|
|
for ps in project_supervisors:
|
|
|
|
if ps.email not in current_project_supervisors_email_in_new_year_group:
|
2023-01-14 17:38:03 +01:00
|
|
|
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,
|
|
|
|
)
|
2023-01-14 01:37:31 +01:00
|
|
|
db.session.add(new_ps)
|
2022-11-12 16:18:07 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
return {"message": "Project Supervisors was added!"}
|