From: Ross Jones Date: Tue, 30 Oct 2012 13:03:28 +0000 Subject: Changed to provide a lambda to check if the extension is installed X-Git-Url: http://maxious.lambdacomplex.org/git/?p=ckanext-ga-report.git&a=commitdiff&h=3a6e269b20a5fa022149bfa5f1a495fb27f641a5 --- Changed to provide a lambda to check if the extension is installed --- --- /dev/null +++ b/ckanext/ga_report/helpers.py @@ -1,1 +1,41 @@ +import logging +import operator +import ckan.lib.base as base +import ckan.model as model +_log = logging.getLogger(__name__) + +def most_popular_datasets(publisher, count=20): + from ckanext.ga_report.ga_model import GA_Url + + if not publisher: + _log.error("No valid publisher passed to 'most_popular_datasets'") + return "" + + datasets = {} + entries = model.Session.query(GA_Url).\ + filter(GA_Url.department_id==publisher.name).\ + filter(GA_Url.url.like('/dataset/%')).\ + order_by('ga_url.pageviews::int desc')[:count] + for entry in entries: + p = model.Package.get(entry.url[len('/dataset/'):]) + if not p in datasets: + datasets[p] = {'views':0, 'visits': 0} + datasets[p]['views'] = datasets[p]['views'] + int(entry.pageviews) + datasets[p]['visits'] = datasets[p]['visits'] + int(entry.visitors) + + results = [] + for k, v in datasets.iteritems(): + results.append((k,v['views'],v['visits'])) + + results = sorted(results, key=operator.itemgetter(1), reverse=True) + + ctx = { + 'dataset_count': len(datasets), + 'datasets': results, + + 'publisher': publisher + } + + return base.render_snippet('ga_report/publisher/popular.html', **ctx) + --- a/ckanext/ga_report/plugin.py +++ b/ckanext/ga_report/plugin.py @@ -2,19 +2,28 @@ import ckan.lib.helpers as h import ckan.plugins as p from ckan.plugins import implements, toolkit -#import gasnippet -#import commands -#import dbutil log = logging.getLogger('ckanext.ga-report') class GAReportPlugin(p.SingletonPlugin): implements(p.IConfigurer, inherit=True) implements(p.IRoutes, inherit=True) + implements(p.ITemplateHelpers, inherit=True) def update_config(self, config): toolkit.add_template_directory(config, 'templates') toolkit.add_public_directory(config, 'public') + + def get_helpers(self): + """ + A dictionary of extra helpers that will be available to provide + ga report info to templates. + """ + from ckanext.ga_report.helpers import most_popular_datasets + return { + 'ga_report_installed': lambda: True, + 'most_popular_datasets': most_popular_datasets, + } def after_map(self, map): map.connect( --- /dev/null +++ b/ckanext/ga_report/templates/ga_report/publisher/popular.html @@ -1,1 +1,26 @@ + + +

We do not currently have analytics data for ${publisher.title}

+
+ + + + + + + + --- a/ckanext/ga_report/templates/ga_report/publisher/read.html +++ b/ckanext/ga_report/templates/ga_report/publisher/read.html @@ -6,6 +6,7 @@ Analytics for ${g.site_title} +
  • ${c.publisher.title}