From db71e3072786a6718ecbe28130d89165aa96513e Mon Sep 17 00:00:00 2001 From: Ross Jones Date: Tue, 14 Feb 2012 10:34:13 +0000 Subject: [PATCH] [xs] Fix for alpha pagination when using strings rather than objects --- ckan/lib/alphabet_paginate.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/ckan/lib/alphabet_paginate.py b/ckan/lib/alphabet_paginate.py index 321c9b6d861..f30f5f9c78d 100644 --- a/ckan/lib/alphabet_paginate.py +++ b/ckan/lib/alphabet_paginate.py @@ -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()} @@ -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 @@ -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 @@ -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. @@ -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 @@ -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'], @@ -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()