Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add pagination to the ingredient overview

Roland Geiderc90017565e1b
diff --git a/manager/static/css/workout-manager.css b/manager/static/css/workout-manager.css
index 1cc5699e4..1afd9e6f7 100644
--- a/manager/static/css/workout-manager.css
+++ b/manager/static/css/workout-manager.css
@@ -198,3 +198,49 @@ div.warning a, div.warning a:hover, div.warning a:active{
margin: .8em 0 .2em;
line-height: 1.5;
}
+
+
+/*
+ *
+ * Pagination
+ *
+ * See http://cssdeck.com/labs/css-pagination-styles
+ */
+.pagination a:hover
+{
+ color:black;
+}
+
+.pagination .page {
+ display: inline-block;
+ padding: 0px 9px;
+ margin-right: 4px;
+ border-radius: 3px;
+ border: solid 1px #c0c0c0;
+ background: #e9e9e9;
+ box-shadow: inset 0px 1px 0px rgba(255,255,255, .8), 0px 1px 3px rgba(0,0,0, .1);
+ font-size: .875em;
+ font-weight: bold;
+ text-decoration: none;
+ color: #717171;
+ text-shadow: 0px 1px 0px rgba(255,255,255, 1);
+}
+
+.pagination .page:hover, .pagination .page.gradient:hover {
+ background: #fefefe;
+ background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#FEFEFE), to(#f0f0f0));
+ background: -moz-linear-gradient(0% 0% 270deg,#FEFEFE, #f0f0f0);
+}
+
+.pagination .page.active {
+ border: none;
+ background: #616161;
+ box-shadow: inset 0px 0px 8px rgba(0,0,0, .5), 0px 1px 0px rgba(255,255,255, .8);
+ color: #f0f0f0;
+ text-shadow: 0px 0px 3px rgba(0,0,0, .5);
+}
+
+.pagination .page.gradient {
+ background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#f8f8f8), to(#e9e9e9));
+ background: -moz-linear-gradient(0% 0% 270deg,#f8f8f8, #e9e9e9);
+}
\ No newline at end of file
diff --git a/nutrition/templates/ingredient_overview.html b/nutrition/templates/ingredient_overview.html
index 3efe5f6b3..44a7ef439 100644
--- a/nutrition/templates/ingredient_overview.html
+++ b/nutrition/templates/ingredient_overview.html
@@ -22,6 +22,26 @@
</div>
{% endif %}
+
+<div style="padding-top:3em;"></div>
+<div class="pagination">
+ {% if ingredients.has_previous %}
+ <a href="?page={{ ingredients.previous_page_number }}" class="page gradient">{% trans "previous" %}</a>
+ {% endif %}
+
+ {% for i in ingredients.paginator.page_range %}
+ {% if ingredients.number != i %}
+ <a href="?page={{ i }}" class="page gradient">{{i}}</a>
+ {% else %}
+ <span class="page active">{{i}}</span>
+ {% endif %}
+ {% endfor %}
+
+ {% if ingredients.has_next %}
+ <a href="?page={{ ingredients.next_page_number }}" class="page gradient">{%trans "next" %}</a>
+ {% endif %}
+</div>
+
{% endblock %}
{% block sidebar %}
diff --git a/nutrition/views.py b/nutrition/views.py
index f4834c89a..53fc90893 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -25,16 +25,19 @@ from django.http import HttpResponseForbidden
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
+from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required
from django.forms import ModelForm
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
+
from django.views.generic import DeleteView
from django.views.generic import CreateView
from django.views.generic import UpdateView
+
from nutrition.models import NutritionPlan
from nutrition.models import Meal
from nutrition.models import MealItem
@@ -444,7 +447,20 @@ def ingredient_overview(request):
languages = load_ingredient_languages(request)
# Load the ingredients
- ingredients = Ingredient.objects.filter(language__in = languages)
+ ingredients_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)
+
template_data['ingredients'] = ingredients
return render_to_response('ingredient_overview.html',