refactor code - update paginate_models function

This commit is contained in:
dominik24c 2022-06-13 18:04:30 +02:00
parent 5267e55925
commit bb070977f9
5 changed files with 15 additions and 27 deletions

View File

@ -1,6 +1,6 @@
from typing import TypedDict, Tuple from typing import TypedDict, Tuple
from flask import current_app from flask import current_app, abort
from flask_sqlalchemy import BaseQuery from flask_sqlalchemy import BaseQuery
from sqlalchemy import desc from sqlalchemy import desc
@ -20,14 +20,9 @@ def order_by_column_name(query: BaseQuery, model_field: str, order_by_col_name:
return query return query
def paginate_models(page: str, query: BaseQuery, per_page = 10) -> PaginationResponse or Tuple[dict, int]: def paginate_models(page: int, query: BaseQuery, per_page=10) -> PaginationResponse:
default_page = 1 default_page = 1
if page is not None: if page is not None:
try:
page = int(page)
except ValueError:
return {"error": f"Invalid page! Page must be integer!"}, 400
query = query.paginate(page=page, per_page=per_page, error_out=False) query = query.paginate(page=page, per_page=per_page, error_out=False)
else: else:
query = query.paginate(page=default_page, per_page=per_page, error_out=False) query = query.paginate(page=default_page, per_page=per_page, error_out=False)

View File

@ -22,12 +22,11 @@ def list_groups(query: dict) -> dict:
groups_query = Group.search_by_name(search_name) groups_query = Group.search_by_name(search_name)
response = paginate_models(page, groups_query, per_page) data = paginate_models(page, groups_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
return { return {
"groups": response['items'], "groups": data['items'],
"max_pages": response['max_pages'] "max_pages": data['max_pages']
} }

View File

@ -25,13 +25,11 @@ def list_project_supervisors(query: dict) -> dict:
project_supervisor_query = ProjectSupervisor.search_by_fullname_and_mode_and_order_by_first_name_or_last_name( project_supervisor_query = ProjectSupervisor.search_by_fullname_and_mode_and_order_by_first_name_or_last_name(
fullname, mode, order_by_first_name, order_by_last_name) fullname, mode, order_by_first_name, order_by_last_name)
response = paginate_models(page, project_supervisor_query, per_page) data = paginate_models(page, project_supervisor_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
# print(get_debug_queries()[0]) # print(get_debug_queries()[0])
return { return {
"project_supervisors": response['items'], "project_supervisors": data['items'],
"max_pages": response['max_pages'] "max_pages": data['max_pages']
} }

View File

@ -33,13 +33,11 @@ def list_students(query: dict) -> dict:
student_query = Student.search_by_fullname_and_mode_and_order_by_first_name_or_last_name( student_query = Student.search_by_fullname_and_mode_and_order_by_first_name_or_last_name(
fullname, mode, order_by_first_name, order_by_last_name) fullname, mode, order_by_first_name, order_by_last_name)
response = paginate_models(page, student_query, per_page) data = paginate_models(page, student_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
# print(get_debug_queries()[0]) # print(get_debug_queries()[0])
return { return {
"students": response['items'], "students": data['items'],
"max_pages": response['max_pages'] "max_pages": data['max_pages']
} }

View File

@ -24,16 +24,14 @@ def list_available_groups(query: dict) -> dict:
ps_query = ps_query.filter(ProjectSupervisor.mode == mode) ps_query = ps_query.filter(ProjectSupervisor.mode == mode)
ps_query = ps_query.group_by(Group.id) ps_query = ps_query.group_by(Group.id)
response = paginate_models(page, ps_query, per_page) data = paginate_models(page, ps_query, per_page)
if (message := response.get('message')) is not None:
abort(response['status_code'], message)
project_supervisors = [] project_supervisors = []
for project_supervisor, available_groups in response['items']: for project_supervisor, available_groups in data['items']:
setattr(project_supervisor, 'available_groups', available_groups) setattr(project_supervisor, 'available_groups', available_groups)
project_supervisors.append(project_supervisor) project_supervisors.append(project_supervisor)
return { return {
"project_supervisors": project_supervisors, "project_supervisors": project_supervisors,
"max_pages": response['max_pages'] "max_pages": data['max_pages']
} }