From aa6e38c8750b294445064b96ac9d2ed4dcc291ed Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Thu, 18 Apr 2013 20:54:48 +0200 Subject: [PATCH] [#652] Only allow aliases in search to simplify checks. Also make this clear in the docs. --- ckanext/datastore/logic/action.py | 26 +++++++++----------------- doc/datastore-api.rst | 2 +- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/ckanext/datastore/logic/action.py b/ckanext/datastore/logic/action.py index 32e37454476..859172d1999 100644 --- a/ckanext/datastore/logic/action.py +++ b/ckanext/datastore/logic/action.py @@ -108,24 +108,20 @@ def datastore_upsert(context, data_dict): :rtype: dictionary ''' + model = _get_or_bust(context, 'model') if 'id' in data_dict: data_dict['resource_id'] = data_dict['id'] res_id = _get_or_bust(data_dict, 'resource_id') - data_dict['connection_url'] = pylons.config['ckan.datastore.write_url'] - - resources_sql = sqlalchemy.text(u'''SELECT 1 FROM "_table_metadata" - WHERE name = :id AND alias_of IS NULL''') - results = db._get_engine(None, data_dict).execute(resources_sql, id=res_id) - res_exists = results.rowcount > 0 - - if not res_exists: + if not model.Resource.get(res_id): raise p.toolkit.ObjectNotFound(p.toolkit._( 'Resource "{0}" was not found.'.format(res_id) )) p.toolkit.check_access('datastore_upsert', context, data_dict) + data_dict['connection_url'] = pylons.config.get('ckan.datastore.write_url') + result = db.upsert(context, data_dict) result.pop('id', None) result.pop('connection_url') @@ -147,24 +143,20 @@ def datastore_delete(context, data_dict): :rtype: dictionary ''' + model = _get_or_bust(context, 'model') if 'id' in data_dict: data_dict['resource_id'] = data_dict['id'] res_id = _get_or_bust(data_dict, 'resource_id') - data_dict['connection_url'] = pylons.config['ckan.datastore.write_url'] - - resources_sql = sqlalchemy.text(u'''SELECT 1 FROM "_table_metadata" - WHERE name = :id AND alias_of IS NULL''') - results = db._get_engine(None, data_dict).execute(resources_sql, id=res_id) - res_exists = results.rowcount > 0 - - if not res_exists: + if not model.Resource.get(res_id): raise p.toolkit.ObjectNotFound(p.toolkit._( 'Resource "{0}" was not found.'.format(res_id) )) p.toolkit.check_access('datastore_delete', context, data_dict) + data_dict['connection_url'] = pylons.config.get('ckan.datastore.write_url') + result = db.delete(context, data_dict) result.pop('id', None) result.pop('connection_url') @@ -284,7 +276,7 @@ def datastore_search_sql(context, data_dict): raise p.toolkit.ValidationError({ 'query': ['Query is not a single statement or contains semicolons.'], 'hint': [('If you want to use semicolons, use character encoding' - '(; equals chr(59)) and string concatenation (||). ')] + '(; equals chr(59)) and string concatenation (||). ')] }) p.toolkit.check_access('datastore_search', context, data_dict) diff --git a/doc/datastore-api.rst b/doc/datastore-api.rst index de803cb0362..9853685ee5e 100644 --- a/doc/datastore-api.rst +++ b/doc/datastore-api.rst @@ -116,7 +116,7 @@ You can find more information about the formatting of dates in the `date/time ty Resource aliases ---------------- -A resource in the DataStore can have multiple aliases that are easier to remember than the resource id. Aliases can be created and edited with the :meth:`~ckanext.datastore.logic.action.datastore_create` API endpoint. All aliases can be found in a special view called ``_table_metadata``. See :ref:`db_internals` for full reference. +A resource in the DataStore can have multiple aliases that are easier to remember than the resource id. Aliases can be created and edited with the :meth:`~ckanext.datastore.logic.action.datastore_create` API endpoint. All aliases can be found in a special view called ``_table_metadata``. See :ref:`db_internals` for full reference. Aliases can only be used in the search. .. _datastore_search_htsql: