2022-06-12 22:20:10 +02:00
|
|
|
from apiflask import APIBlueprint
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
from ...project_supervisor.models import ProjectSupervisor, YearGroupProjectSupervisors
|
2022-06-12 22:20:10 +02:00
|
|
|
from ..models import Group
|
|
|
|
from ...dependencies import db
|
2022-10-25 20:38:47 +02:00
|
|
|
from ..schemas import ProjectSupervisorQuerySchema, ProjectSupervisorPaginationSchema
|
2022-06-12 22:20:10 +02:00
|
|
|
from ...base.utils import paginate_models
|
|
|
|
|
|
|
|
bp = APIBlueprint("registrations", __name__, url_prefix="/registrations")
|
|
|
|
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
@bp.get('/<int:year_group_id>/')
|
2022-06-12 22:20:10 +02:00
|
|
|
@bp.input(ProjectSupervisorQuerySchema, location='query')
|
|
|
|
@bp.output(ProjectSupervisorPaginationSchema)
|
2022-11-12 16:18:07 +01:00
|
|
|
def list_available_groups(year_group_id: int, query: dict) -> dict:
|
2022-06-12 22:20:10 +02:00
|
|
|
page = query.get('page')
|
|
|
|
per_page = query.get('per_page')
|
|
|
|
|
2022-11-12 16:18:07 +01:00
|
|
|
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)
|
2022-06-12 22:20:10 +02:00
|
|
|
|
2022-06-13 18:04:30 +02:00
|
|
|
data = paginate_models(page, ps_query, per_page)
|
2022-06-12 22:20:10 +02:00
|
|
|
|
|
|
|
project_supervisors = []
|
2022-06-13 18:04:30 +02:00
|
|
|
for project_supervisor, available_groups in data['items']:
|
2022-06-12 22:20:10 +02:00
|
|
|
setattr(project_supervisor, 'available_groups', available_groups)
|
|
|
|
project_supervisors.append(project_supervisor)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"project_supervisors": project_supervisors,
|
2022-06-13 18:04:30 +02:00
|
|
|
"max_pages": data['max_pages']
|
2022-06-12 22:20:10 +02:00
|
|
|
}
|