From 01f0cba13cf1f4cd59e753af50026c7aeb9f2300 Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Thu, 27 Nov 2014 14:02:34 -0500 Subject: [PATCH] make choices not special --- README.md | 8 ++----- ckanext/__init__.py | 7 ++++++ ckanext/scheming/camel_photos.json | 17 +++++--------- ckanext/scheming/presets.json | 7 ++++++ .../scheming/snippets/form_field.html | 6 +---- ckanext/scheming/tests/test_display.py | 3 ++- ckanext/scheming/tests/test_validation.py | 22 +++++++++---------- 7 files changed, 36 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 55fc147a..115bd1b2 100644 --- a/README.md +++ b/README.md @@ -157,8 +157,7 @@ you added to your that aren't handled by this extension. This extension includes the following form snippets: * [text.html](ckanext/scheming/templates/scheming/form_snippets/text.html) - - a simple text field for free-form text or numbers (default when no - choices list is given) + a simple text field for free-form text or numbers (default) * [large_text.html](ckanext/scheming/templates/scheming/form_snippets/large_text.html) - a larger text field, typically used for the title * [slug.html](ckanext/scheming/templates/scheming/form_snippets/slug.html) - @@ -172,7 +171,7 @@ This extension includes the following form snippets: * [upload.html](ckanext/scheming/templates/scheming/form_snippets/upload.html) - an upload field for resource files * [select.html](ckanext/scheming/templates/scheming/form_snippets/select.html) - - a select box (default when choices list is given) + a select box ### `display_snippet` @@ -241,9 +240,6 @@ single-choice fields. The `label`s are human-readable text for the dataset editing form and the `value`s are stored in the dataset field or are used for tag names in tag vocabularies. -A validator is automatically added for creating or updating datasets -that only allows values from this list. - ### `tag_vocabulary` diff --git a/ckanext/__init__.py b/ckanext/__init__.py index e69de29b..2e2033b3 100644 --- a/ckanext/__init__.py +++ b/ckanext/__init__.py @@ -0,0 +1,7 @@ +# this is a namespace package +try: + import pkg_resources + pkg_resources.declare_namespace(__name__) +except ImportError: + import pkgutil + __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/ckanext/scheming/camel_photos.json b/ckanext/scheming/camel_photos.json index a998a4ac..57cf27de 100644 --- a/ckanext/scheming/camel_photos.json +++ b/ckanext/scheming/camel_photos.json @@ -6,16 +6,13 @@ { "field_name": "title", "label": "Title", - "form_snippet": "large_text.html", - "validators": "if_empty_same_as(name) unicode", - "form_attrs": {"data-module": "slug-preview-target"}, + "preset": "title", "form_placeholder": "eg. Larry, Peter, Susan" }, { "field_name": "name", "label": "URL", - "form_snippet": "slug.html", - "validators": "not_empty unicode name_validator package_name_validator", + "preset": "dataset_slug", "form_placeholder": "eg. camel-no-5" }, { @@ -25,8 +22,9 @@ "form_placeholder": "eg. 2" }, { - "field_name": "class", - "label": "Class", + "field_name": "category", + "label": "Category", + "preset": "select", "choices": [ { "value": "bactrian", @@ -60,11 +58,8 @@ { "field_name": "url", "label": "Photo", - "validators": "not_empty unicode remove_whitespace", - "form_snippet": "upload.html", + "preset": "resource_url_upload", "form_placeholder": "http://example.com/my-camel-photo.jpg", - "upload_field": "upload", - "upload_clear": "clear_upload", "upload_label": "Photo" }, { diff --git a/ckanext/scheming/presets.json b/ckanext/scheming/presets.json index abfaf6ef..694c5794 100644 --- a/ckanext/scheming/presets.json +++ b/ckanext/scheming/presets.json @@ -59,6 +59,13 @@ "data-module-source": "/api/2/util/resource/format_autocomplete?incomplete=?" } } + }, + { + "preset_name": "select", + "values": { + "form_snippet": "select.html", + "display_snippet": "select.html" + } } ] } diff --git a/ckanext/scheming/templates/scheming/snippets/form_field.html b/ckanext/scheming/templates/scheming/snippets/form_field.html index 4e578cac..758b3ea6 100644 --- a/ckanext/scheming/templates/scheming/snippets/form_field.html +++ b/ckanext/scheming/templates/scheming/snippets/form_field.html @@ -4,11 +4,7 @@ {%- set form_snippet = field.form_snippet -%} {%- if not form_snippet -%} - {%- if 'choices' in field -%} - {%- set form_snippet = 'select.html' -%} - {%- else -%} - {%- set form_snippet = 'text.html' -%} - {%- endif -%} + {%- set form_snippet = 'text.html' -%} {%- endif -%} {%- if '/' not in form_snippet -%} diff --git a/ckanext/scheming/tests/test_display.py b/ckanext/scheming/tests/test_display.py index 27b7e107..d889577f 100644 --- a/ckanext/scheming/tests/test_display.py +++ b/ckanext/scheming/tests/test_display.py @@ -42,7 +42,8 @@ def test_choice_field_shows_labels(self): user=user, type='camel-photos', name='with-choice', - **{'class': 'hybrid'}) + category='hybrid', + ) app = self._get_test_app() response = app.get(url='/dataset/with-choice') assert_true('Hybrid Camel' in response.body) diff --git a/ckanext/scheming/tests/test_validation.py b/ckanext/scheming/tests/test_validation.py index e17ef64f..7bc0a7e3 100644 --- a/ckanext/scheming/tests/test_validation.py +++ b/ckanext/scheming/tests/test_validation.py @@ -18,17 +18,17 @@ def test_converter_name(self): class TestChoices(object): def test_choice_field_only_accepts_given_choices(self): lc = LocalCKAN() - assert_raises(ValidationError, lc.action.package_create, **{ - 'type':'camel-photos', - 'name':'fred', - 'class': 'rocker', - }) + assert_raises(ValidationError, lc.action.package_create, + type='camel-photos', + name='fred', + category='rocker', + ) def test_choice_field_accepts_valid_choice(self): lc = LocalCKAN() - d = lc.action.package_create(**{ - 'type':'camel-photos', - 'name':'fred', - 'class': 'f2hybrid', - }) - assert_equals(d['class'], 'f2hybrid') + d = lc.action.package_create( + type='camel-photos', + name='fred', + category='f2hybrid', + ) + assert_equals(d['category'], 'f2hybrid')