2022-06-12 22:20:10 +02:00
|
|
|
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:
|
2022-06-14 01:34:50 +02:00
|
|
|
mode = 0 if query.get('mode') else 1
|
2022-06-12 22:20:10 +02:00
|
|
|
page = query.get('page')
|
|
|
|
per_page = query.get('per_page')
|
|
|
|
|
2022-06-13 22:58:52 +02:00
|
|
|
available_groups = (ProjectSupervisor.limit_group - ProjectSupervisor.count_groups)
|
2022-06-14 01:34:50 +02:00
|
|
|
ps_query = db.session.query(ProjectSupervisor, available_groups).join(Group, isouter=True)
|
2022-06-13 22:58:52 +02:00
|
|
|
|
2022-06-12 22:20:10 +02:00
|
|
|
if mode is not None:
|
2022-06-14 01:34:50 +02:00
|
|
|
ps_query = ps_query.filter(ProjectSupervisor.mode != 1-mode)
|
2022-06-13 22:58:52 +02:00
|
|
|
|
2022-06-23 00:49:22 +02:00
|
|
|
ps_query = ps_query.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
|
|
|
}
|