2022-06-11 12:53:55 +02:00
|
|
|
from apiflask import APIBlueprint
|
2023-01-14 17:38:03 +01:00
|
|
|
from flask import abort
|
2022-06-11 12:53:55 +02:00
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
from ...base.schemas import MessageSchema
|
2023-01-14 17:38:03 +01:00
|
|
|
from ...base.utils import paginate_models
|
|
|
|
from ...dependencies import db
|
|
|
|
from ...project_supervisor.models import ProjectSupervisor
|
|
|
|
from ...students.models import Group, ProjectGradeSheet, Student, YearGroup
|
|
|
|
from ..schemas.groups import (
|
|
|
|
GroupCreateSchema,
|
|
|
|
GroupEditSchema,
|
|
|
|
GroupQuerySchema,
|
2023-01-16 01:12:15 +01:00
|
|
|
GroupSetGradeSchema,
|
2023-01-14 17:38:03 +01:00
|
|
|
GroupsPaginationSchema,
|
|
|
|
)
|
|
|
|
from ..schemas.students import DetailGroupSchema
|
2023-01-16 01:12:15 +01:00
|
|
|
from ..utils import attach_grade_to_group_models
|
2022-06-11 12:53:55 +02:00
|
|
|
|
|
|
|
bp = APIBlueprint("groups", __name__, url_prefix="/groups")
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.get("/<int:year_group_id>/")
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.input(GroupQuerySchema, location="query")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.output(GroupsPaginationSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def list_groups(year_group_id: int, query: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
search_name = query.get("name")
|
|
|
|
page = query.get("page")
|
|
|
|
per_page = query.get("per_page")
|
2022-06-13 17:52:15 +02:00
|
|
|
|
2022-12-15 22:32:01 +01:00
|
|
|
groups_query = Group.search_by_name(year_group_id, search_name)
|
2022-06-13 18:04:30 +02:00
|
|
|
data = paginate_models(page, groups_query, per_page)
|
2023-01-14 17:38:03 +01:00
|
|
|
items = data["items"]
|
2023-01-16 01:12:15 +01:00
|
|
|
attach_grade_to_group_models(items)
|
2023-01-14 17:38:03 +01:00
|
|
|
return {"groups": items, "max_pages": data["max_pages"]}
|
2022-06-11 12:53:55 +02:00
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.post("/<int:year_group_id>/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.input(GroupCreateSchema)
|
2023-01-08 17:15:02 +01:00
|
|
|
@bp.output(MessageSchema, status_code=201)
|
2022-11-12 16:18:07 +01:00
|
|
|
def create_group(year_group_id: int, data: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
name = data["name"]
|
|
|
|
students_ids = data["students"]
|
|
|
|
project_supervisor_id = data["project_supervisor_id"]
|
2022-06-12 17:45:45 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
yg = YearGroup.query.filter(YearGroup.id == year_group_id).first()
|
|
|
|
if yg is None:
|
2023-01-08 20:26:41 +01:00
|
|
|
abort(404, "Not found year group!")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(
|
|
|
|
id=project_supervisor_id
|
|
|
|
).first()
|
2022-06-13 20:03:27 +02:00
|
|
|
|
2022-06-13 21:20:43 +02:00
|
|
|
if project_supervisor is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found project supervisor!")
|
|
|
|
|
|
|
|
group = Group(
|
|
|
|
name=name,
|
|
|
|
project_supervisor_id=project_supervisor_id,
|
|
|
|
year_group_id=year_group_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
students_without_groups = (
|
|
|
|
db.session.query(Student, Group)
|
|
|
|
.join(Group, Student.groups)
|
|
|
|
.filter(Group.year_group_id == year_group_id)
|
|
|
|
.filter(db.or_(*[Student.id == st_id for st_id in students_ids]))
|
|
|
|
.all()
|
|
|
|
)
|
2023-01-14 01:37:31 +01:00
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
if len(students_without_groups) > 0:
|
2022-06-13 17:52:15 +02:00
|
|
|
abort(400, "One or more students have already belonged to group!")
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
students = db.session.query(Student).filter(Student.id.in_(students_ids)).all()
|
|
|
|
if len(students) != len(students_ids):
|
2023-01-08 20:26:41 +01:00
|
|
|
abort(404, "Not found students!")
|
|
|
|
|
2022-06-13 21:47:12 +02:00
|
|
|
db.session.add(group)
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-06-12 17:45:45 +02:00
|
|
|
for student in students:
|
2022-11-17 10:21:36 +01:00
|
|
|
group.students.append(student)
|
2022-12-15 20:44:03 +01:00
|
|
|
pgs = ProjectGradeSheet(group_id=group.id)
|
|
|
|
db.session.add(pgs)
|
2022-06-12 17:45:45 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2022-10-27 19:53:39 +02:00
|
|
|
return {"message": "Group was created!"}
|
2022-06-11 12:53:55 +02:00
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.get("/<int:group_id>/detail/")
|
2023-01-06 19:24:02 +01:00
|
|
|
@bp.output(DetailGroupSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def detail_group(group_id: int) -> Group:
|
|
|
|
group = Group.query.filter_by(id=group_id).first()
|
2022-06-12 17:45:45 +02:00
|
|
|
if group is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found group!")
|
2023-01-16 01:12:15 +01:00
|
|
|
attach_grade_to_group_models([group])
|
2022-06-11 12:53:55 +02:00
|
|
|
return group
|
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.delete("/<int:group_id>/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.output(MessageSchema, status_code=202)
|
2023-01-14 01:37:31 +01:00
|
|
|
def delete_group(group_id: int) -> dict:
|
|
|
|
group = Group.query.filter_by(id=group_id).first()
|
2022-06-12 17:45:45 +02:00
|
|
|
if group is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found group!")
|
2022-06-14 00:44:12 +02:00
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
group.students = []
|
2022-06-12 17:45:45 +02:00
|
|
|
db.session.delete(group)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Group was deleted!"}
|
|
|
|
|
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
@bp.put("/<int:group_id>/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.input(GroupEditSchema)
|
|
|
|
@bp.output(MessageSchema)
|
2023-01-14 01:37:31 +01:00
|
|
|
def edit_group(group_id: int, data: dict) -> dict:
|
2022-06-12 17:45:45 +02:00
|
|
|
if not data:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(400, "You have passed empty data!")
|
2022-06-13 17:52:15 +02:00
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
group_query = Group.query.filter_by(id=group_id)
|
2022-06-12 17:45:45 +02:00
|
|
|
group = group_query.first()
|
|
|
|
|
|
|
|
if group is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found group!")
|
2022-06-13 17:52:15 +02:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
students_ids = data.get("students")
|
|
|
|
name = data.get("name")
|
|
|
|
project_supervisor_id = data.get("project_supervisor_id")
|
2023-01-08 20:26:41 +01:00
|
|
|
|
2023-01-14 01:37:31 +01:00
|
|
|
if students_ids is not None:
|
|
|
|
students = db.session.query(Student).filter(Student.id.in_(students_ids)).all()
|
|
|
|
if len(students_ids) != len(students):
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found students!")
|
2023-01-08 20:26:41 +01:00
|
|
|
group.students = students
|
|
|
|
|
|
|
|
if name is not None:
|
|
|
|
group.name = name
|
|
|
|
|
|
|
|
if project_supervisor_id is not None:
|
2023-01-14 17:38:03 +01:00
|
|
|
ps = ProjectSupervisor.query.filter(
|
|
|
|
ProjectSupervisor.id == project_supervisor_id
|
|
|
|
).first()
|
2023-01-08 20:26:41 +01:00
|
|
|
if ps is None:
|
|
|
|
abort(404, "Not found project supervisor!")
|
|
|
|
group.project_supervisor_id = project_supervisor_id
|
2023-01-06 19:24:02 +01:00
|
|
|
|
2022-06-12 17:45:45 +02:00
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Group was updated!"}
|
2023-01-16 01:12:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.put("/<int:group_id>/set-grades/")
|
|
|
|
@bp.input(GroupSetGradeSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def set_grade_for_group(group_id: int, data: dict) -> dict:
|
|
|
|
if not data:
|
|
|
|
abort(400, "You have passed empty data!")
|
|
|
|
|
|
|
|
group_query = Group.query.filter_by(id=group_id)
|
|
|
|
group = group_query.first()
|
|
|
|
|
|
|
|
if group is None:
|
|
|
|
abort(404, "Not found group!")
|
|
|
|
|
|
|
|
group_query.update(data)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {"message": "Grade was updated!"}
|