From 0b8a036cb302ad5fe4118e4337a68cab89deb97a Mon Sep 17 00:00:00 2001 From: hayley-leblanc Date: Mon, 22 Jul 2019 13:28:59 +0200 Subject: [PATCH 1/2] Add request_type helper function --- ckanext/pdfview/plugin.py | 7 +++++++ .../theme/templates/package/snippets/resource_view.html | 1 + 2 files changed, 8 insertions(+) diff --git a/ckanext/pdfview/plugin.py b/ckanext/pdfview/plugin.py index 7717646..126f0b0 100644 --- a/ckanext/pdfview/plugin.py +++ b/ckanext/pdfview/plugin.py @@ -2,9 +2,12 @@ import ckan.plugins as p import ckan.lib.datapreview as datapreview +from ckan.common import is_flask_request log = logging.getLogger(__name__) +def request_type(): + return is_flask_request() class PdfView(p.SingletonPlugin): '''This extension views PDFs. ''' @@ -18,6 +21,7 @@ class PdfView(p.SingletonPlugin): p.implements(p.IConfigurer, inherit=True) p.implements(p.IConfigurable, inherit=True) p.implements(p.IResourceView, inherit=True) + p.implements(p.ITemplateHelpers) PDF = ['pdf', 'x-pdf', 'acrobat', 'vnd.pdf'] proxy_is_enabled = False @@ -52,3 +56,6 @@ def can_view(self, data_dict): def view_template(self, context, data_dict): return 'pdf.html' + + def get_helpers(self): + return {'pdfview_request_type': request_type} diff --git a/ckanext/pdfview/theme/templates/package/snippets/resource_view.html b/ckanext/pdfview/theme/templates/package/snippets/resource_view.html index 1e5413e..691a8e2 100644 --- a/ckanext/pdfview/theme/templates/package/snippets/resource_view.html +++ b/ckanext/pdfview/theme/templates/package/snippets/resource_view.html @@ -34,6 +34,7 @@

{% if not to_preview %} + {{ h.pdfview_request_type() }} {% set current_filters = request.str_GET.get('filters') %} {% if current_filters %} {% set src = h.url(qualified=true, controller='package', From 6284c240ab234c1cd8ffedc920f9f7c9ae13af87 Mon Sep 17 00:00:00 2001 From: hayley-leblanc Date: Mon, 22 Jul 2019 13:41:23 +0200 Subject: [PATCH 2/2] Get URL based on flask vs. pylons use --- .../package/snippets/resource_view.html | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/ckanext/pdfview/theme/templates/package/snippets/resource_view.html b/ckanext/pdfview/theme/templates/package/snippets/resource_view.html index 691a8e2..9c429a4 100644 --- a/ckanext/pdfview/theme/templates/package/snippets/resource_view.html +++ b/ckanext/pdfview/theme/templates/package/snippets/resource_view.html @@ -34,23 +34,44 @@

{% if not to_preview %} - {{ h.pdfview_request_type() }} - {% set current_filters = request.str_GET.get('filters') %} - {% if current_filters %} - {% set src = h.url(qualified=true, controller='package', - action='resource_view', id=package['name'], - resource_id=resource['id'], - view_id=resource_view['id'], - filters=current_filters) %} + {% set is_flask = h.pdfview_request_type() %} + {# set current_filters correctly, depending on whether we are using flask or pylons #} + {% if is_flask %} + {% set current_filters = request.args.get('filters') %} + {% if current_filters %} + {% set src = h.url(qualified=true, controller='resource', + action='view', id=package['name'], + resource_id=resource['id'], + view_id=resource_view['id'], + filters=current_filters) %} + {% else %} + {% set src = h.url(qualified=true, controller='resource', + action='view', id=package['name'], + resource_id=resource['id'], + view_id=resource_view['id']) %} + {% endif %} {% else %} - {% set src = h.url(qualified=true, controller='package', - action='resource_view', id=package['name'], - resource_id=resource['id'], - view_id=resource_view['id']) %} + {% set current_filters = request.str_GET.get('filters') %} + {% if current_filters %} + {% set src = h.url(qualified=true, controller='package', + action='resource_view', id=package['name'], + resource_id=resource['id'], + view_id=resource_view['id'], + filters=current_filters) %} + {% else %} + {% set src = h.url(qualified=true, controller='package', + action='resource_view', id=package['name'], + resource_id=resource['id'], + view_id=resource_view['id']) %} + {% endif %} {% endif %} {% else %} {# When previewing we need to stick the whole resource_view as a param as there is no other way to pass to information on to the iframe #} - {% set src = h.url(qualified=true, controller='package', action='resource_view', id=package['name'], resource_id=resource['id']) + '?' + h.urlencode({'resource_view': h.dump_json(resource_view)}) %} + {% if is_flask %} + {% set src = h.url(qualified=true, controller='resource', action='view', id=package['name'], resource_id=resource['id']) + '?' + h.urlencode({'resource_view': h.dump_json(resource_view)}) %} + {% else %} + {% set src = h.url(qualified=true, controller='package', action='resource_view', id=package['name'], resource_id=resource['id']) + '?' + h.urlencode({'resource_view': h.dump_json(resource_view)}) %} + {% endif %} {% endif %}