Skip to content

Commit

Permalink
Merge branch '2949-reenable-data-api-button'
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Oct 1, 2012
2 parents 76931ef + 9057d53 commit 40c10e0
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 50 deletions.
5 changes: 3 additions & 2 deletions ckan/controllers/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,9 @@ def resource_read(self, id, resource_id):
get_license_register()[license_id].isopen()
except KeyError:
c.package['isopen'] = False
c.datastore_api = h.url_for('datastore_read', id=c.resource.get('id'),
qualified=True)

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

c.related_count = c.pkg.related_count
return render('package/resource_read.html')
Expand Down
157 changes: 157 additions & 0 deletions ckan/public/base/vendor/bootstrap/js/bootstrap-collapse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/* =============================================================
* bootstrap-collapse.js v2.0.4
* http://twitter.github.com/bootstrap/javascript.html#collapse
* =============================================================
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============================================================ */


!function ($) {

"use strict"; // jshint ;_;


/* COLLAPSE PUBLIC CLASS DEFINITION
* ================================ */

var Collapse = function (element, options) {
this.$element = $(element)
this.options = $.extend({}, $.fn.collapse.defaults, options)

if (this.options.parent) {
this.$parent = $(this.options.parent)
}

this.options.toggle && this.toggle()
}

Collapse.prototype = {

constructor: Collapse

, dimension: function () {
var hasWidth = this.$element.hasClass('width')
return hasWidth ? 'width' : 'height'
}

, show: function () {
var dimension
, scroll
, actives
, hasData

if (this.transitioning) return

dimension = this.dimension()
scroll = $.camelCase(['scroll', dimension].join('-'))
actives = this.$parent && this.$parent.find('> .accordion-group > .in')

if (actives && actives.length) {
hasData = actives.data('collapse')
if (hasData && hasData.transitioning) return
actives.collapse('hide')
hasData || actives.data('collapse', null)
}

this.$element[dimension](0)
this.transition('addClass', $.Event('show'), 'shown')
this.$element[dimension](this.$element[0][scroll])
}

, hide: function () {
var dimension
if (this.transitioning) return
dimension = this.dimension()
this.reset(this.$element[dimension]())
this.transition('removeClass', $.Event('hide'), 'hidden')
this.$element[dimension](0)
}

, reset: function (size) {
var dimension = this.dimension()

this.$element
.removeClass('collapse')
[dimension](size || 'auto')
[0].offsetWidth

this.$element[size !== null ? 'addClass' : 'removeClass']('collapse')

return this
}

, transition: function (method, startEvent, completeEvent) {
var that = this
, complete = function () {
if (startEvent.type == 'show') that.reset()
that.transitioning = 0
that.$element.trigger(completeEvent)
}

this.$element.trigger(startEvent)

if (startEvent.isDefaultPrevented()) return

this.transitioning = 1

this.$element[method]('in')

$.support.transition && this.$element.hasClass('collapse') ?
this.$element.one($.support.transition.end, complete) :
complete()
}

, toggle: function () {
this[this.$element.hasClass('in') ? 'hide' : 'show']()
}

}


/* COLLAPSIBLE PLUGIN DEFINITION
* ============================== */

$.fn.collapse = function (option) {
return this.each(function () {
var $this = $(this)
, data = $this.data('collapse')
, options = typeof option == 'object' && option
if (!data) $this.data('collapse', (data = new Collapse(this, options)))
if (typeof option == 'string') data[option]()
})
}

$.fn.collapse.defaults = {
toggle: true
}

$.fn.collapse.Constructor = Collapse


/* COLLAPSIBLE DATA-API
* ==================== */

$(function () {
$('body').on('click.collapse.data-api', '[data-toggle=collapse]', function ( e ) {
var $this = $(this), href
, target = $this.attr('data-target')
|| e.preventDefault()
|| (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
, option = $(target).data('collapse') ? 'toggle' : $this.data()
$(target).collapse(option)
})
})

}(window.jQuery);
1 change: 1 addition & 0 deletions ckan/public/base/vendor/resource.config
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ vendor =
select2/select2.js
select2/select2.css
bootstrap/js/bootstrap-transition.js
bootstrap/js/bootstrap-collapse.js
bootstrap/js/bootstrap-modal.js
bootstrap/js/bootstrap-alert.js
bootstrap/js/bootstrap-tab.js
Expand Down
92 changes: 46 additions & 46 deletions ckan/templates/ajax_snippets/api_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
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, embedded=true %}
{% snippet 'ajax_snippets/api_info.html', datastore_root_url=datastore_root_url, 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\'' %}

