Commits

Radim Novotny committed facb46f

Fixed pagination

Comments (0)

Files changed (3)

lfs_solr/templates/lfs/catalog/sorting.html

+{% load i18n %}
+<div class="sorting">
+    <form action="{% url lfs_catalog_set_sorting %}"
+          method="post">
+        {% csrf_token %}
+        <select name="sorting"
+                onchange="this.form.submit()">
+            <option value="best"
+                {% ifequal "best" current %}selected="selected"{% endifequal %}>{% trans 'Best result' %}</option>
+            <option value="price asc"
+                {% ifequal "price asc" current %}selected="selected"{% endifequal %}>{% trans 'Price ascending' %}</option>
+            <option value="price desc"
+                {% ifequal "price desc" current %}selected="selected"{% endifequal %}>{% trans 'Price descending' %}</option>
+            <option value="name asc"
+                {% ifequal "name asc" current %}selected="selected"{% endifequal %}>{% trans 'Name ascending' %}</option>
+            <option value="name desc"
+                {% ifequal "name desc" current %}selected="selected"{% endifequal %}>{% trans 'Name descending' %}</option>
+        </select>
+        <noscript>
+            <input type="submit" value="{% trans 'Go!' %}" />
+        </noscript>
+    </form>
+</div>
 
 urlpatterns = patterns('lfs_solr.views',
     url(r'^solr-set-filter', "set_filter", name="solr_set_filter"),
-    url(r'^solr-set-sorting', "set_sorting", name="solr_set_sorting"),
     url(r'^solr-reset-filter', "reset_filter", name="solr_reset_filter"),
     url(r'^solr-reset-field', "reset_field", name="solr_reset_field"),
     url(r'^index-products', "index_products", name="solr_index_products"),

lfs_solr/views.py

 from django.template.loader import render_to_string
 from django.template import RequestContext
 from django.utils import simplejson
+from django.core.paginator import Paginator, Page
 
 # lfs imports
 from lfs.catalog.models import Product
 from lfs_solr.utils import SOLR_ENABLED
 
 
+class SolrResults(object):
+    def __init__(self, objects, total, per_page):
+        self._total = total
+        self.objects = objects
+        self.per_page = per_page
+
+    def __len__(self):
+        return self._total
+
+    def __getitem__(self, index):
+        # since there is eg. 10 items in the list only,
+        # we return particular item as it was on correct position
+        return self.objects[index % self.per_page]
+
+
 @permission_required("manage_shop", login_url="/login/")
 def index_products(request):
     """Indexes full all products.
     return HttpResponseRedirect(reverse("solr_search") + "?q=" + q)
 
 
-def set_sorting(request):
-    """Saves the sorting to current session.
-    """
-    q = request.GET.get("q")
-    sorting = request.GET.get("sorting")
-
-    if sorting:
-        request.session["solr_sorting"] = sorting
-    else:
-        try:
-            del request.session["solr_sorting"]
-        except KeyError:
-            pass
-
-    return HttpResponseRedirect(reverse("solr_search") + "?q=" + q)
-
-
 def livesearch(request, template_name="lfs/search/livesearch_results.html"):
     """Renders the results for the live search.
     """
     q = request.GET.get("q")
 
     try:
-        start = int(request.GET.get("start", 0))
+        page = int(request.GET.get("page", 1))
     except ValueError:
-        start = 0
+        page = 1
 
     if q:
         conn = _get_solr_connection()
           'facet.field': ['categories', 'manufacturer'],
           'facet.mincount': 1,
           'rows': rows,
-          "start": start,
+          "start": (page - 1) * rows,
         }
 
         # Sorting
-        sorting = request.session.get("solr_sorting")
+        sorting = request.session.get("sorting")
         if sorting:
-            params["sort"] = sorting
+            # check validity of sort param. The session may contain old non-solr value
+            if sorting.startswith('-') or ' ' not in sorting:
+                del request.session['sorting']
+            else:
+                params["sort"] = sorting
 
         if request.session.has_key("solr_filter"):
             params["fq"] = []
             else:
                 products.append(product)
 
+        fake_results = SolrResults(products, results.hits, rows)
+
         # Facets
         categories = []
         temp = results.facets["facet_fields"]["categories"]
                     "amount": temp[i],
                 })
 
-        # Pagination
-        if start + rows < results.hits:
-            display_next = True
-            next_start = start + rows
-        else:
-            display_next = False
-            next_start = None
-
-        if start - rows >= 0:
-            display_previous = True
-            previous_start = start - rows
-        else:
-            display_previous = False
-            previous_start = None
+        paginator = Paginator(fake_results, rows)
+        page_obj = Page(fake_results, page, paginator)
 
         return render_to_response(template_name, RequestContext(request, {
             "products": products,
             "categories_reset": "categories" in request.session.get("solr_filter", []),
             "manufacturer_reset": "manufacturer" in request.session.get("solr_filter", []),
             "total": results.hits,
-            "display_next": display_next,
-            "next_start": next_start,
-            "previous_start": previous_start,
-            "display_next": display_next,
-            "display_previous": display_previous,
             "q": q,
             "sorting": sorting,
+            'paginator': paginator,
+            'page_obj': page_obj,
         }))
     else:
         return render_to_response(template_name, RequestContext(request, {}))