Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use common paginate function

Adam Bogdał75a4970b2621
diff --git a/saleor/core/templates/bootstrap/_pagination.html b/saleor/core/templates/bootstrap/_pagination.html
index fc85179695..6c4ed62a7e 100644
--- a/saleor/core/templates/bootstrap/_pagination.html
+++ b/saleor/core/templates/bootstrap/_pagination.html
@@ -3,8 +3,8 @@
<nav>
<ul class="pagination">
<li {% if not items.has_previous %}class="disabled"{% endif %}><a href="{% if items.has_previous%}?page={{ items.previous_page_number }}{% else %}#{% endif %}" aria-label="{% trans "Previous" %}"><span aria-hidden="true">&laquo;</span></a></li>
- {% for page in pages %}
- {% if page == current_page %}
+ {% for page in items.paginator.page_range %}
+ {% if page == items.number %}
<li class="active"><a href="#">{{ page }} <span class="sr-only">(current)</span></a></li>
{% else %}
<li><a href="?page={{ page }}">{{ page }}</a></li>
diff --git a/saleor/core/templatetags/bootstrap.py b/saleor/core/templatetags/bootstrap.py
index ddc8674ba7..c60f5be410 100644
--- a/saleor/core/templatetags/bootstrap.py
+++ b/saleor/core/templatetags/bootstrap.py
@@ -42,13 +42,6 @@ def render_field(bound_field, show_label, template):
return render_to_string(template, context_instance=context)
-def render_pagination(items, current_page):
- context = Context({
- 'items': items, 'current_page': current_page,
- 'pages': map(str, range(1, items.paginator.num_pages + 1))})
- return render_to_string(TEMPLATE_PAGINATION, context_instance=context)
-
-
def as_bootstrap(obj, show_label, template):
if isinstance(obj, BoundField):
return render_field(obj, show_label, template)
@@ -81,7 +74,7 @@ def render_widget(obj, **attrs):
return obj.as_widget(attrs=attrs)
[email protected]_tag(takes_context=True)
-def pagination(context, items):
- current_page = context['request'].GET.get('page', '1')
- return render_pagination(items, current_page)
+def as_pagination(items):
+ context = Context({'items': items})
+ return render_to_string(TEMPLATE_PAGINATION, context_instance=context)
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 327159e8d3..2a7e8ff9c9 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -3,7 +3,6 @@ from __future__ import unicode_literals
import re
from django import forms
-from django.conf import settings
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.template.response import TemplateResponse
from django.utils.encoding import iri_to_uri, smart_text
@@ -85,8 +84,8 @@ def build_absolute_uri(location, is_secure=False):
return iri_to_uri(location)
-def get_paginator_items(items, page):
- paginator = Paginator(items, settings.PAGINATE_BY)
+def get_paginator_items(items, paginate_by, page):
+ paginator = Paginator(items, paginate_by)
try:
items = paginator.page(page)
except PageNotAnInteger:
diff --git a/saleor/dashboard/customer/views.py b/saleor/dashboard/customer/views.py
index 46f9d779bc..c67a5408cf 100644
--- a/saleor/dashboard/customer/views.py
+++ b/saleor/dashboard/customer/views.py
@@ -6,8 +6,8 @@ from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
from .forms import CustomerSearchForm
-from ..utils import paginate
from ..views import staff_member_required
+from ...core.utils import get_paginator_items
from ...userprofile.models import User
@@ -30,9 +30,9 @@ def customer_list(request):
orders__status__in=['new', 'payment-pending', 'fully-paid'])
title = _('Results (%s)') % len(customers)
- customers, paginator = paginate(customers, 30, request.GET.get('page'))
+ customers = get_paginator_items(customers, 30, request.GET.get('page'))
ctx = {'customers': customers, 'form': form, 'title': title,
- 'paginator': paginator, 'default_pagination_params': form_values}
+ 'default_pagination_params': form_values}
return TemplateResponse(request, 'dashboard/customer/list.html', ctx)
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 912680882e..99966c4366 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -7,9 +7,9 @@ from django.utils.http import is_safe_url
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.http import require_http_methods
+from ...core.utils import get_paginator_items
from ...product.models import Product, ProductImage, Stock, ProductAttribute, \
ProductVariant
-from ..utils import paginate
from ..views import staff_member_required
from . import forms
@@ -20,8 +20,8 @@ def product_list(request):
form = forms.ProductClassForm(request.POST or None)
if form.is_valid():
return redirect('dashboard:product-add')
- products, paginator = paginate(products, 30, request.GET.get('page'))
- ctx = {'form': form, 'products': products, 'paginator': paginator}
+ products = get_paginator_items(products, 30, request.GET.get('page'))
+ ctx = {'form': form, 'products': products}
return TemplateResponse(request, 'dashboard/product/list.html', ctx)
diff --git a/saleor/dashboard/templates/dashboard/includes/_pagination.html b/saleor/dashboard/templates/dashboard/includes/_pagination.html
index 9db2875318..4d35d00c02 100644
--- a/saleor/dashboard/templates/dashboard/includes/_pagination.html
+++ b/saleor/dashboard/templates/dashboard/includes/_pagination.html
@@ -1,6 +1,6 @@
{% load i18n %}
{% load utils %}
-{% if paginator.num_pages > 1 %}
+{% if page_obj.paginator.num_pages > 1 %}
<ul class="pagination">
<li{% if not page_obj.has_previous %} class="disabled"{% endif %}>
{% if page_obj.has_previous %}
@@ -11,7 +11,7 @@
<span><i class="mdi-navigation-chevron-left"></i></span>
{% endif %}
</li>
- {% for page in paginator.page_range %}
+ {% for page in page_obj.paginator.page_range %}
<li class="{% if page == page_obj.number %}active{% else %}waves-effect{% endif %}">
<a href="{% construct_get_query page=page %}">
{{ page }}
diff --git a/saleor/dashboard/utils.py b/saleor/dashboard/utils.py
deleted file mode 100644
index f972793f6b..0000000000
--- a/saleor/dashboard/utils.py
+++ /dev/null
@@ -1,12 +0,0 @@
-from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
-
-
-def paginate(queryset, paginate_by, page=None):
- paginator = Paginator(queryset, paginate_by)
- try:
- queryset = paginator.page(page)
- except PageNotAnInteger:
- queryset = paginator.page(1)
- except EmptyPage:
- queryset = paginator.page(paginator.num_pages)
- return queryset, paginator
diff --git a/saleor/product/templates/category/index.html b/saleor/product/templates/category/index.html
index 99ce934e98..e9ba52a68c 100644
--- a/saleor/product/templates/category/index.html
+++ b/saleor/product/templates/category/index.html
@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% load slice from shop %}
{% load static from staticfiles %}
-{% load pagination from bootstrap %}
+{% load as_pagination from bootstrap %}
{% block title %}{{ category }} — {{ block.super }}{% endblock %}
@@ -40,7 +40,7 @@
<div class="row">
{% include "category/_items.html" %}
</div>
- {% pagination products %}
+ {{ products|as_pagination }}
</div>
</div>
{% endblock content %}
diff --git a/saleor/product/views.py b/saleor/product/views.py
index d8fa763e37..308edf51b8 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -1,6 +1,7 @@
from __future__ import unicode_literals
from django.http import HttpResponsePermanentRedirect
+from django.conf import settings
from django.contrib import messages
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
@@ -48,7 +49,8 @@ def category_index(request, path, category_id):
category_id=category_id)
products = category.products.get_available_products().select_subclasses()
products = products.prefetch_related('images', 'variants', 'variants__stock')
- products = get_paginator_items(products, request.GET.get('page'))
+ products = get_paginator_items(
+ products, settings.PAGINATE_BY, request.GET.get('page'))
return TemplateResponse(
request, 'category/index.html',
{'products': products, 'category': category,