Skip to content

Commit

Permalink
[#2085] only link to http, https and ftp resource urls
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Feb 10, 2015
1 parent 9f8f89e commit 7c9d4ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -104,6 +104,21 @@ def url_for_static(*args, **kw):
return my_url


def is_url(*args, **kw):
'''
Returns True if argument parses as a http, https or ftp URL
'''
if not args:
return False
try:
url = urlparse.urlparse(args[0])
except ValueError:
return False

valid_schemes = ('http', 'https', 'ftp')
return url.scheme in valid_schemes


def _add_i18n_to_url(url_to_amend, **kw):
# If the locale keyword param is provided then the url is rewritten
# using that locale .If return_to is provided this is used as the url
Expand Down Expand Up @@ -1554,6 +1569,7 @@ def SI_number_span(number):
'url',
'url_for',
'url_for_static',
'is_url',
'lang',
'flash',
'flash_error',
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/package/resource_read.html
Expand Up @@ -20,7 +20,7 @@
{% if h.check_access('package_update', {'id':pkg.id }) %}
<li>{% link_for _('Edit'), controller='package', action='resource_edit', id=pkg.name, resource_id=res.id, class_='btn', icon='wrench' %}</li>
{% endif %}
{% if res.url %}
{% if res.url and h.is_url(res.url) %}
<li>
<a class="btn btn-primary resource-url-analytics resource-type-{{ res.resource_type }}" href="{{ res.url }}">
{% if res.resource_type in ('listing', 'service') %}
Expand All @@ -41,7 +41,7 @@
{% block resource_content %}
{% block resource_read_title %}<h1 class="page-heading">{{ h.resource_display_name(res) | truncate(50) }}</h1>{% endblock %}
{% block resource_read_url %}
{% if res.url %}
{% if res.url and h.is_url(res.url) %}
<p class="muted ellipsis">{{ _('URL:') }} <a href="{{ res.url }}" title="{{ res.url }}">{{ res.url }}</a></p>
{% endif %}
{% endblock %}
Expand Down
2 changes: 2 additions & 0 deletions ckan/templates/package/snippets/resource_item.html
Expand Up @@ -28,12 +28,14 @@
{{ _('Preview') }}
</a>
</li>
{% if res.url and h.is_url(res.url) %}
<li>
<a href="{{ res.url }}" class="resource-url-analytics" target="_blank">
<i class="icon-download"></i>
{{ _('Download') }}
</a>
</li>
{% endif %}
{% endblock %}
</ul>
</div>
Expand Down

0 comments on commit 7c9d4ec

Please sign in to comment.