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 @@
+<html xmlns:py="http://genshi.edgewall.org/"
+ xmlns:i18n="http://genshi.edgewall.org/i18n"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ py:strip="">
+
+<div class="popular_datasets">
+ <h2>Featured dataset</h2>
+
+ <h3>${h.link_to(dataset.title, h.url_for(controller='package', action='read', id=dataset.name))}</h3> <div>${h.truncate(dataset.notes, length=200, whole_word=True)}</div>
+ <p></p>
+ <div>
+<a href="${h.url_for(controller='ckanext.ga_report.controller:GaPublisherReport',action='index')}" class="btn btn-primary">More popular datasets</a>
+<a href="${h.url_for(controller='ckanext.ga_report.controller:GaReport',action='index')}" class="btn btn-primary">All usage data</a>
+ </div>
+ </div>
+
+
+</html>
+
+
+
+