Skip to content

Commit

Permalink
[1607] Fix for alpha pagination when providing unicode and no lookup …
Browse files Browse the repository at this point in the history
…attribute
  • Loading branch information
rossjones committed Feb 20, 2012
1 parent 7d4ee82 commit 34bc52a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ckan/lib/alphabet_paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
collection=query,
page=request.params.get('page', 'A'),
)
Template:
Template:
${c.page.pager()}
${package_list(c.page.items)}
${c.page.pager()}
Expand All @@ -21,7 +21,7 @@
from routes import url_for

class AlphaPage(object):
def __init__(self, collection, alpha_attribute, page, other_text, paging_threshold=50,
def __init__(self, collection, alpha_attribute, page, other_text, paging_threshold=50,
controller_name='tag'):
'''
@param collection - sqlalchemy query of all the items to paginate
Expand All @@ -34,7 +34,7 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho
start paginating them.
@param controller_name - The name of the controller that will be linked to,
which defaults to tag. The controller name should be the
same as the route so for some this will be the full
same as the route so for some this will be the full
controller name such as 'A.B.controllers.C:ClassName'
'''
self.collection = collection
Expand All @@ -47,8 +47,8 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho
for c in self.collection:
x = c if isinstance( c, unicode ) else getattr(c, self.alpha_attribute)[0]
self.available[x] = self.available.get(x, 0) + 1



def pager(self, q=None):
'''Returns pager html - for navigating between the pages.
Expand All @@ -72,10 +72,10 @@ def pager(self, q=None):
if self.available.get(letter, 0):
page_element = HTML.a(class_='pager_link', href=url_for(controller=self.controller_name, action='index', page=letter),c=letter)
else:
page_element = HTML.span(class_="pager_empty", c=letter)
page_element = HTML.span(class_="pager_empty", c=letter)
else:
page_element = HTML.span(class_='pager_curpage', c=letter)
pages.append(page_element)
pages.append(page_element)
div = HTML.tag('div', class_='pager', *pages)
return div

Expand All @@ -89,7 +89,7 @@ def items(self):
attribute = getattr(query.table.c,
self.alpha_attribute)
elif sqav.startswith("0.5"):
attribute = getattr(query._entity_zero().selectable.c,
attribute = getattr(query._entity_zero().selectable.c,
self.alpha_attribute)
else:
entity = getattr(query.column_descriptions[0]['expr'],
Expand All @@ -111,13 +111,15 @@ def items(self):
if self.item_count >= self.paging_threshold:
if self.page != self.other_text:
if isinstance(self.collection[0], dict):
items = [x for x in self.collection if x[self.alpha_attribute][0:1].lower() == self.page.lower()]
items = [x for x in self.collection if x[self.alpha_attribute][0:1].lower() == self.page.lower()]
elif isinstance(self.collection[0], unicode):
items = [x for x in self.collection if x[0:1].lower() == self.page.lower()]
else:
items = [x for x in self.collection if getattr(x,self.alpha_attribute)[0:1].lower() == self.page.lower()]
else:
# regexp search
if isinstance(self.collection[0], dict):
items = [x for x in self.collection if re.match('^[^a-zA-Z].*',x[self.alpha_attribute])]
items = [x for x in self.collection if re.match('^[^a-zA-Z].*',x[self.alpha_attribute])]
else:
items = [x for x in self.collection if re.match('^[^a-zA-Z].*',x)]
items.sort()
Expand Down

0 comments on commit 34bc52a

Please sign in to comment.