100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
from flask import abort
|
|
from apiflask import APIBlueprint
|
|
from flask_sqlalchemy import get_debug_queries
|
|
|
|
from ...students.models import Group, Student
|
|
from ...project_supervisor.models import ProjectSupervisor
|
|
from ..schemas import GroupSchema, GroupEditSchema, GroupsPaginationSchema, \
|
|
GroupCreateSchema, MessageSchema, FileSchema, GroupQuerySchema
|
|
from ...dependencies import db, ma
|
|
from ...base.utils import paginate_models
|
|
|
|
bp = APIBlueprint("groups", __name__, url_prefix="/groups")
|
|
|
|
|
|
@bp.route("/", methods=["GET"])
|
|
@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')
|
|
|
|
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']
|
|
}
|
|
|
|
|
|
@bp.route("/", methods=["POST"])
|
|
@bp.input(GroupCreateSchema)
|
|
@bp.output(MessageSchema)
|
|
def create_group(data: dict) -> dict:
|
|
name = data['name']
|
|
students = data['students']
|
|
project_supervisor_id = data['project_supervisor_id']
|
|
group = Group.query.filter_by(name=name).first()
|
|
if group is not None:
|
|
abort(400, "Group has already exists!")
|
|
|
|
project_supervisor = ProjectSupervisor.query.filter_by(id=project_supervisor_id).first()
|
|
|
|
if project_supervisor is None:
|
|
abort(404, f"Student with id {project_supervisor_id} doesn't exist!")
|
|
|
|
group = Group(name=name, project_supervisor_id = data['project_supervisor_id'])
|
|
|
|
for student in students:
|
|
st = Student.query(index = student.index).first()
|
|
if st is None:
|
|
abort(404, 'Not found student!')
|
|
st.group_id = group.id
|
|
|
|
db.session.add(group)
|
|
db.session.commit()
|
|
|
|
return {"message": "Student was created!"}
|
|
|
|
|
|
@bp.route("/<int:id>/", methods=["GET"])
|
|
@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"])
|
|
@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!')
|
|
|
|
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!")
|
|
|
|
group_query.update(group)
|
|
db.session.commit()
|
|
return {"message": "Group was updated!"}
|