update create groups endpoint - change logic of counting groups and list available groups endpoint

This commit is contained in:
dominik24c 2022-06-13 22:58:52 +02:00
parent 980cf5da50
commit f6b53a84a3
3 changed files with 14 additions and 8 deletions

View File

@ -16,6 +16,7 @@ class Config:
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = f'sqlite:///{BASE_DIR / "db.sqlite"}'
LIMIT_STUDENTS_PER_GROUP = 5
DESCRIPTION = 'System PRI'
OPENAPI_VERSION = '3.0.2'

View File

@ -1,4 +1,4 @@
from flask import abort
from flask import abort, current_app
from apiflask import APIBlueprint
from flask_sqlalchemy import get_debug_queries
@ -40,16 +40,17 @@ def create_group(data: dict) -> dict:
project_supervisor = ProjectSupervisor.query.filter_by(id=project_supervisor_id).first()
limit_student_per_group = current_app.config.get('LIMIT_STUDENTS_PER_GROUP')
if project_supervisor is None:
abort(400, f"Project Supervisor with id {project_supervisor_id} doesnt exist")
elif project_supervisor.limit_group < len(students_indexes):
elif limit_student_per_group is not None and limit_student_per_group < len(students_indexes):
abort(400, f"Too much students you want add to group, "
f"this group can have only {project_supervisor.limit_group} students")
f"The group can have only {limit_student_per_group} students")
result = db.session.query(ProjectSupervisor.count_groups - db.func.count(ProjectSupervisor.id)).join(Group).filter(
limit = db.session.query(ProjectSupervisor.limit_group - db.func.count(ProjectSupervisor.id)).join(Group).filter(
ProjectSupervisor.id == project_supervisor_id).group_by(ProjectSupervisor.id).scalar()
if result is not None and result <= 0:
if limit is not None and limit <= 0:
abort(400, "Can't create new group, project supervisor achieved a limit of groups")
group = Group(name=name, project_supervisor_id=project_supervisor_id)
@ -66,6 +67,7 @@ def create_group(data: dict) -> dict:
students = db.session.query(Student).filter(Student.index.in_(students_indexes)).all()
for student in students:
student.group_id = group.id
project_supervisor.count_groups += 1
db.session.commit()

View File

@ -18,11 +18,14 @@ def list_available_groups(query: dict) -> dict:
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)
available_groups = (ProjectSupervisor.limit_group - ProjectSupervisor.count_groups)
ps_query = db.session.query(ProjectSupervisor, available_groups).join(Group)
if mode is not None:
ps_query = ps_query.filter(ProjectSupervisor.mode == mode)
ps_query = ps_query.group_by(Group.id)
ps_query = ps_query.group_by(ProjectSupervisor.id). \
having(available_groups > 0)
data = paginate_models(page, ps_query, per_page)