Skip to content

Commit

Permalink
[#440] Extend h.sorted_extras() to do substitutions and auto clean keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Feb 21, 2013
1 parent 41a6c5b commit 70fb87f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ckan/lib/helpers.py
Expand Up @@ -548,15 +548,28 @@ def _search_url(params):
return _url_with_params(url, params)


def sorted_extras(list_):
''' Used for outputting package extras '''
def sorted_extras(package_extras, auto_clean=False, subs=None):
''' Used for outputting package extras
:param package_extras: the package extras
:type package_extras: dict
:param auto_clean: If true capitalize and replace -_ with spaces
:type auto_clean: bool
:param subs: substitutes to use instead of given keys
:type subs: dict {'key': 'replacement'}
'''

output = []
for extra in sorted(list_, key=lambda x: x['key']):
for extra in sorted(package_extras, key=lambda x: x['key']):
if extra.get('state') == 'deleted':
continue
k, v = extra['key'], extra['value']
if k in g.package_hide_extras:
continue
if subs and k in subs:
k = subs[k]
elif auto_clean:
k = k.replace('_', ' ').replace('-', ' ').title()
if isinstance(v, (list, tuple)):
v = ", ".join(map(unicode, v))
output.append((k, v))
Expand Down

0 comments on commit 70fb87f

Please sign in to comment.