Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move the pagination code to a helpers file
Roland Geider694db02f57e9
diff --git a/manager/templates/tags/pagination.html b/manager/templates/tags/pagination.htmlindex ff235fdec..ed005665f 100644--- a/manager/templates/tags/pagination.html+++ b/manager/templates/tags/pagination.html@@ -17,7 +17,7 @@{% endfor %}{% if page.has_next %}- <a href="?page={{ ingredients.next_page_number }}"+ <a href="?page={{ page.next_page_number }}"class="page gradient load-maincontent">{%trans "next" %}</a>{% endif %}</div>diff --git a/nutrition/views.py b/nutrition/views.pyindex 4c913c045..2e2521d3f 100644--- a/nutrition/views.py+++ b/nutrition/views.py@@ -55,6 +55,7 @@ from workout_manager import get_versionfrom workout_manager.constants import NUTRITION_TABfrom workout_manager.generic_views import YamlFormMixinfrom workout_manager.generic_views import YamlDeleteMixin+from workout_manager import helperslogger = logging.getLogger('workout_manager.custom')@@ -446,48 +447,12 @@ def ingredient_overview(request):# (the user can also want to see ingredients in English, see load_ingredient_languages)languages = load_ingredient_languages(request)- # Load the ingredients+ # Load the ingredients and paginate itingredients_list = Ingredient.objects.filter(language__in = languages)- # Show 25 ingredients per page- paginator = Paginator(ingredients_list, 25)- page = request.GET.get('page')- try:- ingredients = paginator.page(page)- except PageNotAnInteger:- # If page is not an integer, deliver first page.- ingredients = paginator.page(1)- except EmptyPage:- # If page is out of range (e.g. 9999), deliver last page of results.- ingredients = paginator.page(paginator.num_pages)-- # The English ingredient list has more than 8000 items, which results in over 300 pages.- # We muck around here to remove the ones not inmediately 'around' the current page- # TODO: move this to a file as a helper function- max_total_pages = 10- pages_around_current = 5-- if paginator.num_pages > max_total_pages:-- start_page = ingredients.number - pages_around_current- for i in range(ingredients.number - pages_around_current, ingredients.number +1):- if i > 0:- start_page = i- break-- end_page = ingredients.number + pages_around_current- for i in range(ingredients.number, ingredients.number + pages_around_current):- if i > paginator.num_pages:- end_page = i-- break-- page_range = range(start_page, end_page)- else:- page_range = paginator.page_range-- template_data['page_range'] = page_range- template_data['ingredients'] = ingredients+ pagination = helpers.pagination(ingredients_list, request.GET.get('page'))+ template_data['page_range'] = pagination['page_range']+ template_data['ingredients'] = pagination['page']return render_to_response('ingredient_overview.html',template_data,diff --git a/workout_manager/helpers.py b/workout_manager/helpers.pynew file mode 100644index 000000000..24c01aa3a--- /dev/null+++ b/workout_manager/helpers.py@@ -0,0 +1,68 @@+# -*- coding: utf-8 -*-++# This file is part of Workout Manager.+#+# Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger+++def pagination(object_list,+ request_page,+ paginator_class = Paginator,+ objects_per_page = 25,+ max_total_pages = 10,+ pages_around_current = 5):+ """+ Helper function to initialise the pagination.++ If the list is too long, only pages around the current one are shown.+ """++ paginator = paginator_class(object_list, objects_per_page)++ try:+ paginated_page = paginator.page(request_page)+ except PageNotAnInteger:+ # If page is not an integer, deliver first page.+ paginated_page = paginator.page(1)+ except EmptyPage:+ # If page is out of range (e.g. 9999), deliver last page of results.+ paginated_page = paginator.page(paginator.num_pages)+++ # For very long lists (e.g. the English ingredient with more than 8000 items)+ # we muck around here to remove the pages not inmediately 'around' the current+ # one, otherwise we end up with a which a useless block with 300 pages.+ if paginator.num_pages > max_total_pages:++ start_page = paginated_page.number - pages_around_current+ for i in range(paginated_page.number - pages_around_current, paginated_page.number +1):+ if i > 0:+ start_page = i+ break++ end_page = paginated_page.number + pages_around_current+ for i in range(paginated_page.number, paginated_page.number + pages_around_current):+ if i > paginator.num_pages:+ end_page = i++ break++ page_range = range(start_page, end_page)+ else:+ page_range = paginator.page_range++ # OK, return+ return({'page': paginated_page,+ 'page_range': page_range})