Skip to content

Commit

Permalink
facets show all functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed May 23, 2012
1 parent 9dbaef2 commit 7fc29e1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
14 changes: 12 additions & 2 deletions ckan/controllers/package.py
Expand Up @@ -135,18 +135,20 @@ def drill_down_url(alternative_url=None, **by):

c.drill_down_url = drill_down_url

def remove_field(key, value=None):
def remove_field(key, value=None, replace=None):
"""
Remove a key from the current search parameters. A specific
key/value pair can be removed by passing a second value argument
otherwise all pairs matching the key will be removed.
If replace is given then a new param key=replace will be added.
"""
params = list(params_nopage)
if value:
params.remove((key, value))
else:
[params.remove((k, v)) for (k, v) in params[:] if k==key]

if replace is not None:
params.append((key, replace))
return search_url(params)

c.remove_field = remove_field
Expand Down Expand Up @@ -226,6 +228,14 @@ def pager_url(q=None, page=None):
c.query_error = True
c.facets = {}
c.page = h.Page(collection=[])
c.search_facets_limits = {}
for facet in c.facets.keys():
limit = int(request.params.get('_%s_limit' % facet, 10))
c.search_facets_limits[facet] = limit
c.facet_titles = {'groups' : _('Groups'),
'tags' : _('Tags'),
'res_format' : _('Formats'),
'license' : _('Licence'), }

return render( self._search_template(package_type) )

Expand Down
8 changes: 7 additions & 1 deletion ckan/lib/helpers.py
Expand Up @@ -438,7 +438,12 @@ def unselected_facet_items(facet, limit=10):
continue
if not (facet, facet_item['name']) in request.params.items():
facets.append(facet_item)
return sorted(facets, key=lambda item: item['count'], reverse=True)[:limit]
facets = sorted(facets, key=lambda item: item['count'], reverse=True)
limit = c.search_facets_limits.get(facet)
if limit:
return facets[:limit]
else:
return facets

def facet_items(*args, **kwargs):
"""
Expand Down Expand Up @@ -468,6 +473,7 @@ def _facet_items(name, limit=10):
return sorted(facets, key=lambda (k, v): v, reverse=True)[:limit]

def facet_title(name):
# FIXME this looks like an i18n issue
return config.get('search.facets.%s.title' % name, name.capitalize())

def am_authorized(c, action, domain_object=None):
Expand Down
8 changes: 4 additions & 4 deletions ckan/templates/package/search.html
Expand Up @@ -33,7 +33,7 @@
<py:otherwise>All datasets</py:otherwise>
</strong>
<py:for each="(field, value) in c.fields">
<span class="facet">${h.facet_title(field)}:</span>
<span class="facet">${c.facet_titles.get(field)}:</span>
<span class="filtered pill" py:choose="">
<py:when test="c.translated_fields and c.translated_fields.has_key((field,value))">
${c.translated_fields[(field,value)]}
Expand Down Expand Up @@ -65,9 +65,9 @@
</section>
</div>
<div class="secondary">
${h.snippet('snippets/facet_list.html', title='Tags', name='tags', limit=10)}
${h.snippet('snippets/facet_list.html', title='Formats', name='res_format', limit=10)}
${h.snippet('snippets/facet_list.html', title='Groups', name='groups', limit=10)}
${h.snippet('snippets/facet_list.html', title='Tags', name='tags')}
${h.snippet('snippets/facet_list.html', title='Formats', name='res_format')}
${h.snippet('snippets/facet_list.html', title='Groups', name='groups')}
</div>
</body>
</html>
7 changes: 4 additions & 3 deletions ckan/templates/snippets/facet_list.html
Expand Up @@ -23,9 +23,9 @@
how a facet-item's count is displayed.
-->
<py:with vars="items=h.unselected_facet_items(name, limit)" xmlns:py="http://genshi.edgewall.org/">
<py:with vars="items=h.unselected_facet_items(name)" xmlns:py="http://genshi.edgewall.org/">
<section class="module">
<h2 class="heading">${h.facet_title(title)} <a href="${c.remove_field(name)}" class="action">Clear All</a></h2>
<h2 class="heading">${c.facet_titles.get(name)} <a href="${c.remove_field(name)}" class="action">Clear All</a></h2>
<nav py:if="items">
<ul class="unstyled nav nav-simple">
<py:for each="item in items">
Expand All @@ -39,7 +39,8 @@ <h2 class="heading">${h.facet_title(title)} <a href="${c.remove_field(name)}" cl
</ul>
</nav>
<p class="footer" py:if="items">
<a href="#" class="read-more">Show All ${h.facet_title(title)}</a>
<a py:if="c.search_facets_limits.get(name)" href="${c.remove_field('_%s_limit' % name, replace=0)}" class="read-more">Show All ${c.facet_titles.get(name)}</a>
<a py:if="not c.search_facets_limits.get(name)" href="${c.remove_field('_%s_limit' % name)}" class="read-more">Show Popular ${c.facet_titles.get(name)}</a>
</p>
</section>
</py:with>

0 comments on commit 7fc29e1

Please sign in to comment.