Skip to content

Commit

Permalink
[ckan#3831] Standardize on url_for on popup
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Sep 25, 2017
1 parent e49686e commit de7db05
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ckan/controllers/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ def resource_read(self, id, resource_id):
except KeyError:
c.package['isopen'] = False

# TODO: find a nicer way of doing this
# Deprecated: c.datastore_api - use h.action_url instead
c.datastore_api = '%s/api/action' % \
config.get('ckan.site_url', '').rstrip('/')

Expand Down
8 changes: 8 additions & 0 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import copy
import urlparse
from urllib import urlencode
import uuid

from paste.deploy import converters
from webhelpers.html import HTML, literal, tags, tools
Expand Down Expand Up @@ -2402,3 +2403,10 @@ def load_plugin_helpers():

for plugin in reversed(list(p.PluginImplementations(p.ITemplateHelpers))):
helper_functions.update(plugin.get_helpers())

@core_helper
def sanitize_id(id_):
'''Given an id (uuid4), if it has any invalid characters it raises
ValueError.
'''
return str(uuid.UUID(id_))
24 changes: 12 additions & 12 deletions ckanext/datastore/templates/ajax_snippets/api_info.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{#
Displays information about accessing a resource via the API.

datastore_root_url - The root API url.
resource_id - The resource id
embedded - If true will not include the "modal" classes on the snippet.

Example

{% snippet 'ajax_snippets/api_info.html', datastore_root_url=datastore_root_url, resource_id=resource_id, embedded=true %}
{% snippet 'ajax_snippets/api_info.html', resource_id=resource_id, embedded=true %}

#}

{% set sql_example_url = datastore_root_url + '/datastore_search_sql?sql=SELECT * from "' + resource_id + '" WHERE title LIKE \'jones\'' %}

{% set resource_id = h.sanitize_id(resource_id) %}
{% set sql_example_url = h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search_sql', qualified=True) + '?sql=SELECT * from "' + resource_id + '" WHERE title LIKE \'jones\'' %}
{# not urlencoding the sql because its clearer #}
<div{% if not embedded %} class="modal"{% endif %}>
<div class="modal-header">
<h3>
Expand Down Expand Up @@ -40,19 +40,19 @@ <h3>
<tbody>
<tr>
<th scope="row">{{ _('Create') }}</th>
<td><code>{{ datastore_root_url }}/datastore_create</code></td>
<td><code>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_create', qualified=True) }}</code></td>
</tr>
<tr>
<th scope="row">{{ _('Update / Insert') }}</th>
<td><code>{{ datastore_root_url }}/datastore_upsert</code></td>
<td><code>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_upsert', qualified=True) }}</code></td>
</tr>
<tr>
<th scope="row">{{ _('Query') }}</th>
<td><code>{{ datastore_root_url }}/datastore_search</code></td>
<td><code>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', qualified=True) }}</code></td>
</tr>
<tr>
<th scope="row">{{ _('Query (via SQL)') }}</th>
<td><code>{{ datastore_root_url }}/datastore_search_sql</code></td>
<td><code>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search_sql', qualified=True) }}</code></td>
</tr>

</tbody>
Expand All @@ -69,12 +69,12 @@ <h3>
<div class="accordion-inner">
<strong>{{ _('Query example (first 5 results)') }}</strong>
<p>
<code>{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&limit=5</code>
<code>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5, qualified=True) }}</code>
</p>

<strong>{{ _('Query example (results containing \'jones\')') }}</strong>
<p>
<code>{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&q=jones</code>
<code>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', resource_id=resource_id, q='jones', qualified=True) }}</code>
</p>

<strong>{{ _('Query example (via SQL statement)') }}</strong>
Expand All @@ -100,7 +100,7 @@ <h3>
q: 'jones' // query for 'jones'
};
$.ajax({
url: '{{ datastore_root_url }}/datastore_search',
url: '{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', qualified=True) }}',
data: data,
dataType: 'jsonp',
success: function(data) {
Expand All @@ -119,7 +119,7 @@ <h3>
<div class="accordion-inner">
<pre>
import urllib
url = '{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&amp;limit=5&amp;q=title:jones'
url = '{{ h.url_for(qualified=True, controller='api', action='action', ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5) + '&q=title:jones' }}' {# not urlencoding the ":" because its clearer #}
fileobj = urllib.urlopen(url)
print fileobj.read()
</pre>
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/templates/package/resource_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block resource_actions_inner %}
{{ super() }}
{% if res.datastore_active %}
<li>{% snippet 'package/snippets/data_api_button.html', resource=res, datastore_root_url=c.datastore_api %}</li>
<li>{% snippet 'package/snippets/data_api_button.html', resource=res %}</li>
{% endif %}
{% endblock %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{# Data API Help Button

resource: the resource
datastore_root_url: the root url of the datastore

#}
{% if resource.datastore_active %}
{% set loading_text = _('Loading...') %}
{% set api_info_url = h.url_for(controller='api', action='snippet', ver=1, snippet_path='api_info.html', datastore_root_url=datastore_root_url, resource_id=resource.id) %}
{% set api_info_url = h.url_for(controller='api', action='snippet', ver=1, snippet_path='api_info.html', resource_id=resource.id) %}
<a class="btn btn-success" href="{{ api_info_url }}" data-module="api-info" data-module-template="{{ api_info_url }}" data-loading-text="{{ loading_text }}"><i class="fa fa-flask fa-lg"></i> {{ _('Data API') }}</a>
{% endif %}

0 comments on commit de7db05

Please sign in to comment.