Support custom resource.download handlers

This commit is contained in:
Sergey Motornyuk 2021-02-10 11:00:30 +07:00
parent 02dc6f22a5
commit 8d9ea6cd49
2 changed files with 27 additions and 1 deletions

View File

@ -67,6 +67,7 @@ Installation
googleanalytics.track_events = false googleanalytics.track_events = false
googleanalytics.fields = {} googleanalytics.fields = {}
googleanalytics.enable_user_id = false googleanalytics.enable_user_id = false
ckanext.googleanalytics.download_handler = ckan.views.resource:download
``resource_prefix`` is an arbitrary identifier so that we can query ``resource_prefix`` is an arbitrary identifier so that we can query
for downloads in Google Analytics. It can theoretically be any for downloads in Google Analytics. It can theoretically be any
@ -93,6 +94,15 @@ Installation
This way metrics can be tracked for every logged in user. You can read more This way metrics can be tracked for every logged in user. You can read more
about this feature and its benefits `here <https://support.google.com/analytics/answer/3123662>`_. about this feature and its benefits `here <https://support.google.com/analytics/answer/3123662>`_.
When resource is downloaded, ckanext-googleanalytics posts event to
GA and calls default download callback. If you are using CKAN>=2.9
and some of your plugins redefines `resource.download`
route(ckanext-cloudstorage, for example), you can specify which
function must be called instead of `ckan.views.resource:download`
via `ckanext.googleanalytics.download_handler` config variable. For ckanext-cloudstorage you can use::
ckanext.googleanalytics.download_handler = ckanext.cloudstorage.views:download
Domain Linking Domain Linking
-------------- --------------

View File

@ -5,6 +5,7 @@ import logging
import six import six
from flask import Blueprint from flask import Blueprint
from werkzeug.utils import import_string
import ckan.logic as logic import ckan.logic as logic
import ckan.plugins.toolkit as tk import ckan.plugins.toolkit as tk
@ -13,6 +14,8 @@ import ckan.views.resource as resource
from ckan.common import g from ckan.common import g
CONFIG_HANDLER_PATH = 'ckanext.googleanalytics.download_handler'
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
ga = Blueprint("google_analytics", "google_analytics") ga = Blueprint("google_analytics", "google_analytics")
@ -49,6 +52,17 @@ ga.add_url_rule(
def download(id, resource_id, filename=None, package_type="dataset"): def download(id, resource_id, filename=None, package_type="dataset"):
handler_path = tk.config.get(CONFIG_HANDLER_PATH)
if handler_path:
handler = import_string(handler_path, silent=True)
else:
handler = None
log.warning((
'Missing {} config option.'
).format(CONFIG_HANDLER_PATH))
if not handler:
log.debug('Use default CKAN callback for resource.download')
handler = resource.download
_post_analytics( _post_analytics(
g.user, g.user,
"CKAN Resource Download Request", "CKAN Resource Download Request",
@ -56,7 +70,9 @@ def download(id, resource_id, filename=None, package_type="dataset"):
"Download", "Download",
resource_id, resource_id,
) )
return resource.download(package_type, id, resource_id, filename) return handler(
package_type=package_type, id=id,
resource_id=resource_id, filename=filename)
ga.add_url_rule( ga.add_url_rule(