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

40 lines
1.4 KiB
Python

from apiflask import APIBlueprint
from flask import abort
from ...project_supervisor.models import ProjectSupervisor
from ..models import Group
from ...dependencies import db
from ..schemas import ProjectSupervisorPaginationSchema, ProjectSupervisorQuerySchema
from ...base.utils import paginate_models
bp = APIBlueprint("registrations", __name__, url_prefix="/registrations")
@bp.route('/', methods=['GET'])
@bp.input(ProjectSupervisorQuerySchema, location='query')
@bp.output(ProjectSupervisorPaginationSchema)
def list_available_groups(query: dict) -> dict:
mode = query.get('mode')
page = query.get('page')
per_page = query.get('per_page')
ps_query = db.session.query(ProjectSupervisor,
(ProjectSupervisor.count_groups - db.func.count(Group.id))).join(Group)
if mode is not None:
ps_query = ps_query.filter(ProjectSupervisor.mode == mode)
ps_query = ps_query.group_by(Group.id)
response = paginate_models(page, ps_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
project_supervisors = []
for project_supervisor, available_groups in response['items']:
setattr(project_supervisor, 'available_groups', available_groups)
project_supervisors.append(project_supervisor)
return {
"project_supervisors": project_supervisors,
"max_pages": response['max_pages']
}