Commits

Kai Diefenbach committed 8e9d373 Merge

Merged 0.7 branch.

Comments (0)

Files changed (3)

docs/misc/contributing_translations.rst

 ========
 
 If your are a (Django-)developer you might want to fork django-lfs and
-django-lfstheme on `Bitbucket <https://bitbucket.org/diefenbach/django-lfs>`_ or
-`GitHub <https://github.com/diefenbach/django-lfs>`_, add the translations as
-`used to <https://docs.djangoproject.com/en/dev/topics/i18n/translation
-/#localization-how-to-create-language-files>`_, commit them and send us a pull
-request.
+django-lfstheme on `Bitbucket <https://bitbucket.org/diefenbach/django-lfs>`_
+add the translations as `used to <https://docs.djangoproject.com/en/dev/topics/i18n/translation/#localization-how-to-create-language-files>`_,
+commit them and send us a pull request.
+
+.. Note::
+
+    Please make sure to fork the correct version branch.
 
 Via Transifex
 =============
 your choice and start to translate via the web interface. Once you are ready we
 would add your translations to the source code.
 
+.. Note::
+
+    Please make sure to use the correct version.
+
 Other
 =====
 

lfs/catalog/views.py

 # django imports
 from django.conf import settings
 from django.core.cache import cache
+from django.core.paginator import Paginator, EmptyPage, InvalidPage
 from django.core.urlresolvers import reverse
 from django.http import Http404
 from django.http import HttpResponse
 from lfs.catalog.settings import PROPERTY_VALUE_TYPE_DEFAULT
 from lfs.catalog.settings import SELECT
 from lfs.catalog.settings import VARIANT
-from lfs.core.utils import LazyEncoder
+from lfs.core.utils import LazyEncoder, lfs_pagination
 from lfs.core.templatetags import lfs_tags
 from lfs.utils import misc as lfs_utils
 
 def category_view(request, slug, template_name="lfs/catalog/category_base.html"):
     """
     """
-    start = request.REQUEST.get("start", 0)
+    start = request.REQUEST.get("start", 1)
     category = lfs_get_object_or_404(Category, slug=slug)
     if category.get_content() == CONTENT_PRODUCTS:
         inline = category_products(request, slug, start)
     return result
 
 
-def category_products(request, slug, start=0, template_name="lfs/catalog/categories/product/default.html"):
+def category_products(request, slug, start=1, template_name="lfs/catalog/categories/product/default.html"):
     """Displays the products of the category with passed slug.
 
     This view is called if the user chooses a template that is situated in settings.PRODUCT_PATH ".
     try:
         start = int(start)
     except (ValueError, TypeError):
-        start = 0
+        start = 1
 
     format_info = category.get_format_info()
     amount_of_rows = format_info["product_rows"]
     all_products = lfs.catalog.utils.get_filtered_products_for_category(
         category, product_filter, price_filter, sorting)
 
+    # prepare paginator
+    paginator = Paginator(all_products, amount)
+
+    try:
+        current_page = paginator.page(start)
+    except (EmptyPage, InvalidPage):
+        current_page = paginator.page(paginator.num_pages)
+
     # Calculate products
     row = []
     products = []
-    for i, product in enumerate(all_products[start:start + amount]):
+    for i, product in enumerate(current_page.object_list):
         if product.is_product_with_variants():
             default_variant = product.get_variant_for_category(request)
             if default_variant:
     amount_of_products = all_products.count()
 
     # Calculate urls
-    pages = []
-    for i in range(0, amount_of_products / amount + 1):
-        page_start = i * amount
-        pages.append({
-            "name": i + 1,
-            "start": page_start,
-            "selected": start == page_start,
-        })
-
-    if (start + amount) < amount_of_products:
-        next_url = "%s?start=%s" % (category.get_absolute_url(), start + amount)
-    else:
-        next_url = None
-
-    if (start - amount) >= 0:
-        previous_url = "%s?start=%s" % (category.get_absolute_url(), start - amount)
-    else:
-        previous_url = None
+    pagination_data = lfs_pagination(request, current_page, url=category.get_absolute_url())
 
     render_template = category.get_template_name()
     if render_template != None:
     result = render_to_string(template_name, RequestContext(request, {
         "category": category,
         "products": products,
-        "next_url": next_url,
-        "previous_url": previous_url,
         "amount_of_products": amount_of_products,
-        "pages": pages,
-        "show_pages": amount_of_products > amount,
+        "pagination": pagination_data,
         "all_products": all_products,
     }))
 

lfs/core/utils.py

 import datetime
 import sys
 import urllib
+from collections import deque
+from itertools import count
 
 # django imports
 from django.conf import settings
             })
 
         return categories
+
+
+def define_page_range(current_page, total_pages, window=6):
+    """ Returns range of pages that contains current page and few pages before and after it.
+
+        @current_page - starts from 1
+        @tota_pages - total number of pages
+        @window - maximum number of pages shown with current page - should be even
+
+        Examples (cucumber style):
+             Given window = 6
+             When current_page is 8
+             and total_pages = 20
+             Then I should see: 5 6 7 [8] 9 10 11
+
+             Given window = 6
+             When current_page is 8
+             and total_pages = 9
+             Then I should see: 3 4 5 6 7 [8] 9
+
+             Given window = 6
+             When current_page is 1
+             and total_pages = 9
+             Then I should see: [1] 2 3 4 5 6 7
+    """
+    # maximum length of page range is window + 1
+    maxlen = window + 1
+    page_range = deque(maxlen=maxlen)
+
+    # minimum possible index is either: (current_page - window) or 1
+    window_start = (current_page - window) if (current_page - window) > 0 else 1
+
+    # maximum possible index is current_page + window or total_pages
+    window_end = total_pages if (current_page + window) > total_pages else (current_page + window)
+
+    # if we have enough pages then we should end at preffered end
+    preffered_end = current_page + int(window / 2.0)
+
+    for i in count(window_start):
+        if i > window_end:
+            # if we're on first page then our window will be [1] 2 3 4 5 6 7
+            break
+        elif i > preffered_end and len(page_range) == maxlen:
+            # if we have enough pages already then stop at preffered_end
+            break
+        page_range.append(i)
+    return list(page_range)
+
+
+def lfs_pagination(request, current_page, url='', getparam='start'):
+    """Prepare data for pagination
+
+       @page - number of current page (starting from 1)
+       @paginator - paginator object, eg. Paginator(contact_list, 25)
+    """
+    paginator = current_page.paginator
+    current_page_no = current_page.number
+
+    has_next = current_page.has_next()
+    has_prev = current_page.has_previous()
+
+    page_range = define_page_range(current_page.number, paginator.num_pages)
+
+    first = 1
+    last = paginator.num_pages
+
+    if first in page_range:
+        first = None
+
+    if last in page_range:
+        last = None
+
+    to_return = {'page_range': page_range,
+                 'current_page': current_page_no,
+                 'total_pages': paginator.num_pages,
+                 'has_next': has_next,
+                 'has_prev': has_prev,
+                 'next': current_page_no + 1,
+                 'prev': current_page_no - 1,
+                 'url': url,
+                 'getparam': getparam,
+                 'first_page': first,
+                 'last_page': last,
+                 'getvars': ''
+                }
+
+    getvars = request.GET.copy()
+    if getparam in getvars:
+        del getvars[getparam]
+    if len(getvars.keys()) > 0:
+        to_return['getvars'] = "&%s" % getvars.urlencode()
+    return to_return