Skip to content

Commit

Permalink
test-datastore-choices schema
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Jul 29, 2016
1 parent a332801 commit cded673
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ckanext/scheming/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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']]

Expand Down
48 changes: 48 additions & 0 deletions ckanext/scheming/tests/test_datastore_choices.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
1 change: 1 addition & 0 deletions test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test_subclass.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cded673

Please sign in to comment.