From: Ross Jones Date: Fri, 02 Nov 2012 17:50:41 +0000 Subject: New single entry random dataset helper X-Git-Url: https://maxious.lambdacomplex.org/git/?p=ckanext-ga-report.git&a=commitdiff&h=6ca9caae693d4d8ec451fb9a7b61290bc4cd18d6 --- New single entry random dataset helper --- --- a/ckanext/ga_report/helpers.py +++ b/ckanext/ga_report/helpers.py @@ -26,6 +26,30 @@ 'publisher': publisher } return base.render_snippet('ga_report/ga_popular_datasets.html', **ctx) + +def single_popular_dataset(top=20): + import random + + datasets = {} + rand = random.randrange(0, top) + entry = model.Session.query(GA_Url).\ + filter(GA_Url.url.like('/dataset/%')).\ + order_by('ga_url.pageviews::int desc')[rand] + + + dataset = None + while not dataset: + dataset = model.Package.get(entry.url[len('/dataset/'):]) + if dataset and not dataset.state == 'active': + dataset = None + else: + publisher = model.Group.get(entry.department_id) + + ctx = { + 'dataset': dataset, + 'publisher': publisher + } + return base.render_snippet('ga_report/ga_popular_single.html', **ctx) def most_popular_datasets(publisher, count=20): --- a/ckanext/ga_report/plugin.py +++ b/ckanext/ga_report/plugin.py @@ -4,7 +4,8 @@ from ckan.plugins import implements, toolkit from ckanext.ga_report.helpers import (most_popular_datasets, - popular_datasets) + popular_datasets, + single_popular_dataset) log = logging.getLogger('ckanext.ga-report') @@ -26,6 +27,7 @@ 'ga_report_installed': lambda: True, 'popular_datasets': popular_datasets, 'most_popular_datasets': most_popular_datasets, + 'single_popular_dataset': single_popular_dataset } def after_map(self, map): --- /dev/null +++ b/ckanext/ga_report/templates/ga_report/ga_popular_single.html @@ -1,1 +1,23 @@ + + + + + + + + + +