Skip to content

Commit

Permalink
fix bug it can't work pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
key committed Jan 2, 2013
1 parent 76eb481 commit 5b5f14d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.txt → README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Solr API for Python
===================

Forked solrpy for correct django pagination.

``solrpy`` is a Python client for Solr_, an enterprise search server
built on top of Lucene_. ``solrpy`` allows you to add documents to a
Solr instance, and then to perform queries and gather search results
Expand Down
34 changes: 23 additions & 11 deletions solr/paginator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import math
from django.core import paginator


class PageNotAnInteger(paginator.PageNotAnInteger):
pass


class EmptyPage(paginator.EmptyPage):
pass


class SolrPaginator:
"""
Expand All @@ -23,7 +33,7 @@ class SolrPaginator:
"""

def __init__(self, result, default_page_size=None):
self.params = result.header['params']
self.params = result._params
self.result = result
self.query = result._query

Expand All @@ -35,10 +45,11 @@ def __init__(self, result, default_page_size=None):
except ValueError:
raise ValueError('default_page_size must be an integer')

if self.page_size < len(self.result.results):
raise ValueError('Invalid default_page_size specified, lower '
'than number of results')

#if self.page_size < len(self.result.results):
# raise ValueError('Invalid default_page_size specified, lower '
# 'than number of results')
if self.page_size < 0:
raise ValueError('default_page_size must be a positive value')
else:
self.page_size = len(self.result.results)

Expand All @@ -60,30 +71,31 @@ def page_range(self):
# Add one because range is right-side exclusive
return range(1, self.num_pages + 1)

def _fetch_page(self, start=0):
def _fetch_page(self, start=0, rows=10):
"""Retrieve a new result response from Solr."""
# need to convert the keys to strings to pass them as parameters
new_params = {}
for k, v in self.params.items():
new_params[str(k)] = v.encode('utf-8')
new_params[str(k)] = str(v).encode('utf-8')

# get the new start index
new_params['start'] = start
new_params['rows'] = rows
return self.query(**new_params)

def page(self, page_num=1):
"""Return the requested Page object"""
try:
int(page_num)
page_num = int(page_num)
except:
raise 'PageNotAnInteger'
raise PageNotAnInteger

if page_num not in self.page_range:
raise 'EmptyPage', 'That page does not exist.'
raise EmptyPage('That page does not exist.')

# Page 1 starts at 0; take one off before calculating
start = (page_num - 1) * self.page_size
new_result = self._fetch_page(start=start)
new_result = self._fetch_page(start=start, rows=self.page_size)
return SolrPage(new_result.results, page_num, self)


Expand Down

0 comments on commit 5b5f14d

Please # to comment.