Skip to content

Commit

Permalink
[#1258] refactor action function validators
Browse files Browse the repository at this point in the history
  • Loading branch information
joetsoi committed Nov 5, 2013
1 parent 2307e83 commit 39a526b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 62 deletions.
8 changes: 2 additions & 6 deletions ckan/logic/__init__.py
Expand Up @@ -502,7 +502,7 @@ def get_or_bust(data_dict, keys):
return values[0]
return tuple(values)

def validate(schema_func, check_schema_in_context=False, can_skip_validator=False):
def validate(schema_func, can_skip_validator=False):
''' A decorator that validates an action function against a given schema
'''
def action_decorator(action):
Expand All @@ -512,11 +512,7 @@ def wrapper(context, data_dict, **kwargs):
if context.get('skip_validation'):
return action(context, data_dict)

if not check_schema_in_context:
schema = schema_func()
else:
schema = context.get('schema') or schema()

schema = context.get('schema', schema_func())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)
Expand Down
67 changes: 13 additions & 54 deletions ckan/logic/action/get.py
Expand Up @@ -63,6 +63,7 @@ def site_read(context,data_dict=None):
_check_access('site_read',context,data_dict)
return True

@logic.validate(logic.schema.default_pagination_schema)
def package_list(context, data_dict):
'''Return a list of the names of the site's datasets (packages).
Expand All @@ -81,12 +82,6 @@ def package_list(context, data_dict):

_check_access('package_list', context, data_dict)

schema = context.get('schema', logic.schema.default_pagination_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)


package_revision_table = model.package_revision_table
col = (package_revision_table.c.id
if api == 2 else package_revision_table.c.name)
Expand All @@ -107,6 +102,7 @@ def package_list(context, data_dict):
query = query.offset(offset)
return list(zip(*query.execute())[0])

@logic.validate(logic.schema.default_package_list_schema)
def current_package_list_with_resources(context, data_dict):
'''Return a list of the site's datasets (packages) and their resources.
Expand All @@ -125,20 +121,13 @@ def current_package_list_with_resources(context, data_dict):
'''
model = context["model"]
schema = context.get('schema', logic.schema.default_package_list_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

limit = data_dict.get('limit')
offset = int(data_dict.get('offset', 0))
offset = data_dict.get('offset', 0)

if not 'offset' in data_dict and 'page' in data_dict:
log.warning('"page" parameter is deprecated. '
'Use the "offset" parameter instead')
page = int(data_dict['page'])
if page < 1:
raise ValidationError(_('Must be larger than 0'))
page = data_dict['page']
if limit:
offset = (page - 1) * limit
else:
Expand Down Expand Up @@ -1156,6 +1145,7 @@ def tag_show_rest(context, data_dict):

return tag_dict

@logic.validate(logic.schema.default_autocomplete_schema)
def package_autocomplete(context, data_dict):
'''Return a list of datasets (packages) that match a string.
Expand All @@ -1171,11 +1161,6 @@ def package_autocomplete(context, data_dict):
:rtype: list of dictionaries
'''
schema = context.get('schema', logic.schema.default_autocomplete_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

model = context['model']

_check_access('package_autocomplete', context, data_dict)
Expand Down Expand Up @@ -1207,6 +1192,7 @@ def package_autocomplete(context, data_dict):

return pkg_list

@logic.validate(logic.schema.default_autocomplete_schema)
def format_autocomplete(context, data_dict):
'''Return a list of resource formats whose names contain a string.
Expand All @@ -1219,11 +1205,6 @@ def format_autocomplete(context, data_dict):
:rtype: list of strings
'''
schema = context.get('schema', logic.schema.default_autocomplete_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

model = context['model']
session = context['session']

Expand All @@ -1247,6 +1228,7 @@ def format_autocomplete(context, data_dict):

return [resource.format.lower() for resource in query]

@logic.validate(logic.schema.default_autocomplete_schema)
def user_autocomplete(context, data_dict):
'''Return a list of user names that contain a string.
Expand All @@ -1260,11 +1242,6 @@ def user_autocomplete(context, data_dict):
``'fullname'``, and ``'id'``
'''
schema = context.get('schema', logic.schema.default_autocomplete_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

model = context['model']
user = context['user']

Expand Down Expand Up @@ -1532,6 +1509,7 @@ def package_search(context, data_dict):

return search_results

@logic.validate(logic.schema.default_resource_search_schema)
def resource_search(context, data_dict):
'''
Searches for resources satisfying a given search criteria.
Expand Down Expand Up @@ -1603,11 +1581,6 @@ def resource_search(context, data_dict):
:rtype: dict
'''
schema = context.get('schema', logic.schema.default_resource_search_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

model = context['model']

# Allow either the `query` or `fields` parameter to be given, but not both.
Expand Down Expand Up @@ -2619,6 +2592,7 @@ def group_followee_count(context, data_dict):
context['model'].UserFollowingGroup)


@logic.validate(logic.schema.default_follow_user_schema)
def followee_list(context, data_dict):
'''Return the list of objects that are followed by the given user.
Expand All @@ -2641,11 +2615,6 @@ def followee_list(context, data_dict):
'''
_check_access('followee_list', context, data_dict)
schema = context.get('schema') or (
ckan.logic.schema.default_follow_user_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

def display_name(followee):
'''Return a display name for the given user, group or dataset dict.'''
Expand Down Expand Up @@ -2778,6 +2747,7 @@ def group_followee_list(context, data_dict):
return [model_dictize.group_dictize(group, context) for group in groups]


@logic.validate(logic.schema.default_pagination_schema)
def dashboard_activity_list(context, data_dict):
'''Return the authorized user's dashboard activity stream.
Expand All @@ -2798,17 +2768,11 @@ def dashboard_activity_list(context, data_dict):
:rtype: list of activity dictionaries
'''
schema = context.get('schema',
ckan.logic.schema.default_pagination_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

_check_access('dashboard_activity_list', context, data_dict)

model = context['model']
user_id = model.User.get(context['user']).id
offset = int(data_dict.get('offset', 0))
offset = data_dict.get('offset', 0)
limit = int(
data_dict.get('limit', config.get('ckan.activity_list_limit', 31)))

Expand All @@ -2835,6 +2799,7 @@ def dashboard_activity_list(context, data_dict):
return activity_dicts


@logic.validate(ckan.logic.schema.default_pagination_schema)
def dashboard_activity_list_html(context, data_dict):
'''Return the authorized user's dashboard activity stream as HTML.
Expand All @@ -2854,15 +2819,9 @@ def dashboard_activity_list_html(context, data_dict):
:rtype: string
'''
schema = context.get(
'schema', ckan.logic.schema.default_pagination_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

activity_stream = dashboard_activity_list(context, data_dict)
model = context['model']
offset = int(data_dict.get('offset', 0))
offset = data_dict.get('offset', 0)
extra_vars = {
'controller': 'user',
'action': 'dashboard',
Expand Down
3 changes: 2 additions & 1 deletion ckan/logic/schema.py
Expand Up @@ -32,6 +32,7 @@
isodate,
int_validator,
natural_number_validator,
is_positive_integer,
boolean_validator,
user_about_validator,
vocabulary_name_validator,
Expand Down Expand Up @@ -521,7 +522,7 @@ def default_package_list_schema():
schema = {
'limit': [ignore_missing, natural_number_validator],
'offset': [ignore_missing, natural_number_validator],
'page': [ignore_missing, natural_number_validator]
'page': [ignore_missing, is_positive_integer]
}
return schema

Expand Down
8 changes: 7 additions & 1 deletion ckan/logic/validators.py
Expand Up @@ -68,7 +68,13 @@ def int_validator(value, context):
def natural_number_validator(value, context):
value = int_validator(value, context)
if value < 0:
raise Invalid(_('Must be natural number'))
raise Invalid(_('Must be a natural number'))
return value

def is_positive_integer(value, context):
value = int_validator(value, context)
if value < 1:
raise Invalid(_('Must be a postive integer'))
return value

def boolean_validator(value, context):
Expand Down

0 comments on commit 39a526b

Please sign in to comment.