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

107 lines
3.4 KiB
Python
Raw Normal View History

from flask import abort
from apiflask import APIBlueprint
from flask_sqlalchemy import get_debug_queries
2022-06-12 17:45:45 +02:00
from ...students.models import Group, Student
from ...project_supervisor.models import ProjectSupervisor
from ..schemas import GroupSchema, GroupEditSchema, GroupsPaginationSchema, \
GroupCreateSchema, MessageSchema, GroupQuerySchema
from ...dependencies import db
2022-06-12 17:45:45 +02:00
from ...base.utils import paginate_models
bp = APIBlueprint("groups", __name__, url_prefix="/groups")
@bp.route("/", methods=["GET"])
2022-06-12 17:45:45 +02:00
@bp.input(GroupQuerySchema, location='query')
@bp.output(GroupsPaginationSchema)
def list_groups(query: dict) -> dict:
search_name = query.get('name')
page = query.get('page')
per_page = query.get('per_page')
2022-06-12 17:45:45 +02:00
groups_query = Group.search_by_name(search_name)
response = paginate_models(page, groups_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
return {
"groups": response['items'],
"max_pages": response['max_pages']
}
2022-06-12 17:45:45 +02:00
@bp.route("/", methods=["POST"])
@bp.input(GroupCreateSchema)
@bp.output(MessageSchema)
def create_group(data: dict) -> dict:
name = data['name']
students_indexes = data['students']
2022-06-12 17:45:45 +02:00
project_supervisor_id = data['project_supervisor_id']
# can assign a new group to project_supervisor
result = db.session.query(ProjectSupervisor.count_groups - db.func.count(ProjectSupervisor.id)). \
join(Group).filter(ProjectSupervisor.id == project_supervisor_id). \
group_by(ProjectSupervisor.id).scalar()
if result is None:
abort(400, "Project Supervisor doesnt exist")
elif result <= 0:
abort(400, "Can't create new group, project supervisor achieved a limit of groups")
group = Group(name=name, project_supervisor_id=project_supervisor_id)
students_without_groups = db.session.query(Student).join(Group, isouter=True) \
.filter(Group.id.is_(None)).filter(Student.index.in_(students_indexes)).count()
2022-06-12 17:45:45 +02:00
if students_without_groups != len(students_indexes):
abort(400, "One or more students have already belonged to group!")
students = db.session.query(Student).filter(Student.index.in_(students_indexes)).all()
2022-06-12 17:45:45 +02:00
for student in students:
student.group_id = group.id
db.session.add_all(students)
2022-06-12 17:45:45 +02:00
db.session.add(group)
db.session.commit()
return {"message": "Project Supervisor was created!"}
@bp.route("/<int:id>/", methods=["GET"])
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!")
return group
@bp.route("/<int:id>/", methods=["DELETE"])
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!")
db.session.delete(group)
db.session.commit()
return {"message": "Group was deleted!"}
@bp.route("/<int:id>", methods=["PUT"])
@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-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-12 17:45:45 +02:00
group_query.update(group)
db.session.commit()
return {"message": "Group was updated!"}