Skip to content

Commit

Permalink
make choices not special
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Nov 27, 2014
1 parent 67f99f4 commit 01f0cba
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) -
Expand All @@ -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`
Expand Down Expand Up @@ -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`

Expand Down
7 changes: 7 additions & 0 deletions ckanext/__init__.py
Original file line number Diff line number Diff line change
@@ -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__)
17 changes: 6 additions & 11 deletions ckanext/scheming/camel_photos.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand All @@ -25,8 +22,9 @@
"form_placeholder": "eg. 2"
},
{
"field_name": "class",
"label": "Class",
"field_name": "category",
"label": "Category",
"preset": "select",
"choices": [
{
"value": "bactrian",
Expand Down Expand Up @@ -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"
},
{
Expand Down
7 changes: 7 additions & 0 deletions ckanext/scheming/presets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
6 changes: 1 addition & 5 deletions ckanext/scheming/templates/scheming/snippets/form_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
3 changes: 2 additions & 1 deletion ckanext/scheming/tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions ckanext/scheming/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 01f0cba

Please sign in to comment.