Skip to content

Commit

Permalink
Merge pull request #37 from wardi/master
Browse files Browse the repository at this point in the history
"display_snippet": null to hide fields, pass scheming validators whole schema
  • Loading branch information
wardi committed Feb 26, 2015
2 parents c2f7c4b + a6899af commit e0af510
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
22 changes: 12 additions & 10 deletions ckanext/scheming/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def validate(self, context, data_dict, schema, action):
if action_type == 'show' else _field_validators)

for f in scheming_fields:
schema[f['field_name']] = get_validators(f,
schema[f['field_name']] = get_validators(f, scheming_schema,
f['field_name'] not in schema)

return navl_validate(data_dict, schema, context)
Expand Down Expand Up @@ -202,12 +202,13 @@ def validate(self, context, data_dict, schema, action):
get_validators = _field_validators

for f in scheming_schema['dataset_fields']:
schema[f['field_name']] = get_validators(f,
schema[f['field_name']] = get_validators(f, scheming_schema,
f['field_name'] not in schema)

resource_schema = schema['resources']
for f in scheming_schema['resource_fields']:
resource_schema[f['field_name']] = get_validators(f, False)
resource_schema[f['field_name']] = get_validators(
f, scheming_schema, False)

return navl_validate(data_dict, schema, context)

Expand Down Expand Up @@ -324,7 +325,7 @@ def _load_schema_url(url):
return json.loads(tables)


def _field_output_validators(f, convert_extras):
def _field_output_validators(f, schema, convert_extras):
"""
Return the output validators for a scheming field f
"""
Expand All @@ -333,16 +334,17 @@ def _field_output_validators(f, convert_extras):
else:
validators = [ignore_missing]
if 'output_validators' in f:
validators += validators_from_string(f['output_validators'], f)
validators += validators_from_string(
f['output_validators'], f, schema)
return validators

def _field_validators(f, convert_extras):
def _field_validators(f, schema, convert_extras):
"""
Return the validators for a scheming field f
"""
validators = []
if 'validators' in f:
validators = validators_from_string(f['validators'], f)
validators = validators_from_string(f['validators'], f, schema)
elif helpers.scheming_field_required(f):
validators = [not_empty, unicode]
else:
Expand All @@ -352,14 +354,14 @@ def _field_validators(f, convert_extras):
validators = validators + [convert_to_extras]
return validators

def _field_create_validators(f, convert_extras):
def _field_create_validators(f, schema, convert_extras):
"""
Return the validators to use when creating for scheming field f,
normally the same as the validators used for updating
"""
if 'create_validators' not in f:
return _field_validators(f, convert_extras)
validators = validators_from_string(f['create_validators'], f)
return _field_validators(f, schema, convert_extras)
validators = validators_from_string(f['create_validators'], f, schema)

if convert_extras:
validators = validators + [convert_to_extras]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "package/snippets/additional_info.html" %}

{%- set exclude_fields = [
'id',
'title',
'name',
'notes',
Expand All @@ -11,7 +12,8 @@

{% block package_additional_info %}
{%- for field in schema.dataset_fields -%}
{%- if field.field_name not in exclude_fields -%}
{%- if field.field_name not in exclude_fields
and field.display_snippet is not none -%}
<tr>
<th scope="row" class="dataset-label">{{
h.scheming_language_text(field.label) }}</th>
Expand Down
15 changes: 9 additions & 6 deletions ckanext/scheming/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ def test_choice_field_accepts_valid_choice(self):
category='f2hybrid',
)
assert_equals(d['category'], 'f2hybrid')

class TestRequired(object):
def test_required_is_set_to_true(self):
assert_equals(not_empty,scheming_required({'required': True}))

assert_equals(not_empty, scheming_required(
{'required': True}, {}))

def test_required_is_set_to_false(self):
assert_equals(ignore_missing,scheming_required({'required': False}))

assert_equals(ignore_missing, scheming_required(
{'required': False}, {}))

def test_required_is_not_present(self):
assert_equals(ignore_missing,scheming_required({'other_field': True}))
assert_equals(ignore_missing, scheming_required(
{'other_field': True}, {}))


class TestDates(object):
Expand Down
12 changes: 6 additions & 6 deletions ckanext/scheming/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ def scheming_validator(fn):
Decorate a validator that needs to have the scheming fields
passed with this function. When generating navl validator lists
the function decorated will be called passing the field
data to produce the actual validator for each field.
and complete schema to produce the actual validator for each field.
"""
fn.is_a_scheming_validator = True
return fn


@scheming_validator
def scheming_choices(field):
def scheming_choices(field, schema):
"""
Require that one of the field choices values is passed.
"""
return OneOf([c['value'] for c in field['choices']])


@scheming_validator
def scheming_required(field):
def scheming_required(field, schema):
"""
not_empty if field['required'] else ignore_missing
"""
Expand All @@ -38,7 +38,7 @@ def scheming_required(field):


@scheming_validator
def scheming_multiple_choice(field):
def scheming_multiple_choice(field, schema):
"""
Accept zero or more values from a list of choices and convert
to a json list for storage:
Expand Down Expand Up @@ -95,7 +95,7 @@ def scheming_multiple_choice_output(value):
return [value]


def validators_from_string(s, field):
def validators_from_string(s, field, schema):
"""
convert a schema validators string to a list of validators
Expand All @@ -112,7 +112,7 @@ def validators_from_string(s, field):
else:
v = get_validator_or_converter(p)
if getattr(v, 'is_a_scheming_validator', False):
v = v(field)
v = v(field, schema)
out.append(v)
return out

Expand Down

0 comments on commit e0af510

Please sign in to comment.