Skip to content

Commit

Permalink
[#2562] ckan.datastore.resource_query.enabled setting
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Feb 26, 2018
1 parent fe359de commit cfd84ca
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -2566,6 +2566,13 @@ def clean_html(html):
return bleach_clean(unicode(html))


@core_helper
def resource_query_enabled():
'''Placeholder to report whether resource query feature is available,
overridden by datastore extension'''
return False


core_helper(flash, name='flash')
core_helper(localised_number)
core_helper(localised_SI_number)
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/snippets/resource_form.html
Expand Up @@ -23,7 +23,7 @@
{% set is_upload = (data.url_type == 'upload') %}
{{ form.image_upload(data, errors, field_url='url', field_upload='upload', field_clear='clear_upload',
is_upload_enabled=h.uploads_enabled(), is_url=data.url and not is_upload, is_upload=is_upload,
upload_label=_('Data'), url_label=_('URL'), placeholder=_('http://example.com/external-data.csv'), field_name='name', query=true) }}
upload_label=_('Data'), url_label=_('URL'), placeholder=_('http://example.com/external-data.csv'), field_name='name', query=h.resource_query_enabled()) }}
{% endblock %}

{% block basic_fields_name %}
Expand Down
5 changes: 4 additions & 1 deletion ckanext/datastore/backend/postgres.py
Expand Up @@ -1622,9 +1622,12 @@ def configure(self, config):
error_msg = 'ckan.datastore.read_url not found in config'
raise DatastoreException(error_msg)

# Check whether users have disabled datastore_search_sql
# Check whether users have disabled datastore_search_sql,
# resource queries
self.enable_sql_search = toolkit.asbool(
self.config.get('ckan.datastore.sqlsearch.enabled', True))
self.enable_resource_query = self.enable_sql_search and toolkit.asbool(
self.config.get('ckan.datastore.resource_query.enabled', True))

# Check whether we are running one of the paster commands which means
# that we should ignore the following tests.
Expand Down
6 changes: 6 additions & 0 deletions ckanext/datastore/helpers.py
Expand Up @@ -9,6 +9,7 @@
from six import string_types

from ckan.plugins.toolkit import get_action, ObjectNotFound, NotAuthorized
from ckanext.datastore.backend import DatastoreBackend

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -164,3 +165,8 @@ def datastore_dictionary(resource_id):
if not f['id'].startswith(u'_')]
except (ObjectNotFound, NotAuthorized):
return []


def resource_query_enabled():
backend = DatastoreBackend.get_active_backend()
return getattr(backend, 'enable_resource_query', False)
9 changes: 7 additions & 2 deletions ckanext/datastore/logic/action.py
Expand Up @@ -38,8 +38,7 @@ def datastore_create(context, data_dict):
``resource_id``.
To create a table with data from other DataStore tables pass a
select statement as the ``create_table_as_sql`` parameter. Update
data after the source data has changed by calling ``datastore_refresh``.
select statement as the ``create_table_as_sql`` parameter.
See :ref:`fields` and :ref:`records` for details on how to lay out records.
Expand All @@ -56,6 +55,9 @@ def datastore_create(context, data_dict):
:type aliases: list or comma separated string
:param create_table_as_sql: a single SQL select statement to create
table based on other DataStore data. (optional)
Both :ref:`ckan.datastore.sqlsearch.enabled` and
:ref:`ckan.datastore.resource_query.enabled` must be set
to ``True`` to use this parameter.
:type create_table_as_sql: string
:param fields: fields/columns and their extra metadata.
(optional)
Expand Down Expand Up @@ -115,6 +117,9 @@ def datastore_create(context, data_dict):
except p.toolkit.NotAuthorized as e:
raise p.toolkit.ValidationError(
{'create_table_as_sql': e.message})
except KeyError:
raise p.toolkit.ValidationError(
{'create_table_as_sql': 'Feature not enabled'})

if 'resource' in data_dict and 'resource_id' in data_dict:
raise p.toolkit.ValidationError({
Expand Down
9 changes: 7 additions & 2 deletions ckanext/datastore/plugin.py
Expand Up @@ -274,7 +274,9 @@ def datastore_search(self, context, data_dict, fields_types, query_dict):

def get_helpers(self):
return {
'datastore_dictionary': datastore_helpers.datastore_dictionary}
'datastore_dictionary': datastore_helpers.datastore_dictionary,
'resource_query_enabled': datastore_helpers.resource_query_enabled,
}

# IForkObserver

Expand All @@ -289,7 +291,10 @@ def before_fork(self):
# IValidators

def get_validators(self):
return {u'datastore_resource_query': datastore_resource_query}
if getattr(self.backend, 'enable_resource_query', False):
return {u'datastore_resource_query': datastore_resource_query}
else:
return {}


def datastore_resource_query(key, data, errors, context):
Expand Down
17 changes: 17 additions & 0 deletions doc/maintaining/configuration.rst
Expand Up @@ -270,6 +270,23 @@ Default value: ``True``
This option allows you to disable the datastore_search_sql action function, and
corresponding API endpoint if you do not wish it to be activated.

.. _ckan.datastore.resource_query.enabled:

ckan.datastore.resource_query.enabled
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Example::

ckan.datastore.resource_query.enabled = False

Default value: ``True``

This option allows you to disable the resource query feature and the
datastore_create ``create_table_as_sql`` parameter it is based on.
Setting :ref:`ckan.datastore.sqlsearch.enabled` to ``False`` also disables
resource queries.


Site Settings
-------------

Expand Down

0 comments on commit cfd84ca

Please sign in to comment.