Skip to content

Commit

Permalink
add patch
Browse files Browse the repository at this point in the history
  • Loading branch information
tino097 committed Sep 26, 2017
1 parent c3ff043 commit 9ba6f08
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ckan/controllers/package.py
Expand Up @@ -1092,7 +1092,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
9 changes: 9 additions & 0 deletions ckan/lib/helpers.py
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 @@ -2353,6 +2354,14 @@ def radio(selected, id, checked):
value="%s" type="radio">') % (selected, id, selected, id))


@core_helper
def sanitize_id(id_):
'''Given an id (uuid4), if it has any invalid characters it raises
ValueError.
'''
return str(uuid.UUID(id_))


core_helper(flash, name='flash')
core_helper(localised_number)
core_helper(localised_SI_number)
Expand Down
25 changes: 12 additions & 13 deletions ckan/templates/ajax_snippets/api_info.html
@@ -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,13 +69,12 @@ <h3>
<div class="accordion-inner">
<strong>{{ _('Query example (first 5 results)') }}</strong>
<p>
<code><a href="{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&limit=5" target="_blank">{{ datastore_root_url }}/datastore_search?resource_id={{resource_id}}&limit=5</a></code>
<code><a href="{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5, qualified=True) }}" target="_blank">{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', resource_id=resource_id, limit=5, qualified=True) }}</a></code>
</p>

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

<strong>{{ _('Query example (via SQL statement)') }}</strong>
Expand All @@ -102,7 +101,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 @@ -121,7 +120,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 ckan/templates/package/resource_read.html
Expand Up @@ -46,7 +46,7 @@
</li>
{% endif %}
{% if 'datastore' in g.plugins %}
<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 %}
</ul>
Expand Down
3 changes: 1 addition & 2 deletions ckan/templates/package/snippets/data_api_button.html
@@ -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="icon-beaker icon-large"></i> {{ _('Data API') }}</a>
{% endif %}

0 comments on commit 9ba6f08

Please sign in to comment.