<div{% if not embedded %} class="modal"{% endif %}>
<div class="modal-header">
<h3>
Expand All @@ -18,46 +22,39 @@ <h3>
<div{% if not embedded %} class="modal-body"{% endif %}">
<p><strong>Access resource data via a web API with powerful query
support</strong>. Further information in the <a
href="http://docs.ckan.org/en/latest/using-data-api.html" target="_blank">main
href="http://docs.ckan.org/en/latest/datastore.html" target="_blank">main
CKAN Data API and DataStore documentation</a>.</p>

<div class="accordion" id="accordion2">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" href=".collapse-endpoints">Endpoints &raquo;</a>

<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-endpoints">Endpoints &raquo;</a>
<!-- <h4>Endpoints</h4>-->
</div>
<div class="collapse-endpoints in accordion-body collapse">
<div id="collapse-endpoints" class="in accordion-body collapse">
<div class="accordion-inner">
<p>The Data API builds directly on ElasticSearch, with a resource API
endpoint being equivalent to a single index 'type' in ElasticSearch.
This means you can directly re-use <a
href="http://www.elasticsearch.org/guide/appendix/clients.html"
_target="blank">ElasticSearch
client libraries</a> when connecting to the API endpoint.</p>
<p>The Data API can be accessed via the following actions of the CKAN action API.</p>
<table class="table-condensed table-striped table-bordered">
<thead></thead>
<tbody>
<tr>
<th>Base</th>
<td><code><a href="{{ datastore_root_url }}" target="_blank">{{ datastore_root_url }}</a></code></td>
<th>Create</th>
<td><code><a href="{{ datastore_root_url }}/datastore_create" target="_blank">{{ datastore_root_url }}/datastore_create</a></code></td>
</tr>
<tr>
<th>Query</th>
<td>
<code><a href="{{ datastore_root_url }}/_search" target="_blank">{{ datastore_root_url }}/_search</a></code>
</td>
<th>Update / Insert</th>
<td><code><a href="{{ datastore_root_url }}/datastore_upsert" target="_blank">{{ datastore_root_url }}/datastore_upsert</a></code></td>
</tr>
<tr>
<th>Query example</th>
<td>
<code><a href="{{ datastore_root_url }}/_search?size=5&amp;pretty=true" target="_blank">{{ datastore_root_url }}/_search?size=5&amp;pretty=true</a></code>
</td>
<th>Query</th>
<td><code><a href="{{ datastore_root_url }}/datastore_search" target="_blank">{{ datastore_root_url }}/datastore_search</a></code></td>
</tr>
<tr>
<th>Schema (Mapping)</th>
<td>
<code><a href="{{ datastore_root_url }}/_mapping" target="_blank">{{ datastore_root_url }}/_mapping?pretty=true</a></code>
</td>
<th>Query (via SQL)</th>
<td><code><a href="{{ datastore_root_url }}/datastore_search_sql" target="_blank">{{ datastore_root_url }}/datastore_search_sql</a></code></td>
</tr>

</tbody>
</table>
</div>
Expand All @@ -66,53 +63,52 @@ <h3>

<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" href=".collapse-querying" data-toggle="collapse">Querying
&raquo;</a>
<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-querying">Querying &raquo;</a>
<!-- <h4>Querying</h4>-->
</div>
<div class="collapse-endpoints in accordion-body">
<div id="collapse-querying" class="collapse accordion-body in">
<div class="accordion-inner">
<strong>Query example (first 5 results)</strong>
<p>
<code><a href="{{ datastore_root_url }}/_search?size=5&amp;pretty=true" target="_blank">{{ datastore_root_url }}/_search?size=5&amp;pretty=true</a></code>
<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>
</p>

