From ee06dada8bb5b2aa97ffa1386268f337b0599693 Mon Sep 17 00:00:00 2001 From: Toby Date: Wed, 30 May 2012 14:16:29 +0100 Subject: [PATCH] move popular into a helper function to make easier to include --- ckan/lib/helpers.py | 13 ++++++++++++- ckan/templates/package/read.html | 2 +- ckan/templates/snippets/package_list.html | 2 +- ckan/templates/snippets/popular.html | 6 +++--- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 2cfcebcb156..e2c05c09e5d 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -34,7 +34,7 @@ from pylons import request from pylons import session from pylons import c -from pylons.i18n import _ +from pylons.i18n import _, ungettext import html_resources from lib.maintain import deprecated @@ -940,6 +940,16 @@ def debug_inspect(arg): ''' Output pprint.pformat view of supplied arg ''' return literal('
') + pprint.pformat(arg) + literal('
') +def popular(type_, number, min=1, title=None): + ''' display a popular icon. ''' + if type_ == 'views': + title = ungettext('{number} view', '{number} views', number) + elif type_ == 'recent views': + title = ungettext('{number} recent view', '{number} recent views', number) + elif not title: + raise Exception('popular() did not recieve a valid type_ or title') + return snippet('snippets/popular.html', title=title, number=number, min=min) + # these are the functions that will end up in `h` template helpers # if config option restrict_template_vars is true __allowed_functions__ = [ @@ -999,6 +1009,7 @@ def debug_inspect(arg): 'debug_inspect', 'dict_list_reduce', 'full_current_url', + 'popular', # imported into ckan.lib.helpers 'literal', 'link_to', diff --git a/ckan/templates/package/read.html b/ckan/templates/package/read.html index bc5edca9018..d45f528e64e 100644 --- a/ckan/templates/package/read.html +++ b/ckan/templates/package/read.html @@ -43,7 +43,7 @@

Data and Resources

${h.truncate(h.resource_display_name(res), 50)}${res.format} - ${h.snippet('snippets/popular.html', title=ungettext('{num} view', '{num} views', res.tracking_summary.total), num=res.tracking_summary.total)} + ${h.popular('views', res.tracking_summary.total, min=10)}

diff --git a/ckan/templates/snippets/package_list.html b/ckan/templates/snippets/package_list.html index 709205a99b3..2eae113ed93 100644 --- a/ckan/templates/snippets/package_list.html +++ b/ckan/templates/snippets/package_list.html @@ -3,7 +3,7 @@

${h.link_to(h.truncate(title, 80), h.url_for(controller='package', action='read', id=package.get('name')))} - ${h.snippet('snippets/popular.html', title=ungettext('{num} recent view', '{num} recent views', package.tracking_summary.recent), num=package.tracking_summary.recent)} + ${h.popular('recent views', package.tracking_summary.recent, min=10)}

${notes} diff --git a/ckan/templates/snippets/popular.html b/ckan/templates/snippets/popular.html index 963da60c3d2..baf42dfa9b6 100644 --- a/ckan/templates/snippets/popular.html +++ b/ckan/templates/snippets/popular.html @@ -1,4 +1,4 @@ -{# show poular icon basic functionality will need updating #} -{% if num > 10 %} -Popular +{# show poular icon #} +{% if number >= min %} +Popular {% endif %}