Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions solr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def __str__(self):
# ===================================================================
# Connection Object
# ===================================================================
class SolrConnection:
class SolrConnection(object):
"""
Represents a Solr connection.

Expand Down Expand Up @@ -326,14 +326,14 @@ def __init__(self, url,
self.ssl_key = ssl_key
self.ssl_cert = ssl_cert
self.max_retries = int(max_retries)

assert self.max_retries >= 0

kwargs = {}

if self.timeout and _python_version >= 2.6 and _python_version < 3:
kwargs['timeout'] = self.timeout

if self.scheme == 'https':
self.conn = httplib.HTTPSConnection(self.host,
key_file=ssl_key, cert_file=ssl_cert, **kwargs)
Expand All @@ -345,7 +345,7 @@ def __init__(self, url,

# Responses from Solr will always be in UTF-8
self.decoder = codecs.getdecoder('utf-8')

# Set timeout, if applicable.
if self.timeout and _python_version < 2.6:
self.conn.connect()
Expand Down Expand Up @@ -469,7 +469,7 @@ def query(self, q, fields=None, highlight=None,
xml = StringIO(rsp.read())
if self.debug:
logging.info("solrpy got response: %s" % xml.getvalue())

data = parse_query_response(xml, params=params, connection=self)

finally:
Expand Down
54 changes: 27 additions & 27 deletions solr/paginator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import math

class SolrPaginator:
class SolrPaginator(object):
"""
Create a Django-like Paginator for a solr response object. Can be handy
when you want to hand off a Paginator and/or Page to a template to
when you want to hand off a Paginator and/or Page to a template to
display results, and provide links to next page, etc.

For example:
Expand All @@ -15,33 +15,33 @@ class SolrPaginator:
>>> print paginator.num_pages
>>> page = paginator.get_page(5)

For more details see the Django Paginator documentation and solrpy
For more details see the Django Paginator documentation and solrpy
unittests.

http://docs.djangoproject.com/en/dev/topics/pagination/

"""

def __init__(self, result, default_page_size=None):
self.params = result.header['params']
self.result = result
self.conn = result._connection

if 'rows' in self.params:
self.page_size = int(self.params['rows'])
elif default_page_size:
try:
self.page_size = int(default_page_size)
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')

else:
self.page_size = len(self.result.results)

@property
def count(self):
return int(self.result.numFound)
Expand All @@ -51,7 +51,7 @@ def num_pages(self):
if self.count == 0:
return 0
return int(math.ceil(float(self.count) / float(self.page_size)))

@property
def page_range(self):
"""List the index numbers of the available result pages."""
Expand All @@ -63,70 +63,70 @@ def page_range(self):
def _fetch_page(self, start=0):
"""Retrieve a new result response from Solr."""
# need to convert the keys to strings to pass them as parameters
new_params = {}
new_params = {}
for k, v in self.params.items():
new_params[str(k)] = v.encode('utf-8')

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

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

if page_num not in self.page_range:
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)
return SolrPage(new_result.results, page_num, self)


class SolrPage:

class SolrPage(object):
"""A single Paginator-style page."""

def __init__(self, result, page_num, paginator):
self.result = result
self.number = page_num
self.paginator = paginator

@property
def object_list(self):
return self.result

def has_next(self):
if self.number < self.paginator.num_pages:
return True
return False

def has_previous(self):
if self.number > 1:
return True
return False

def has_other_pages(self):
if self.paginator.num_pages > 1:
return True
return False

def start_index(self):
# off by one because self.number is 1-based w/django,
# but start is 0-based in solr
# but start is 0-based in solr
return (self.number - 1) * self.paginator.page_size

def end_index(self):
# off by one because we want the last one in this set,
# off by one because we want the last one in this set,
# not the next after that, to match django paginator
return self.start_index() + len(self.result) - 1

def next_page_number(self):
return self.number + 1

def previous_page_number(self):
return self.number - 1