<strong>Query example (results with 'jones' in <code>title</code> field)</strong>
<strong>Query example (results containing 'jones')</strong>
<p>
<code><a href="{{ datastore_root_url }}/_search?q=title:jones&amp;size=5&amp;pretty=true"
target="_blank">{{ datastore_root_url }}/_search?q=title:jones&amp;size=5&amp;pretty=true</a></code>
<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>
</p>

<strong>Schema (Mapping)</strong>
<strong>Query example (via SQL statement)</strong>
<p>
<code><a href="{{ datastore_root_url }}/_mapping" target="_blank">{{ datastore_root_url }}/_mapping?pretty=true</a></code>
<code><a href="{{sql_example_url}}"
target="_blank">{{sql_example_url}}</a></code>
</p>

<strong>Endpoint (for clients)</strong>
<p>
<code><a href="{{ datastore_root_url }}" target="_blank">{{ datastore_root_url }}</a></code>
</p>
</div>
</div>
</div>

<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" href=".collapse-javascript" data-toggle="collapse">Example: Javascript</a>
<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-javascript">Example: Javascript &raquo;</a>
<!-- <h4>Example: Javascript</h4>-->
</div>
<div class="accordion-body collapse-javascript in">
<div id="collapse-javascript" class="accordion-body collapse">
<div class="accordion-inner">
<p>A simple ajax (JSONP) request to the data API using jQuery.</p>
<pre>
var data = {
size: 5 // get 5 results
q: 'title:jones' // query on the title field for 'jones'
resource_id: '{{resource_id}}', // the resource id
limit: 5, // get 5 results
q: 'jones' // query for 'jones'
};
$.ajax({
url: '{{ datastore_root_url }}/_search',
url: '{{ datastore_root_url }}/datastore_search',
data: data,
dataType: 'jsonp',
success: function(data) {
alert('Total results found: ' + data.hits.total)
alert('Total results found: ' + data.result.total)
}
});</pre>
</div>
Expand All @@ -121,18 +117,22 @@ <h3>

<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" href=".collapse-javascript" data-toggle="collapse">Example: Python</a>
<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-python">Example: Pyhton &raquo;</a>
<!-- <h4>Example: Python</h4>-->
</div>
<div class="accordion-body collapse-javascript in">
<div id="collapse-python" class="accordion-body collapse">
<div class="accordion-inner">
<pre>
import urllib
url = '{{ datastore_root_url }}/_search?size=5&amp;q=title:jones'
url = '{{ datastore_root_url }}/datastore_search?limit=5&amp;q=title:jones'
fileobj = urllib.urlopen(url)
print fileobj.read()
</pre>
</div>
</div>
</div>
</div>

</div>

</div>
2 changes: 1 addition & 1 deletion ckan/templates/package/resource_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</li>
{% endif %}

{% if config.get('ckan.datastore.enabled', False) %}
{% if 'datastore' in config.get('ckan.plugins') %}
<li>{% snippet 'package/snippets/data_api_button.html', resource=res, datastore_root_url=c.datastore_api %}</li>
{% endif %}
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/snippets/data_api_button.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#}
{% if true or resource.webstore_url %}
{% set loading_text = _('Loading...') %}
<a class="btn btn-success" href="{% url_for controller='package', action='api_data', id=resource.id %}" data-module="api-info" data-module-template="{% url_for controller='api', action='snippet', ver=1, snippet_path='api_info.html', datastore_root_url=datastore_root_url %}" data-loading-text="{{ loading_text }}"><i class="icon-beaker icon-large"></i> Data API</a>
<a class="btn btn-success" href="{% url_for controller='package', action='api_data', id=resource.id %}" data-module="api-info" data-module-template="{% url_for controller='api', action='snippet', ver=1, snippet_path='api_info.html', datastore_root_url=datastore_root_url, resource_id=resource.id %}" data-loading-text="{{ loading_text }}"><i class="icon-beaker icon-large"></i> Data API</a>
{% else %}
<a class="btn disabled" rel="tooltip" title="Data API is unavailable for this resource as DataStore is disabled"><i class="icon-beaker icon-large"></i> Data API</a>
{% endif %}

0 comments on commit 40c10e0

Please sign in to comment.