Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Nov 18, 2019
2 parents 7452851 + f2cea08 commit 02a1242
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 18 deletions.
4 changes: 1 addition & 3 deletions ckan/logic/__init__.py
Expand Up @@ -3,10 +3,8 @@
import functools
import logging
import re
import sys
from collections import defaultdict

import formencode.validators
from six import string_types, text_type

import ckan.model as model
Expand Down Expand Up @@ -674,9 +672,9 @@ def get_validator(validator):
_validators_cache.update(validators)
validators = _import_module_functions('ckan.logic.validators')
_validators_cache.update(validators)
_validators_cache.update({'OneOf': formencode.validators.OneOf})
converters = _import_module_functions('ckan.logic.converters')
_validators_cache.update(converters)
_validators_cache.update({'OneOf': _validators_cache['one_of']})

for plugin in reversed(list(p.PluginImplementations(p.IValidators))):
for name, fn in plugin.get_validators().items():
Expand Down
4 changes: 2 additions & 2 deletions ckan/logic/schema.py
Expand Up @@ -343,13 +343,13 @@ def default_extras_schema(

@validator_args
def default_relationship_schema(
ignore_missing, unicode_safe, not_empty, OneOf, ignore):
ignore_missing, unicode_safe, not_empty, one_of, ignore):
return {
'id': [ignore_missing, unicode_safe],
'subject': [ignore_missing, unicode_safe],
'object': [ignore_missing, unicode_safe],
'type': [not_empty,
OneOf(ckan.model.PackageRelationship.get_all_types())],
one_of(ckan.model.PackageRelationship.get_all_types())],
'comment': [ignore_missing, unicode_safe],
'state': [ignore],
}
Expand Down
11 changes: 11 additions & 0 deletions ckan/logic/validators.py
@@ -1,3 +1,4 @@

# encoding: utf-8

import collections
Expand Down Expand Up @@ -28,6 +29,7 @@
Missing = df.Missing
missing = df.missing


def owner_org_validator(key, data, errors, context):

value = data.get(key)
Expand Down Expand Up @@ -858,3 +860,12 @@ def email_validator(value, context):
if not email_pattern.match(value):
raise Invalid(_('Email {email} is not a valid format').format(email=value))
return value


def one_of(list_of_value):
''' Checks if the provided value is present in a list '''
def callable(value):
if value not in list_of_value:
raise Invalid(_('Value must be one of {}'.format(list_of_value)))
return value
return callable
2 changes: 1 addition & 1 deletion ckan/templates/package/snippets/additional_info.html
Expand Up @@ -79,7 +79,7 @@ <h3>{{ _('Additional Info') }}</h3>
{% for extra in h.sorted_extras(pkg_dict.extras) %}
{% set key, value = extra %}
<tr rel="dc:relation" resource="_:extra{{ i }}">
<th scope="row" class="dataset-label" property="rdfs:label">{{ _(key) }}</th>
<th scope="row" class="dataset-label" property="rdfs:label">{{ _(key|e) }}</th>
<td class="dataset-details" property="rdf:value">{{ value }}</td>
</tr>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/snippets/additional_info.html
Expand Up @@ -16,7 +16,7 @@ <h3>{{ _('Additional Info') }}</h3>
{% for extra in extras %}
{% set key, value = extra %}
<tr rel="dc:relation" resource="_:extra{{ i }}">
<th scope="row" class="dataset-label" property="rdfs:label">{{ _(key) }}</th>
<th scope="row" class="dataset-label" property="rdfs:label">{{ _(key|e) }}</th>
<td class="dataset-details" property="rdf:value">{{ value }}</td>
</tr>
{% endfor %}
Expand Down
14 changes: 14 additions & 0 deletions ckan/tests/logic/test_validators.py
@@ -1,3 +1,4 @@

