Skip to content

Commit

Permalink
Merge b4db3c4 into f7ba8fb
Browse files Browse the repository at this point in the history
  • Loading branch information
florianm committed Dec 11, 2014
2 parents f7ba8fb + b4db3c4 commit 555640e
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pyc
ckanext_scheming.egg-info/*
links/*
build/*
dist/*
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ This extension includes the following form snippets:
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
* [date.html](ckanext/scheming/templates/scheming/form_snippets/date.html) -
a date widget with a drop-down date picker - don't forget to use the `isodate` validator
* [slug.html](ckanext/scheming/templates/scheming/form_snippets/slug.html) -
the default name (URL) field
* [license.html](ckanext/scheming/templates/scheming/form_snippets/license.html) -
Expand Down Expand Up @@ -286,11 +288,30 @@ New validators and converters may be added using the
[IValidators plugin interface](http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html?highlight=ivalidator#ckan.plugins.interfaces.IValidators).

Validators that need access to other values in this schema (e.g.
to test values against the choices list) May be decorated with
to test values against the choices list) may be decorated with
the [scheming.validation.scheming_validator](ckanext/scheming/validation.py)
function. This decorator will make scheming pass this field dict to the
validator and use its return value for validation of the field.

CKAN's [validator functions reference](http://docs.ckan.org/en/latest/extensions/validators.html)
lists available validators ready to be used. E.g., date fields using the form snippet
[date.html](ckanext/scheming/templates/scheming/form_snippets/date.html)
should use the validator [isodate](http://docs.ckan.org/en/latest/extensions/validators.html#ckan.logic.validators.isodate).
In this specific case, the `isodate` validator will reject non-ISO8601 values
from older browsers, which might not be able to render the date picker widget
correctly, and default to a text field.


```json
{
"field_name": "a_relevant_date",
"label": "A relevant date",
"help_text": "An example of a date field",
"form_snippet": "date.html",
"validators": "ignore_missing unicode isodate"
},
...
```

### `output_validators`

Expand All @@ -302,4 +323,7 @@ sent to the user.
This extension automatically adds calls to `convert_from_extras`
for extra fields so you should not add that to this list.


### `help_text`
Only if this key is supplied, its value will be shown as inline help text,
Help text must be plain text, no markdown or HTML are allowed.
8 changes: 8 additions & 0 deletions ckanext/scheming/camel_photos.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
{
"field_name": "category",
"label": "Category",
"help_text": "Make and model of camel",
"preset": "select",
"choices": [
{
Expand All @@ -48,6 +49,13 @@
}
]
},
{
"field_name": "a_relevant_date",
"label": "A relevant date",
"help_text": "An example of a date field",
"form_snippet": "date.html",
"validators": "ignore_missing unicode isodate"
},
{
"field_name": "other",
"label": {"en": "Other information"},
Expand Down
18 changes: 18 additions & 0 deletions ckanext/scheming/templates/scheming/form_snippets/date.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% import 'macros/form.html' as form %}
{% call form.input(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
placeholder=h.scheming_language_text(field.form_placeholder),
type='date',
value=data[field.field_name],
error=errors[field.field_name],
classes=['control-medium'],
attrs=field.form_attrs if 'form_attrs' in field else {},
is_required=h.scheming_field_required(field)
)
%}
{% if field.help_text %}
{{ form.info(text=field.help_text, inline=true) }}
{% endif %}
{% endcall %}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import 'macros/form.html' as form %}

{{ form.input(
{% call form.input(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
Expand All @@ -10,4 +10,9 @@
classes=['control-full', 'control-large'],
attrs=field.form_attrs if 'form_attrs' in field else {},
is_required=h.scheming_field_required(field)
) }}
)
%}
{% if field.help_text %}
{{ form.info(text=field.help_text, inline=true) }}
{% endif %}
{% endcall %}
10 changes: 7 additions & 3 deletions ckanext/scheming/templates/scheming/form_snippets/markdown.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import 'macros/form.html' as form %}

{{ form.markdown(
{% call form.markdown(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
Expand All @@ -9,5 +9,9 @@
error=errors[field.field_name],
attrs=field.form_attrs or {},
is_required=h.scheming_field_required(field)
) }}

)
%}
{% if field.help_text %}
{{ form.info(text=field.help_text, inline=true) }}
{% endif %}
{% endcall %}
9 changes: 7 additions & 2 deletions ckanext/scheming/templates/scheming/form_snippets/select.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'text': h.scheming_language_text(c.label)}) -%}
{%- endfor -%}

{{ form.select(
{% call form.select(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
Expand All @@ -20,4 +20,9 @@
classes=['control-medium'],
attrs=field.form_attrs if 'form_attrs' in field else {},
is_required=h.scheming_field_required(field)
) }}
)
%}
{% if field.help_text %}
{{ form.info(text=field.help_text, inline=true) }}
{% endif %}
{% endcall %}
9 changes: 7 additions & 2 deletions ckanext/scheming/templates/scheming/form_snippets/text.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import 'macros/form.html' as form %}

{{ form.input(
{% call form.input(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
Expand All @@ -10,4 +10,9 @@
classes=['control-medium'],
attrs=field.form_attrs if 'form_attrs' in field else {},
is_required=h.scheming_field_required(field)
) }}
)
%}
{% if field.help_text %}
{{ form.info(text=field.help_text, inline=true) }}
{% endif %}
{% endcall %}
20 changes: 20 additions & 0 deletions ckanext/scheming/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ def test_choice_field_accepts_valid_choice(self):
assert_equals(d['category'], 'f2hybrid')


class TestDates(object):
def test_date_field_rejects_non_isodates(self):
lc = LocalCKAN()
assert_raises(ValidationError, lc.action.package_create,
type='camel-photos',
name='fred',
a_relevant_date='31/11/2014',
)
assert_raises(ValidationError, lc.action.package_create,
type='camel-photos',
name='fred',
a_relevant_date='31/11/abcd',
)
assert_raises(ValidationError, lc.action.package_create,
type='camel-photos',
name='fred',
a_relevant_date='this-is-not-a-date',
)


class TestInvalidType(object):
def test_invalid_dataset_type(self):
p = SchemingDatasetsPlugin.instance
Expand Down

0 comments on commit 555640e

Please sign in to comment.