Skip to content

Commit

Permalink
[#652] Only allow aliases in search to simplify checks. Also make thi…
Browse files Browse the repository at this point in the history
…s clear in the docs.
  • Loading branch information
domoritz committed Apr 18, 2013
1 parent c3f3e0b commit aa6e38c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
26 changes: 9 additions & 17 deletions ckanext/datastore/logic/action.py
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion doc/datastore-api.rst
Expand Up @@ -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:

Expand Down

0 comments on commit aa6e38c

Please sign in to comment.