# encoding: utf-8
"""Unit tests for ckan/logic/validators.py.
Expand Down Expand Up @@ -746,4 +747,17 @@ def call_validator(*args, **kwargs):
call_validator(key, {key: url}, errors, None)



class TestOneOfValidator(object):

def test_val_in_list(self):
cont = [1, 2, 3, 4]
func = validators.one_of(cont)
assert_equals(func(1), 1)

def test_val_not_in_list(self):
cont = [1, 2, 3, 4]
func = validators.one_of(cont)
raises_Invalid(func)(5)

# TODO: Need to test when you are not providing owner_org and the validator queries for the dataset with package_show
4 changes: 0 additions & 4 deletions ckanext/datapusher/logic/schema.py
Expand Up @@ -9,13 +9,9 @@

not_missing = get_validator('not_missing')
not_empty = get_validator('not_empty')
resource_id_exists = get_validator('resource_id_exists')
package_id_exists = get_validator('package_id_exists')
ignore_missing = get_validator('ignore_missing')
empty = get_validator('empty')
boolean_validator = get_validator('boolean_validator')
int_validator = get_validator('int_validator')
OneOf = get_validator('OneOf')


def datapusher_submit_schema():
Expand Down
4 changes: 2 additions & 2 deletions ckanext/datastore/blueprint.py
Expand Up @@ -29,7 +29,7 @@
int_validator = get_validator(u'int_validator')
boolean_validator = get_validator(u'boolean_validator')
ignore_missing = get_validator(u'ignore_missing')
OneOf = get_validator(u'OneOf')
one_of = get_validator(u'one_of')
default = get_validator(u'default')
unicode_only = get_validator(u'unicode_only')

Expand All @@ -43,7 +43,7 @@ def dump_schema():
return {
u'offset': [default(0), int_validator],
u'limit': [ignore_missing, int_validator],
u'format': [default(u'csv'), OneOf(DUMP_FORMATS)],
u'format': [default(u'csv'), one_of(DUMP_FORMATS)],
u'bom': [default(False), boolean_validator],
u'filters': [ignore_missing, json_validator],
u'q': [ignore_missing, unicode_or_json_validator],
Expand Down
10 changes: 5 additions & 5 deletions ckanext/datastore/logic/schema.py
Expand Up @@ -18,7 +18,7 @@
empty = get_validator('empty')
boolean_validator = get_validator('boolean_validator')
int_validator = get_validator('int_validator')
OneOf = get_validator('OneOf')
one_of = get_validator('one_of')
unicode_only = get_validator('unicode_only')
default = get_validator('default')
natural_number_validator = get_validator('natural_number_validator')
Expand Down Expand Up @@ -118,11 +118,11 @@ def datastore_create_schema():
'when': [
default(u'before insert or update'),
unicode_only,
OneOf([u'before insert or update'])],
one_of([u'before insert or update'])],
'for_each': [
default(u'row'),
unicode_only,
OneOf([u'row'])],
one_of([u'row'])],
'function': [not_empty, unicode_only],
},
'calculate_record_count': [ignore_missing, default(False),
Expand All @@ -138,7 +138,7 @@ def datastore_upsert_schema():
'resource_id': [not_missing, not_empty, text_type],
'force': [ignore_missing, boolean_validator],
'id': [ignore_missing],
'method': [ignore_missing, text_type, OneOf(
'method': [ignore_missing, text_type, one_of(
['upsert', 'insert', 'update'])],
'calculate_record_count': [ignore_missing, default(False),
boolean_validator],
Expand Down Expand Up @@ -183,7 +183,7 @@ def datastore_search_schema():
'total_estimation_threshold': [default(None), int_validator],
'records_format': [
default(u'objects'),
OneOf([u'objects', u'lists', u'csv', u'tsv'])],
one_of([u'objects', u'lists', u'csv', u'tsv'])],
'__junk': [empty],
'__before': [rename('id', 'resource_id')]
}
Expand Down

0 comments on commit 02a1242

Please sign in to comment.