Skip to content

Commit

Permalink
Merge branch 'feature-1607-dgu-maintentance-refactor' of https://gith…
Browse files Browse the repository at this point in the history
…ub.com/okfn/ckan into feature-1607-dgu-maintentance-refactor
  • Loading branch information
rossjones committed Feb 16, 2012
2 parents 53952ee + 765de64 commit 8981364
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ckan/config/deployment.ini_tmpl
Expand Up @@ -111,6 +111,11 @@ ckan.site_url =
## Favicon (default is the CKAN software favicon)
ckan.favicon = /images/icons/ckan.ico

## The gravatar default to use. This can be any of the pre-defined strings
## as defined on http://en.gravatar.com/site/implement/images/ (e.g. "identicon"
## or "mm"). Or it can be a url, e.g. "http://example.com/images/avatar.jpg"
ckan.gravatar_default = identicon

## Solr support
#solr_url = http://127.0.0.1:8983/solr

Expand Down
14 changes: 12 additions & 2 deletions ckan/lib/helpers.py
Expand Up @@ -7,6 +7,7 @@
"""
import datetime
import re
import urllib

from webhelpers.html import escape, HTML, literal, url_escape
from webhelpers.html.tools import mail_to
Expand Down Expand Up @@ -249,14 +250,23 @@ def icon_html(url, alt=None):
def icon(name, alt=None):
return icon_html(icon_url(name),alt)

def linked_gravatar(email_hash, size=100, default="identicon"):
def linked_gravatar(email_hash, size=100, default=None):
return literal('''<a href="https://gravatar.com/" target="_blank"
title="Update your avatar at gravatar.com">
%s</a>''' %
gravatar(email_hash,size,default)
)

def gravatar(email_hash, size=100, default="identicon"):
_VALID_GRAVATAR_DEFAULTS = ['404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro']
def gravatar(email_hash, size=100, default=None):
if default is None:
from pylons import config
default = config.get('ckan.gravatar_default', 'identicon')

if not default in _VALID_GRAVATAR_DEFAULTS:
# treat the default as a url
default = urllib.quote(default, safe='')

return literal('''<img src="http://gravatar.com/avatar/%s?s=%d&amp;d=%s"
class="gravatar" />'''
% (email_hash, size, default)
Expand Down
22 changes: 20 additions & 2 deletions ckan/templates/facets.html
Expand Up @@ -5,18 +5,36 @@
py:strip=""
>

<py:def function="facet_sidebar(code, limit=5, label=lambda n: n, title=h.facet_title)">
<div py:if="len(h.facet_items(c, code, limit=limit))" class="facet-box">
<py:def function="facet_sidebar(code, limit=5, label=lambda n: n, title=h.facet_title, if_empty=None)">
<div py:if="if_empty is not None or len(h.facet_items(c, code, limit=limit))" class="facet-box">
<h2>${title(code)}</h2>
<ul class="facet-options">
<li py:for="name, count in h.facet_items(c, code, limit=limit)"
py:if="not (code, name) in c.fields">
<a href="${c.drill_down_url(**{code: name})}">${label(name)}</a> (${count})
</li>
</ul>
<p py:if="not len(h.facet_items(c, code, limit=limit))">${if_empty}</p>
</div>
</py:def>

<!-- Creates a possibly empty list of <li> elements containing links to further search results
This is different from the above function in that it requires the caller to wrap
up the resulting <li> elements in whatever dom element they need. But it does allow for
filters to displayed in a hierarchy.
If if_empty is not None and there are no facets to filter on, then a single <li> element
is generated, with the text specified by if_empty
-->
<py:def function="facet_list_items(code, limit=5, label=lambda n: n, if_empty=None)">
<li py:if="if_empty and len(h.facet_items(c, code, limit=limit)) == 0">${if_empty}</li>
<li py:for="name, count in h.facet_items(c, code, limit=limit)"
py:if="not (code, name) in c.fields">
<a href="${c.drill_down_url(**{code: name})}">${label(name)}</a> (${count})
</li>
</py:def>

<py:def function="field_list()">
<div class="filter-list" py:if="c.fields">
<div class="filter-entry" py:for="(field, value) in c.fields">
Expand Down
34 changes: 33 additions & 1 deletion ckan/tests/lib/test_helpers.py
Expand Up @@ -3,6 +3,8 @@
import datetime
from nose.tools import assert_equal

from pylons import config

from ckan.tests import *
from ckan.lib import helpers as h

Expand Down Expand Up @@ -54,10 +56,40 @@ def test_time_ago_in_words_from_str(self):
def test_gravatar(self):
email = 'zephod@gmail.com'
expected =['<a href="https://gravatar.com/"',
'<img src="http://gravatar.com/avatar/7856421db6a63efa5b248909c472fbd2?s=200&amp;d=identicon"', '</a>']
'<img src="http://gravatar.com/avatar/7856421db6a63efa5b248909c472fbd2?s=200&amp;d=mm"', '</a>']
# Hash the email address
import hashlib
email_hash = hashlib.md5(email).hexdigest()
res = h.linked_gravatar(email_hash, 200, default='mm')
for e in expected:
assert e in res, (e,res)

def test_gravatar_config_set_default(self):
"""Test when default gravatar is None, it is pulled from the config file"""
email = 'zephod@gmail.com'
default = config.get('ckan.gravatar_default', 'identicon')
expected =['<a href="https://gravatar.com/"',
'<img src="http://gravatar.com/avatar/7856421db6a63efa5b248909c472fbd2?s=200&amp;d=%s"' % default,
'</a>']
# Hash the email address
import hashlib
email_hash = hashlib.md5(email).hexdigest()
res = h.linked_gravatar(email_hash, 200)
for e in expected:
assert e in res, (e,res)

def test_gravatar_encodes_url_correctly(self):
"""Test when the default gravatar is a url, it gets urlencoded"""
email = 'zephod@gmail.com'
default = 'http://example.com/images/avatar.jpg'
expected =['<a href="https://gravatar.com/"',
'<img src="http://gravatar.com/avatar/7856421db6a63efa5b248909c472fbd2?s=200&amp;d=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg"',
'</a>']
# Hash the email address
import hashlib
email_hash = hashlib.md5(email).hexdigest()
res = h.linked_gravatar(email_hash, 200, default=default)
for e in expected:
assert e in res, (e,res)


0 comments on commit 8981364

Please sign in to comment.