Skip to content

Commit

Permalink
[#2733] add datastore_active flag to resource dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Aug 23, 2012
1 parent 7458492 commit 35eef77
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
36 changes: 36 additions & 0 deletions ckanext/datastore/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ckan.plugins as p
import ckanext.datastore.logic.action as action
import ckanext.datastore.logic.auth as auth
import ckanext.datastore.db as db
import ckan.logic as logic


class DatastoreException(Exception):
Expand All @@ -21,6 +23,40 @@ def configure(self, config):
error_msg = 'ckan.datastore_write_url not found in config'
raise DatastoreException(error_msg)

## Do light wrapping around action function to add datastore_active
## to resource dict. Not using IAction extension as this prevents other plugins
## from having a custom resource_read.

# Make sure actions are cached
resource_show = p.toolkit.get_action('resource_show')

def new_resource_show(context, data_dict):
engine = db._get_engine(
context,
{'connection_url': config['ckan.datastore_write_url']}
)
new_data_dict = resource_show(context, data_dict)
try:
connection = engine.connect()
result = connection.execute(
'select 1 from pg_tables where tablename = %s',
new_data_dict['id']
).fetchone()
if result:
new_data_dict['datastore_active'] = True
else:
new_data_dict['datastore_active'] = False
finally:
connection.close()
return new_data_dict

## Make sure do not run many times if configure is called repeatedly
## as in tests.
if not hasattr(resource_show, '_datastore_wrapped'):
new_resource_show._datastore_wrapped = True
logic._actions['resource_show'] = new_resource_show


def get_actions(self):
return {'datastore_create': action.datastore_create,
'datastore_delete': action.datastore_delete,
Expand Down
19 changes: 18 additions & 1 deletion ckanext/datastore/tests/test_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
class TestDatastoreCreate(tests.WsgiAppCase):
sysadmin_user = None
normal_user = None
p.load('datastore')

@classmethod
def setup_class(cls):
p.load('datastore')
ctd.CreateTestData.create()
cls.sysadmin_user = model.User.get('testsysadmin')
cls.normal_user = model.User.get('annafan')
Expand Down Expand Up @@ -141,6 +141,15 @@ def test_create_basic(self):
{'book': 'annakarenina', 'author': ['tolstoy', 'putin']},
{'book': 'warandpeace'}] # treat author as null
}
### Firstly test to see if resource things it has datastore table
postparams = '%s=1' % json.dumps({'id': resource.id})
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/resource_show', params=postparams,
extra_environ=auth)
res_dict = json.loads(res.body)
assert res_dict['result']['datastore_active'] == False


postparams = '%s=1' % json.dumps(data)
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/datastore_create', params=postparams,
Expand All @@ -167,6 +176,14 @@ def test_create_basic(self):
assert results.rowcount == 2
model.Session.remove()

# check to test to see if resource now has a datastore table
postparams = '%s=1' % json.dumps({'id': resource.id})
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/resource_show', params=postparams,
extra_environ=auth)
res_dict = json.loads(res.body)
assert res_dict['result']['datastore_active'] == True

####### insert again simple
data2 = {
'resource_id': resource.id,
Expand Down

0 comments on commit 35eef77

Please sign in to comment.