system-pri/backend/app/students/routes/year_group.py

33 lines
1.1 KiB
Python

from apiflask import APIBlueprint
from flask import abort
from ...students.models import YearGroup
from ...dependencies import db
from ...base.utils import paginate_models
from ..schemas import YearGroupQueryStudentSchema, YearGroupStudentPaginationSchema
from ..models import Student
bp = APIBlueprint("year_group", __name__, url_prefix="/year-group")
@bp.get('/')
@bp.input(YearGroupQueryStudentSchema, location='query')
@bp.output(YearGroupStudentPaginationSchema)
def list_of_year_groups(query: dict) -> dict:
index = query.get('index')
st = Student.query.filter(Student.index == index).first()
if st is None:
abort(404, "Not found student!")
####################################
page = query.get('page')
per_page = query.get('per_page')
year_group_query = YearGroup.query.join(YearGroup.students). \
filter(Student.index == st.index).order_by(db.desc(YearGroup.created_at))
data = paginate_models(page, year_group_query, per_page)
return {
"year_groups": data['items'],
"max_pages": data['max_pages']
}