Skip to content

Commit

Permalink
#4228 fixing small bug with resource preview and making resource link…
Browse files Browse the repository at this point in the history
…s relative
  • Loading branch information
alexandru-m-g committed Jun 17, 2016
1 parent b52b059 commit e454bea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand All @@ -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)

Expand Down
34 changes: 27 additions & 7 deletions ckanext-hdx_package/ckanext/hdx_package/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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():
'''
Expand Down
Original file line number Diff line number Diff line change
@@ -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 %}
Expand Down Expand Up @@ -49,7 +48,7 @@
#}
{% if res.url and h.is_url(res.url) %}
<li>
<a class="btn hdx-btn ga-download resource-btn resource-icon-btn resource-url-analytics resource-type-{{ res.resource_type }}" href="{{ resource_dwd_url }}">
<a class="btn hdx-btn ga-download resource-btn resource-icon-btn resource-url-analytics resource-type-{{ res.resource_type }}" href="{{ res.download_url }}">
{#
{% if res.resource_type in ('listing', 'service') %}
<i class="icon-eye-open"></i> {{ _('View') }}
Expand Down Expand Up @@ -78,9 +77,9 @@
{% block resource_read_title %}<h1 class="page-heading">{{ h.resource_display_name(res) | truncate(50) }}</h1>{% endblock %}
{% block resource_read_url %}
{% if res.url and h.is_url(res.url) %}
<p class="muted ellipsis">{{ _('URL:') }} <a href="{{ resource_dwd_url }}" title="{{ resource_dwd_url }}">{{ resource_dwd_url }}</a></p>
<p class="muted ellipsis">{{ _('URL:') }} <a href="{{ res.download_url }}" title="{{ res.download_url }}">{{ res.original_url }}</a></p>
{% elif res.url %}
<p class="muted ellipsis">{{ _('URL:') }} {{ resource_dwd_url }}</p>
<p class="muted ellipsis">{{ _('URL:') }} {{ res.download_url }}</p>
{% endif %}
{% endblock %}
<div class="prose notes" property="rdfs:label">
Expand Down

0 comments on commit e454bea

Please sign in to comment.