From: CKAN data.gov.au Date: Wed, 04 Dec 2013 00:44:01 +0000 Subject: Add embed hint to datapreviews X-Git-Url: https://maxious.lambdacomplex.org/git/?p=ckanext-datagovau.git&a=commitdiff&h=e2d9caa5d42d30433cd2b0da5613ca12b6971861 --- Add embed hint to datapreviews --- --- a/README.rst +++ b/README.rst @@ -4,9 +4,11 @@ * A CKAN Extension "plugin" at ``ckanext/datagovau/plugin.py`` which, when loaded, overrides various settings in the core ``ini``-file to provide: - * A path to local customisations of the core templates + * A path to local customisations of the core templates to include AGLS/Dublin Core minimum metadata * A custom Package edit form that defaults to cc-by licence * A custom n3/rdf output format + * Replaces links with http/https protocol independent versions + * Provides HTML to users to embed data previews on their own website * A cut down licenses.json file --- /dev/null +++ b/admin/reset.sh @@ -1,1 +1,17 @@ +paster --plugin=ckan db clean --config=development.ini +echo "drop extension postgis cascade;" | psql -d ckantest +paster --plugin=ckan db clean --config=development.ini +#to initiate for first time instead of load from dump +#paster --plugin=ckan db init --config=development.in +#paster --plugin=ckan user add maxious password=snmc email=maxious@gmail.com +#paster --plugin=ckan sysadmin add maxious +#paster --plugin=ckan db dump dump.db + +#paster --plugin=ckan db load --config=development.ini dump.db +paster --plugin=ckan db load --config=development.ini dump.harvest.db +echo "create extension postgis;" | psql -d ckantest +#sleep 2 +paster --plugin=ckan search-index rebuild --config=development.ini +#rm -r /tmp/pairtree_* + --- /dev/null +++ b/ckanext/datagovau/controller.py @@ -1,1 +1,84 @@ +import urllib +import json +from pprint import pprint +import logging +import ckan.logic as logic +import hashlib +import threading +from ckan.common import _, c, request, response +from pylons import config +from webob.multidict import UnicodeMultiDict +from paste.util.multidict import MultiDict +log = logging.getLogger(__name__) + +from ckan.controllers.api import ApiController + +class DGAApiController(ApiController): + + def _post_analytics(self,user,request_obj_type,request_function,request_id): + if (config.get('googleanalytics.id') != None): + data = urllib.urlencode({ + "v":1, + "tid":config.get('googleanalytics.id'), + "cid":hashlib.md5(user).hexdigest(), + "t":"event", + "dh":c.environ['HTTP_HOST'], + "dp":c.environ['PATH_INFO'], + "dr":c.environ.get('HTTP_REFERER',''), + "ec":"CKAN API Request", + "ea":request_obj_type+request_function, + "el":request_id, + }) + log.debug("Sending API Analytics Data: "+data) + # send analytics asynchronously + threading.Thread(target=urllib.urlopen,args=("http://www.google-analytics.com/collect", data)).start() + + + def action(self, logic_function, ver=None): + try: + function = logic.get_action(logic_function) + except Exception,e: + log.debug(e) + pass + try: + side_effect_free = getattr(function, 'side_effect_free', False) + request_data = self._get_request_data(try_url_params=side_effect_free) + if isinstance(request_data, dict): + id = request_data.get('id','') + if 'q' in request_data.keys(): + id = request_data['q'] + if 'query' in request_data.keys(): + id = request_data['query'] + self._post_analytics(c.user,logic_function,'', id) + except Exception,e: + print log.debug(e) + pass + + return ApiController.action(self,logic_function, ver) + + def list(self, ver=None, register=None, subregister=None, id=None): + self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"list",id) + return ApiController.list(self,ver, register, subregister, id) + def show(self, ver=None, register=None, subregister=None, id=None, id2=None): + self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"show",id) + return ApiController.show(self,ver, register, subregister, id,id2) + def update(self, ver=None, register=None, subregister=None, id=None, id2=None): + self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"update",id) + return ApiController.update(self,ver, register, subregister, id,id2) + def delete(self, ver=None, register=None, subregister=None, id=None, id2=None): + self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"delete",id) + return ApiController.delete(self,ver, register, subregister, id,id2) + def search(self, ver=None, register=None): + id = None + try: + params = MultiDict(self._get_search_params(request.params)) + if 'q' in params.keys(): + id = params['q'] + if 'query' in params.keys(): + id = params['query'] + except ValueError, e: + print str(e) + pass + self._post_analytics(c.user,register,"search",id) + --- a/ckanext/datagovau/plugin.py +++ b/ckanext/datagovau/plugin.py @@ -6,6 +6,10 @@ import ckan.plugins.toolkit as tk import ckan.model as model from pylons import config +from routes.mapper import SubMapper, Mapper as _Mapper + +from sqlalchemy import orm +import ckan.model #parse the activity feed for last active non-system user def get_last_active_user(id): @@ -26,6 +30,7 @@ lib.helpers.get_action('user_activity_list',{'id':user_dict['id']}) if x['data'].get('package')] return created_datasets_list + active_datasets_list + class DataGovAuPlugin(plugins.SingletonPlugin, tk.DefaultDatasetForm): '''An example IDatasetForm CKAN plugin. @@ -36,6 +41,55 @@ plugins.implements(plugins.IConfigurer, inherit=False) plugins.implements(plugins.IDatasetForm, inherit=False) plugins.implements(plugins.ITemplateHelpers, inherit=False) + plugins.implements(plugins.IRoutes, inherit=True) + + def before_map(self, map): + + # Helpers to reduce code clutter + GET = dict(method=['GET']) + PUT = dict(method=['PUT']) + POST = dict(method=['POST']) + DELETE = dict(method=['DELETE']) + GET_POST = dict(method=['GET', 'POST']) + # intercept API calls that we want to capture analytics on + register_list = [ + 'package', + 'dataset', + 'resource', + 'tag', + 'group', + 'related', + 'revision', + 'licenses', + 'rating', + 'user', + 'activity' + ] + register_list_str = '|'.join(register_list) + # /api ver 3 or none + with SubMapper(map, controller='ckanext.datagovau.controller:DGAApiController', path_prefix='/api{ver:/3|}', + ver='/3') as m: + m.connect('/action/{logic_function}', action='action', + conditions=GET_POST) + + # /api ver 1, 2, 3 or none + with SubMapper(map, controller='ckanext.datagovau.controller:DGAApiController', path_prefix='/api{ver:/1|/2|/3|}', + ver='/1') as m: + m.connect('/search/{register}', action='search') + + # /api/rest ver 1, 2 or none + with SubMapper(map, controller='ckanext.datagovau.controller:DGAApiController', path_prefix='/api{ver:/1|/2|}', + ver='/1', requirements=dict(register=register_list_str) + ) as m: + + m.connect('/rest/{register}', action='list', conditions=GET) + m.connect('/rest/{register}', action='create', conditions=POST) + m.connect('/rest/{register}/{id}', action='show', conditions=GET) + m.connect('/rest/{register}/{id}', action='update', conditions=PUT) + m.connect('/rest/{register}/{id}', action='update', conditions=POST) + m.connect('/rest/{register}/{id}', action='delete', conditions=DELETE) + + return map def update_config(self, config): # Add this plugin's templates dir to CKAN's extra_template_paths, so --- /dev/null +++ b/ckanext/datagovau/templates/base.html @@ -1,1 +1,7 @@ +{% ckan_extends %} + {% block meta %} + {{ super() }} + + {% endblock %} + --- /dev/null +++ b/ckanext/datagovau/templates/dataviewer/base.html @@ -1,1 +1,11 @@ +{% ckan_extends %} +{% block scripts %} + {{ super() }} + +{% endblock %} + + --- /dev/null +++ b/ckanext/datagovau/templates/dataviewer/snippets/data_preview.html @@ -1,1 +1,32 @@ +
+ {% if embed %} + {# images can be embedded directly #} + + {% else %} +
+

+ + {{ _('This resource can not be previewed at the moment.') }} + + {{ _('Click here for more information.') }} + +

+

+

+ + + {{ _('Download resource') }} + +

+
+ + {% endif %} +
+
+

Embed this visualisation in your own website...

+Copy the HTML in the box below and you can display this visualisation on your own website.
+ +
--- a/ckanext/datagovau/templates/package/read.html +++ b/ckanext/datagovau/templates/package/read.html @@ -1,7 +1,6 @@ {% ckan_extends %} {% block primary_content_inner %} {{ super() }} -
{{ h.disqus_comments() }}
@@ -128,3 +127,13 @@ {% endblock %} +{% block secondary_content %} + {{ super() }} + + {% set dataset_extent = h.get_pkg_dict_extra(c.pkg_dict, 'spatial', '') %} + {% if dataset_extent %} + {% snippet "spatial/snippets/dataset_map_sidebar.html", extent=dataset_extent %} + {% endif %} + +{% endblock %} + --- a/ckanext/datagovau/templates/package/read.rdf +++ b/ckanext/datagovau/templates/package/read.rdf @@ -23,28 +23,20 @@ ${c.pkg_dict['notes']} ${c.pkg_dict['metadata_created']} ${c.pkg_dict['metadata_modified']} - en - ${c.pkg_dict['license_url']} + ${c.pkg_dict['license_id']} - + "${c.pkg_dict['license_id']}" ${ tag_dict["name"] } - - ${ group_dict["title"] } - - - - ${c.pkg_dict['name']} @@ -69,6 +61,9 @@ + + + ${ c.pkg_dict['organization']['title'] } @@ -87,10 +82,26 @@ - - ${c.pkg_dict.get('url')} + + + + ${extra_dict.get('key','')} + ${extra_dict.get('value','')} + + + + en + ${c.pkg_dict.contact_point } + + + ${ c.pkg_dict.spatial } + + ${ c.pkg_dict.spatial_coverage } + + ${ c.pkg_dict.jurisdiction } ${ c.pkg_dict.get('temporal_coverage') } @@ -117,14 +128,6 @@ ${ c.pkg_dict.get('granularity') } - - - - ${extra_dict.get('key','')} - ${extra_dict.get('value','')} - - - --- /dev/null +++ b/ckanext/datagovau/templates/package/search.html @@ -1,1 +1,7 @@ +{% ckan_extends %} +{% block secondary_content %} +{{ super() }} + {% snippet "spatial/snippets/spatial_query.html", default_extent="[[-11, 114], [-42, 154]]" %} +{% endblock %} +