Skip to content

Commit

Permalink
[#3816] Run Query button on resource info page
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Oct 13, 2017
1 parent 86aa875 commit 1e40760
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ckanext/datastore/controller.py
Expand Up @@ -112,6 +112,31 @@ def dictionary(self, id, resource_id):
extra_vars={'fields': fields})


def run_query(self, resource_id):
try:
resource = get_action('resource_show')(None, {'id': resource_id})
except (ObjectNotFound, NotAuthorized):
abort(404, _('Resource not found'))

if not resource.get('query'):
# must be a resource with a query defined
abort(404, _('Resource not found'))

if request.method == 'POST':
get_action('datastore_create')(None, {
'resource_id': resource_id,
'force': True,
'materialized_view_sql': resource['query']})

h.flash_success(_('Resource data updated.'))

h.redirect_to(
controller='package',
action='resource_read',
id=resource['package_id'],
resource_id=resource_id)


def dump_to(resource_id, output, fmt, offset, limit, options):
if fmt == 'csv':
writer_factory = csv_writer
Expand Down
4 changes: 4 additions & 0 deletions ckanext/datastore/plugin.py
Expand Up @@ -135,6 +135,10 @@ def before_map(self, m):
'resource_dictionary', '/dataset/{id}/dictionary/{resource_id}',
controller='ckanext.datastore.controller:DatastoreController',
action='dictionary', ckan_icon='book')
m.connect(
'/datastore/query/{resource_id}',
controller='ckanext.datastore.controller:DatastoreController',
action='run_query')
return m

# IResourceController
Expand Down
12 changes: 12 additions & 0 deletions ckanext/datastore/templates-bs2/package/resource_read.html
Expand Up @@ -33,5 +33,17 @@ <h2>{{ _('Data Dictionary') }}</h2>
</table>
</div>
{% endif %}
{% if res.query %}
<div class="module-content">
<h2>{{ _('Resource Query') }}</h2>
<p>{{ _('Click Run Query to update the data in this resource.') }}</p>
<form class="form-horizontal" method="post" data-module="basic-form"
action="{% url_for
controller='ckanext.datastore.controller:DatastoreController',
action='run_query', resource_id=res.id %}">
<button class="btn btn-primary" name="run" value="query" type="submit">{{ _('Run Query') }}</button>
</form>
</div>
{% endif %}
{{ super() }}
{% endblock %}
12 changes: 12 additions & 0 deletions ckanext/datastore/templates/package/resource_read.html
Expand Up @@ -33,5 +33,17 @@ <h2>{{ _('Data Dictionary') }}</h2>
</div>
{% endblock %}
{% endif %}
{% if res.query %}
<div class="module-content">
<h2>{{ _('Resource Query') }}</h2>
<p>{{ _('Click Run Query to update the data in this resource.') }}</p>
<form class="form-horizontal" method="post" data-module="basic-form"
action="{% url_for
controller='ckanext.datastore.controller:DatastoreController',
action='run_query', resource_id=res.id %}">
<button class="btn btn-primary" name="run" value="query" type="submit">{{ _('Run Query') }}</button>
</form>
</div>
{% endif %}
{{ super() }}
{% endblock %}

0 comments on commit 1e40760

Please sign in to comment.