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

38 lines
1.4 KiB
Python

from apiflask import APIBlueprint
from ...project_supervisor.models import ProjectSupervisor, YearGroupProjectSupervisors
from ..models import Group
from ...dependencies import db
from ..schemas import ProjectSupervisorQuerySchema, ProjectSupervisorPaginationSchema
from ...base.utils import paginate_models
bp = APIBlueprint("registrations", __name__, url_prefix="/registrations")
@bp.get('/<int:year_group_id>/')
@bp.input(ProjectSupervisorQuerySchema, location='query')
@bp.output(ProjectSupervisorPaginationSchema)
def list_available_groups(year_group_id: int, query: dict) -> dict:
page = query.get('page')
per_page = query.get('per_page')
available_groups = (YearGroupProjectSupervisors.limit_group - db.func.count(Group.id))
ps_query = db.session. \
query(ProjectSupervisor, available_groups). \
join(Group, isouter=True). \
join(YearGroupProjectSupervisors, isouter=True). \
filter(YearGroupProjectSupervisors.year_group_id == year_group_id).\
group_by(ProjectSupervisor.id)
data = paginate_models(page, ps_query, per_page)
project_supervisors = []
for project_supervisor, available_groups in data['items']:
setattr(project_supervisor, 'available_groups', available_groups)
project_supervisors.append(project_supervisor)
return {
"project_supervisors": project_supervisors,
"max_pages": data['max_pages']
}