From e454bea36c3c1e46278cd9979f9bc9ffe7e0acb5 Mon Sep 17 00:00:00 2001 From: alexandru-m-g Date: Fri, 17 Jun 2016 17:05:47 +0300 Subject: [PATCH] #4228 fixing small bug with resource preview and making resource links relative --- .../controllers/dataset_controller.py | 19 +++++++++-- .../ckanext/hdx_package/helpers/helpers.py | 34 +++++++++++++++---- .../templates/package/resource_read.html | 7 ++-- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py b/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py index dfb988e229..a493ef96f8 100644 --- a/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py +++ b/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py @@ -684,6 +684,12 @@ def read(self, id, format='html'): context, {'id': resource['id']}) resource['has_views'] = len(resource_views) > 0 + if helpers.is_ckan_domain(resource['url']): + resource['url'] = helpers.make_url_relative(resource['url']) + + if resource.get('perma_link') and helpers.is_ckan_domain(resource['perma_link']): + resource['perma_link'] = helpers.make_url_relative(resource['perma_link']) + # Is this an indicator? Load up graph data #c.pkg_dict['indicator'] = 1 try: @@ -1079,9 +1085,9 @@ def resource_read(self, id, resource_id): # set dataset type for google analytics - modified by HDX # c.ga_dataset_type = self._google_analytics_dataset_type(c.package) - c.analytics_is_cod = self._analytics_is_cod(c.package) - c.analytics_is_indicator = self._analytics_is_indicator(c.package) - c.analytics_group_names, c.analytics_group_ids = self._analytics_location(c.package) + c.analytics_is_cod = analytics.is_cod(c.package) + c.analytics_is_indicator = analytics.is_indicator(c.package) + c.analytics_group_names, c.analytics_group_ids = analytics.extract_locations_in_json(c.package) current_resource_view = None view_id = request.GET.get('view_id') @@ -1102,6 +1108,13 @@ def resource_read(self, id, resource_id): 'current_resource_view': current_resource_view, 'dataset_type': dataset_type} + download_url = c.resource.get('perma_link') if c.resource.get('perma_link') else c.resource['url'] + c.resource['original_url'] = download_url + c.resource['download_url'] = download_url + if helpers.is_ckan_domain(download_url): + c.resource['download_url'] = helpers.make_url_relative(download_url) + + template = self._resource_template(dataset_type) return render(template, extra_vars=vars) diff --git a/ckanext-hdx_package/ckanext/hdx_package/helpers/helpers.py b/ckanext-hdx_package/ckanext/hdx_package/helpers/helpers.py index 5202d0550e..ecc49b1507 100644 --- a/ckanext-hdx_package/ckanext/hdx_package/helpers/helpers.py +++ b/ckanext-hdx_package/ckanext/hdx_package/helpers/helpers.py @@ -565,17 +565,13 @@ def hdx_get_proxified_resource_url(data_dict, proxy_schemes=['http','https']): 2) Return a domain relative url (without schema, domain or port) for local resources. :param data_dict: contains a resource and package dict - :type data_dict: dictionary + :type data_dict: dict :param proxy_schemes: list of url schemes to proxy for. :type data_dict: list ''' - ckan_url = config.get('ckan.site_url', '//localhost:5000') - url = data_dict['resource']['url'] - - parsed_url = urlparse.urlparse(url) - ckan_parsed_url = urlparse.urlparse(ckan_url) - same_domain = True if not parsed_url.hostname or parsed_url.hostname == ckan_parsed_url.hostname else False + same_domain = is_ckan_domain(data_dict['resource']['url']) + parsed_url = urlparse.urlparse(data_dict['resource']['url']) scheme = parsed_url.scheme if not same_domain and scheme in proxy_schemes: @@ -590,6 +586,30 @@ def hdx_get_proxified_resource_url(data_dict, proxy_schemes=['http','https']): return url +def is_ckan_domain(url): + ''' + :param url: url to check whether it's on the same domain as ckan + :type url: str + :return: True if it's the same domain. False otherwise + :rtype: bool + ''' + ckan_url = config.get('ckan.site_url', '//localhost:5000') + parsed_url = urlparse.urlparse(url) + ckan_parsed_url = urlparse.urlparse(ckan_url) + same_domain = True if not parsed_url.hostname or parsed_url.hostname == ckan_parsed_url.hostname else False + return same_domain + +def make_url_relative(url): + ''' + Transforms something like http://testdomain.com/test to /test + :param url: url to check whether it's on the same domain as ckan + :type url: str + :return: the new url as a string + :rtype: str + ''' + parsed_url = urlparse.urlparse(url) + return urlparse.urlunparse((None, None) + parsed_url[2:]) + def generate_mandatory_fields(): ''' diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html index 8d45b12ad0..be06791b88 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html @@ -1,7 +1,6 @@ {% extends "package/base.html" %} {% set res = c.resource %} -{% set resource_dwd_url = res.perma_link if res.perma_link else res.url %} {# The lines below are for analytics #} {% block analytics_org_name %}{{ c.package.organization.name }}{% endblock %} @@ -49,7 +48,7 @@ #} {% if res.url and h.is_url(res.url) %}
  • - + {# {% if res.resource_type in ('listing', 'service') %} {{ _('View') }} @@ -78,9 +77,9 @@ {% block resource_read_title %}

    {{ h.resource_display_name(res) | truncate(50) }}

    {% endblock %} {% block resource_read_url %} {% if res.url and h.is_url(res.url) %} -

    {{ _('URL:') }} {{ resource_dwd_url }}

    +

    {{ _('URL:') }} {{ res.original_url }}

    {% elif res.url %} -

    {{ _('URL:') }} {{ resource_dwd_url }}

    +

    {{ _('URL:') }} {{ res.download_url }}

    {% endif %} {% endblock %}