Skip to content

Commit

Permalink
Merge pull request #7763 from mutantsan/7710-fix-datastore-and-datapu…
Browse files Browse the repository at this point in the history
…sher-templates-init

[#7710] add datastore and datapusher templates-bs3
  • Loading branch information
smotornyuk committed Sep 7, 2023
2 parents 169e9df + 4b94dcf commit 771ce03
Show file tree
Hide file tree
Showing 16 changed files with 625 additions and 0 deletions.
88 changes: 88 additions & 0 deletions ckanext/datapusher/templates-bs3/datapusher/resource_data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{% extends "package/resource_edit_base.html" %}

{% block subtitle %}{{ h.dataset_display_name(pkg) }} - {{ h.resource_display_name(res) }}{% endblock %}

{% block primary_content_inner %}

{% set action = h.url_for('datapusher.resource_data', id=pkg.name, resource_id=res.id) %}
{% set show_table = true %}

<form method="post" action="{{ action }}" class="datapusher-form">
<button class="btn btn-primary" name="save" type="submit">
<i class="fa fa-cloud-upload"></i> {{ _('Upload to DataStore') }}
</button>
</form>

{% if status.error and status.error.message %}
{% set show_table = false %}
<div class="alert alert-error">
<strong>{{ _('Upload error:') }}</strong> {{ status.error.message }}
</div>
{% elif status.task_info and status.task_info.error %}
<div class="alert alert-error">
{% if status.task_info.error is string %}
{# DataPusher < 0.0.3 #}
<strong>{{ _('Error:') }}</strong> {{ status.task_info.error }}
{% elif status.task_info.error is mapping %}
<strong>{{ _('Error:') }}</strong> {{ status.task_info.error.message }}
{% for error_key, error_value in status.task_info.error.items() %}
{% if error_key != "message" and error_value %}
<br>
<strong>{{ error_key }}</strong>:
{{ error_value }}
{% endif %}
{% endfor %}
{% elif status.task_info.error is iterable %}
<strong>{{ _('Error traceback:') }}</strong>
<pre>{{ ''.join(status.task_info.error) }}</pre>
{% endif %}
</div>
{% endif %}

<table class="table table-bordered">
<colgroup>
<col width="150">
<col>
</colgroup>
<tr>
<th>{{ _('Status') }}</th>
<td>{{ h.datapusher_status_description(status) }}</td>
</tr>
<tr>
<th>{{ _('Last updated') }}</th>
{% if status.status %}
<td><span class="date" title="{{ h.render_datetime(status.last_updated, with_hours=True) }}">{{ h.time_ago_from_timestamp(status.last_updated) }}</span></td>
{% else %}
<td>{{ _('Never') }}</td>
{% endif %}
</tr>
</table>

{% if status.status and status.task_info and show_table %}
<h3>{{ _('Upload Log') }}</h3>
<ul class="activity">
{% for item in status.task_info.logs|sort(attribute='timestamp') %}
{% set icon = 'ok' if item.level == 'INFO' else 'exclamation' %}
{% set class = ' failure' if icon == 'exclamation' else ' success' %}
{% set popover_content = 'test' %}
<li class="item no-avatar{{ class }}">
<i class="fa icon fa-{{ icon }}"></i>
<p>
{% for line in item.message.strip().split('\n') %}
{{ line | urlize }}<br>
{% endfor %}
<span class="date" title="{{ h.render_datetime(item.timestamp, with_hours=True) }}">
{{ h.time_ago_from_timestamp(item.timestamp) }}
<a href="#" data-target="popover" data-content="<dl>{% for key, value in item.items() %}<dt>{{ key }}</dt><dd>{{ h.clean_html(value|string) }}</dd>{% endfor %}</dl>" data-html="true">{{ _('Details') }}</a>
</span>
</p>
</li>
{% endfor %}
<li class="item no-avatar">
<i class="fa icon fa-info"></i>
<p class="muted">{{ _('End of log') }}</p>
</li>
</ul>
{% endif %}

{% endblock %}
16 changes: 16 additions & 0 deletions ckanext/datapusher/templates-bs3/package/resource_edit_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% ckan_extends %}

{% block scripts %}
{{ super() }}
{% asset 'ckanext-datapusher/datapusher' %}
{% endblock scripts %}

{% block styles %}
{{ super() }}
{% asset 'ckanext-datapusher/datapusher-css' %}
{% endblock %}

{% block inner_primary_nav %}
{{ super() }}
{{ h.build_nav_icon('datapusher.resource_data', _('DataStore'), id=pkg.name, resource_id=res.id) }}
{% endblock %}
137 changes: 137 additions & 0 deletions ckanext/datastore/templates-bs3/ajax_snippets/api_info.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{#
Displays information about accessing a resource via the API.

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

Example

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

#}

{% 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 fade"{% endif %}>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3>
{{ _('CKAN Data API') }}
</h3>
</div>
<div{% if not embedded %} class="modal-body"{% endif %}>
<p><strong>{{ _('Access resource data via a web API with powerful query support') }}</strong>.
{% trans %}
Further information in the <a
href="http://docs.ckan.org/en/latest/maintaining/datastore.html" target="_blank">main
CKAN Data API and DataStore documentation</a>.</p>
{% endtrans %}
<div class="panel-group" id="accordion2">
<div class="panel panel-default">
<div class="panel-heading">

<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-endpoints">{{ _('Endpoints') }} &raquo;</a>
</div>
<div id="collapse-endpoints" class="in panel-collapse collapse">
<div class="panel-body">
<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 scope="row">{{ _('Create') }}</th>
<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>{{ 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>{{ 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>{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search_sql', qualified=True) }}</code></td>
</tr>

</tbody>
</table>
</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-querying">{{ _('Querying') }} &raquo;</a>
</div>
<div id="collapse-querying" class="collapse panel-collapse in">
<div class="panel-body">
<strong>{{ _('Query example (first 5 results)') }}</strong>
<p>
<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" rel="nofollow">{{ 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="{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', resource_id=resource_id, q='jones', qualified=True) }}" target="_blank" rel="nofollow">{{ 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>
<p>
<code><a href="{{sql_example_url}}" target="_blank" rel="nofollow">{{ sql_example_url }}</a></code>
</p>

</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-javascript">{{ _('Example: Javascript') }} &raquo;</a>
</div>
<div id="collapse-javascript" class="panel-collapse collapse">
<div class="panel-body">
<p>{{ _('A simple ajax (JSONP) request to the data API using jQuery.') }}</p>
<pre>
var data = {
resource_id: '{{resource_id}}', // the resource id
limit: 5, // get 5 results
q: 'jones' // query for 'jones'
};
$.ajax({
url: '{{ h.url_for(controller='api', action='action', ver=3, logic_function='datastore_search', qualified=True) }}',
data: data,
dataType: 'jsonp',
success: function(data) {
alert('Total results found: ' + data.result.total)
}
});</pre>
</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="accordion2" href="#collapse-python">{{ _('Example: Python') }} &raquo;</a>
</div>
<div id="collapse-python" class="panel-collapse collapse">
<div class="panel-body">
<pre>
import urllib
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>
</div>
</div>
</div>
</div>

</div>
</div>
</div>

</div>
40 changes: 40 additions & 0 deletions ckanext/datastore/templates-bs3/datastore/api_examples/curl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% call register_example('curl', 'button_label') %}
curl
{% endcall %}


{% call register_example('curl', 'request_limit') %}
<pre class="example-curl"><code class="language-bash"
>curl {{ h.url_for('api.action', logic_function='datastore_search', qualified=True) }} \
-H"Authorization:$API_TOKEN" -d '
{
"resource_id": "{{ resource_id }}",
"limit": 5,
"q": "jones"
}'</code></pre>
{% endcall %}


{% call register_example('curl', 'request_filter') %}
<pre class="example-curl"><code class="language-bash"
>curl {{ h.url_for('api.action', logic_function='datastore_search', qualified=True) }} \
-H"Authorization:$API_TOKEN" -d '
{
"resource_id": "{{ resource_id }}",
"filters": {
"subject": ["watershed", "survey"],
"stage": "active"
}
}'</code></pre>
{% endcall %}


{% call register_example('curl', 'request_sql') %}
<pre class="example-curl"><code class="language-bash"
>curl {{ h.url_for('api.action', logic_function='datastore_search_sql', qualified=True) }} \
-H"Authorization:$API_TOKEN" -d @- &lt;&lt;END
{
"sql": "SELECT * FROM \"{{ resource_id }}\" WHERE title LIKE 'jones'"
}
END</code></pre>
{% endcall %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% call register_example('javascript', 'button_label') %}
JavaScript
{% endcall %}

{% call register_example('javascript', 'request_limit') %}
<pre class="example-javascript"><code class="language-javascript"
>const resp = await fetch(`{{
h.url_for('api.action', logic_function='datastore_search', qualified=True) }}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: API_TOKEN
},
body: JSON.stringify({
resource_id: '{{ resource_id }}',
limit: 5,
q: 'jones'
})
})
await resp.json()</code></pre>
{% endcall %}


{% call register_example('javascript', 'request_filter') %}
<pre class="example-javascript"><code class="language-javascript"
>const resp = await fetch(`{{
h.url_for('api.action', logic_function='datastore_search', qualified=True) }}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: API_TOKEN
},
body: JSON.stringify({resource_id: '{{ resource_id }}', filters: {
subject: ['watershed', 'survey'],
stage: 'active'
}})})
await resp.json()</code></pre>
{% endcall %}


{% call register_example('javascript', 'request_sql') %}
<pre class="example-javascript"><code class="language-javascript"
>const resp = await fetch(`{{
h.url_for('api.action', logic_function='datastore_search_sql', qualified=True) }}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: API_TOKEN
},
body: JSON.stringify({
sql: `SELECT * FROM "{{ resource_id }}" WHERE title LIKE 'jones'`
})
})
await resp.json()</code></pre>
{% endcall %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% call register_example('powershell', 'button_label') %}
PowerShell
{% endcall %}


{% call register_example('powershell', 'request_limit') %}
<pre class="example-powershell"><code class="language-powershell"
>$json = @'
{
"resource_id": "{{resource_id}}",
"limit": 5,
"q": "jones"
}
'@
$response = Invoke-RestMethod {{ h.url_for('api.action', logic_function='datastore_search', qualified=True) }}`
-Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records</code></pre>
{% endcall %}


{% call register_example('powershell', 'request_filter') %}
<pre class="example-powershell"><code class="language-powershell"
>$json = @'
{
"resource_id": "{{resource_id}}",
"filters": {
"subject": ["watershed", "survey"],
"stage": "active"
}
}
'@
$response = Invoke-RestMethod {{ h.url_for('api.action', logic_function='datastore_search', qualified=True) }}`
-Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records</code></pre>
{% endcall %}


{% call register_example('powershell', 'request_sql') %}
<pre class="example-powershell"><code class="language-powershell"
>$json = @'
{
"sql": "SELECT * from \"{{resource_id}}\" WHERE title LIKE 'jones'"
}
'@
$response = Invoke-RestMethod {{ h.url_for('api.action', logic_function='datastore_search_sql', qualified=True) }}`
-Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records</code></pre>
{% endcall %}

0 comments on commit 771ce03

Please sign in to comment.