diff --git a/ckanext/scheming/helpers.py b/ckanext/scheming/helpers.py index 31d27f36..bddb500a 100644 --- a/ckanext/scheming/helpers.py +++ b/ckanext/scheming/helpers.py @@ -91,7 +91,7 @@ def scheming_datastore_choices(field): When columns aren't specified the first column is used as value and second column used as label. """ - import ckanapi + from ckanapi import LocalCKAN, NotFound, NotAuthorized resource_id = field['datastore_choices_resource'] limit = field.get('datastore_choices_limit', 1000) columns = field.get('datastore_choices_columns') @@ -101,11 +101,14 @@ def scheming_datastore_choices(field): # anon user must be able to read choices or this helper # could be used to leak data from private datastore tables - lc = ckanapi.LocalCKAN(user='') - result = lc.action.datastore_search( - resource_id=resource_id, - limit=limit, - fields=fields) + lc = LocalCKAN(username='') + try: + result = lc.action.datastore_search( + resource_id=resource_id, + limit=limit, + fields=fields) + except (NotFound, NotAuthorized): + return [] return [{'value': r[0], 'label': r[1]} for r in result['records']] diff --git a/ckanext/scheming/tests/test_datastore_choices.json b/ckanext/scheming/tests/test_datastore_choices.json new file mode 100644 index 00000000..6ef1b2d2 --- /dev/null +++ b/ckanext/scheming/tests/test_datastore_choices.json @@ -0,0 +1,48 @@ +{ + "scheming_version": 1, + "dataset_type": "test-datastore-choices", + "about_url": "http://github.com/ckan/ckanext-scheming", + "dataset_fields": [ + { + "field_name": "title", + "label": "Title", + "preset": "title", + "form_placeholder": "eg. Larry, Peter, Susan" + }, + { + "field_name": "name", + "label": "URL", + "preset": "dataset_slug", + "form_placeholder": "eg. camel-no-5" + }, + { + "field_name": "category", + "label": "Category", + "help_text": "Make and model", + "help_inline": true, + "preset": "select", + "choices_helper": "scheming_datastore_choices", + "datastore_choices_resource": "category-choices" + }, + { + "field_name": "personality", + "label": "Personality", + "preset": "multiple_checkbox", + "choices_helper": "scheming_datastore_choices", + "datastore_choices_resource": "personality-choices", + "datastore_choices_columns": { + "value": "valcol", + "label": "labelcol" + } + } + ], + "resource_fields": [ + { + "field_name": "url", + "label": "Photo", + "preset": "resource_url_upload", + "form_placeholder": "http://example.com/my-camel-photo.jpg", + "upload_label": "Photo" + } + ] +} diff --git a/test.ini b/test.ini index d6027ac8..bf1bbaf3 100644 --- a/test.ini +++ b/test.ini @@ -15,6 +15,7 @@ ckan.plugins = scheming_datasets scheming_groups scheming_organizations scheming_test_plugin scheming.dataset_schemas = ckanext.scheming:ckan_dataset.json ckanext.scheming.tests:test_schema.json + ckanext.scheming.tests:test_datastore_choices.json scheming.organization_schemas = ckanext.scheming:org_with_dept_id.json scheming.group_schemas = ckanext.scheming:group_with_bookface.json diff --git a/test_subclass.ini b/test_subclass.ini index badc50e3..a1921daa 100644 --- a/test_subclass.ini +++ b/test_subclass.ini @@ -14,6 +14,7 @@ use = config:../ckan/test-core.ini ckan.plugins = scheming_test_subclass scheming_test_plugin scheming.dataset_schemas = ckanext.scheming.tests:test_schema.json + ckanext.scheming.tests:test_datastore_choices.json ckan.site_logo = /img/logo_64px_wide.png ckan.favicon = /images/icons/ckan.ico