2022-11-12 16:18:07 +01:00
|
|
|
from apiflask import APIBlueprint
|
2023-01-14 17:38:03 +01:00
|
|
|
from flask import abort
|
2022-11-12 16:18:07 +01: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 ...students.models import YearGroup
|
|
|
|
from ..schemas.year_group import (
|
|
|
|
YearGroupPaginationSchema,
|
|
|
|
YearGroupQuerySchema,
|
|
|
|
YearGroupSchema,
|
|
|
|
)
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
bp = APIBlueprint("year_group", __name__, url_prefix="/year-group")
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.post("/")
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.input(YearGroupSchema)
|
2023-01-06 22:35:47 +01:00
|
|
|
@bp.output(MessageSchema, status_code=201)
|
2022-11-12 16:18:07 +01:00
|
|
|
def create_year_group(data: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
name = data["name"]
|
|
|
|
mode = data["mode"]
|
|
|
|
year_group = YearGroup.query.filter(
|
|
|
|
YearGroup.name == name, YearGroup.mode == mode
|
|
|
|
).first()
|
2022-11-12 16:18:07 +01:00
|
|
|
if year_group is not None:
|
|
|
|
abort(400, "Year group has already exists!")
|
|
|
|
|
|
|
|
yg = YearGroup(**data)
|
|
|
|
db.session.add(yg)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {"message": "Year group was created!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.get("/")
|
|
|
|
@bp.input(YearGroupQuerySchema, location="query")
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.output(YearGroupPaginationSchema)
|
|
|
|
def list_of_year_groups(query: dict) -> dict:
|
2023-01-14 17:38:03 +01:00
|
|
|
page = query.get("page")
|
|
|
|
per_page = query.get("per_page")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
year_group_query = YearGroup.query.order_by(db.desc(YearGroup.created_at))
|
|
|
|
data = paginate_models(page, year_group_query, per_page)
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
return {"year_groups": data["items"], "max_pages": data["max_pages"]}
|
2022-11-12 16:18:07 +01:00
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.put("/<int:id>/")
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.input(YearGroupSchema)
|
|
|
|
@bp.output(MessageSchema)
|
|
|
|
def update_year_of_group(id: int, data: dict) -> dict:
|
|
|
|
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
|
|
|
|
|
|
|
year_group_query = YearGroup.query.filter(YearGroup.id == id)
|
|
|
|
year_group = year_group_query.first()
|
|
|
|
|
|
|
|
if year_group is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Not found year group!")
|
2022-11-12 16:18:07 +01:00
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
name = data["name"]
|
|
|
|
mode = data["mode"]
|
|
|
|
year_group = YearGroup.query.filter(
|
|
|
|
YearGroup.name == name, YearGroup.mode == mode, YearGroup.id != id
|
|
|
|
).first()
|
2022-11-17 10:21:36 +01:00
|
|
|
if year_group is not None:
|
|
|
|
abort(400, "Year group has already exists!")
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
year_group_query.update(data)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {"message": "Year group was updated!"}
|
|
|
|
|
|
|
|
|
2023-01-14 17:38:03 +01:00
|
|
|
@bp.delete("/<int:id>/")
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.output(MessageSchema, status_code=202)
|
|
|
|
def delete_year_of_group(id: int) -> dict:
|
|
|
|
year_group = YearGroup.query.filter_by(id=id).first()
|
|
|
|
if year_group is None:
|
2023-01-14 17:38:03 +01:00
|
|
|
abort(404, "Year group doesn't exist!")
|
2022-11-12 16:18:07 +01:00
|
|
|
db.session.delete(year_group)
|
|
|
|
db.session.commit()
|
|
|
|
return {"message": "Year group was deleted!"}
|