Skip to content

Commit

Permalink
[xs] Fix for alpha pagination when using strings rather than objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Feb 14, 2012
1 parent 45ac984 commit db71e30
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions ckan/lib/alphabet_paginate.py
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 @@ -45,10 +45,10 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho
self.controller_name = controller_name
self.available = dict( (c,0,) for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
for c in self.collection:
x = c if isinstance( c, unicode ) else getattr(c, self.alpha_attribute)[0]
x = c[0] 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 db71e30

Please sign in to comment.