2022-06-13 22:58:52 +02:00
|
|
|
from flask import abort, current_app
|
2022-06-11 12:53:55 +02:00
|
|
|
from apiflask import APIBlueprint
|
|
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
|
2022-12-15 20:44:03 +01:00
|
|
|
from ...students.models import Group, Student, YearGroup, ProjectGradeSheet
|
2022-11-12 16:18:07 +01:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor, YearGroupProjectSupervisors
|
2022-06-12 17:45:45 +02:00
|
|
|
from ..schemas import GroupSchema, GroupEditSchema, GroupsPaginationSchema, \
|
2022-06-13 17:52:15 +02:00
|
|
|
GroupCreateSchema, MessageSchema, GroupQuerySchema
|
|
|
|
from ...dependencies import db
|
2022-06-12 17:45:45 +02:00
|
|
|
from ...base.utils import paginate_models
|
2022-12-15 22:32:01 +01:00
|
|
|
from ..utils import load_weight_for_project_grade_sheet, calculate_points_for_one_term
|
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>/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.input(GroupQuerySchema, location='query')
|
|
|
|
@bp.output(GroupsPaginationSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def list_groups(year_group_id: int, query: dict) -> dict:
|
2022-06-12 17:45:45 +02: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
|
|
|
weights = load_weight_for_project_grade_sheet()
|
2022-06-12 17:45:45 +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)
|
|
|
|
|
2022-12-15 22:32:01 +01:00
|
|
|
items = data['items']
|
|
|
|
pgs = []
|
|
|
|
for g in items:
|
|
|
|
if len(g.project_grade_sheet)==0:
|
|
|
|
pgs.append(None)
|
|
|
|
else:
|
|
|
|
pgs.append(g.project_grade_sheet[0])
|
|
|
|
calculated_points = calculate_points_for_one_term(weights, pgs)
|
|
|
|
|
|
|
|
for group, points in zip(items, calculated_points):
|
|
|
|
group.points_for_first_term = points[0]
|
|
|
|
group.points_for_second_term = points[1]
|
|
|
|
|
2022-06-12 17:45:45 +02:00
|
|
|
return {
|
2022-12-15 22:32:01 +01:00
|
|
|
"groups": items,
|
2022-06-13 18:04:30 +02:00
|
|
|
"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)
|
|
|
|
@bp.output(MessageSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def create_group(year_group_id: int, data: dict) -> dict:
|
2022-06-12 17:45:45 +02:00
|
|
|
name = data['name']
|
2022-06-13 17:52:15 +02:00
|
|
|
students_indexes = data['students']
|
2022-06-12 17:45:45 +02:00
|
|
|
project_supervisor_id = data['project_supervisor_id']
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
yg = YearGroup.query.filter(YearGroup.id == year_group_id).first()
|
|
|
|
if yg is None:
|
|
|
|
abort(404, "YearGroup doesn't exist!")
|
|
|
|
|
2022-06-13 20:03:27 +02:00
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(id=project_supervisor_id).first()
|
|
|
|
|
2022-06-13 22:58:52 +02:00
|
|
|
limit_student_per_group = current_app.config.get('LIMIT_STUDENTS_PER_GROUP')
|
2022-06-13 21:20:43 +02:00
|
|
|
if project_supervisor is None:
|
|
|
|
abort(400, f"Project Supervisor with id {project_supervisor_id} doesnt exist")
|
2022-06-13 22:58:52 +02:00
|
|
|
elif limit_student_per_group is not None and limit_student_per_group < len(students_indexes):
|
2022-06-13 21:20:43 +02:00
|
|
|
abort(400, f"Too much students you want add to group, "
|
2022-06-13 22:58:52 +02:00
|
|
|
f"The group can have only {limit_student_per_group} students")
|
2022-06-13 21:20:43 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
limit = db.session.query(db.func.count(ProjectSupervisor.id)). \
|
|
|
|
join(Group).filter(ProjectSupervisor.id == project_supervisor_id).group_by(ProjectSupervisor.id).scalar()
|
2022-06-13 21:20:43 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
ygps = YearGroupProjectSupervisors.query. \
|
|
|
|
filter(YearGroupProjectSupervisors.year_group_id == year_group_id,
|
|
|
|
YearGroupProjectSupervisors.project_supervisor_id == project_supervisor_id).first()
|
|
|
|
if limit is not None and ygps is not None and limit >= ygps.limit_group:
|
2022-06-13 21:20:43 +02:00
|
|
|
abort(400, "Can't create new group, project supervisor achieved a limit of groups")
|
2022-06-13 20:03:27 +02:00
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
group = Group(name=name, project_supervisor_id=project_supervisor_id, year_group_id=year_group_id)
|
2022-06-13 17:52:15 +02:00
|
|
|
|
2022-11-17 10:21:36 +01:00
|
|
|
students_without_groups = db.session.query(Student, Group). \
|
|
|
|
join(Group, Student.groups). \
|
|
|
|
filter(Group.year_group_id == year_group_id). \
|
|
|
|
filter(db.or_(*[Student.index == idx for idx in students_indexes])).all()
|
|
|
|
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!")
|
|
|
|
|
2022-06-13 21:47:12 +02:00
|
|
|
db.session.add(group)
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-06-13 17:52:15 +02:00
|
|
|
students = db.session.query(Student).filter(Student.index.in_(students_indexes)).all()
|
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
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-11-17 14:56:19 +01:00
|
|
|
@bp.get("/<int:id>/detail/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.output(GroupSchema)
|
|
|
|
def detail_group(id: int) -> Group:
|
|
|
|
group = Group.query.filter_by(id=id).first()
|
|
|
|
if group is None:
|
|
|
|
abort(400, f"Group with id {id} doesn't exist!")
|
2022-06-11 12:53:55 +02:00
|
|
|
return group
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.delete("/<int:id>/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.output(MessageSchema, status_code=202)
|
|
|
|
def delete_group(id: int) -> dict:
|
|
|
|
group = Group.query.filter_by(id=id).first()
|
|
|
|
if group is None:
|
|
|
|
abort(400, f"Group with id {id} doesn't exist!")
|
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!"}
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.put("/<int:id>/")
|
2022-06-12 17:45:45 +02:00
|
|
|
@bp.input(GroupEditSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def edit_group(id: int, data: dict) -> dict:
|
|
|
|
if not data:
|
|
|
|
abort(400, 'You have passed empty data!')
|
2022-06-13 17:52:15 +02:00
|
|
|
|
2022-06-12 17:45:45 +02:00
|
|
|
group_query = Group.query.filter_by(id=id)
|
|
|
|
group = group_query.first()
|
|
|
|
|
|
|
|
if group is None:
|
|
|
|
abort(400, f"Group with id {id} doesn't exist!")
|
2022-06-13 17:52:15 +02:00
|
|
|
|
2022-06-13 18:34:39 +02:00
|
|
|
group_query.update(data)
|
2022-06-12 17:45:45 +02:00
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Group was updated!"}
|