Skip to content

Commit

Permalink
[#473] Renamed pos_int_validator to natural_number_validator, improve…
Browse files Browse the repository at this point in the history
…d code style, fixed potential server error
  • Loading branch information
domoritz committed May 30, 2013
1 parent 89fa558 commit 8cf11e4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
4 changes: 2 additions & 2 deletions ckan/logic/__init__.py
Expand Up @@ -351,8 +351,8 @@ def get_or_bust(data_dict, keys):
if isinstance(keys, basestring):
keys = [keys]

import ckan.logic.schema as schema_module
schema = schema_module._create_schema_for_required_keys(keys)
import ckan.logic.schema as schema
schema = schema.create_schema_for_required_keys(keys)

data_dict, errors = _validate(data_dict, schema)

Expand Down
18 changes: 9 additions & 9 deletions ckan/logic/action/create.py
Expand Up @@ -344,7 +344,7 @@ def package_relationship_create(context, data_dict):
'''
model = context['model']
user = context['user']
schema = context.get('schema') or ckan.logic.schema.default_create_relationship_schema()
schema = context.get('schema', ckan.logic.schema.default_create_relationship_schema())
api = context.get('api_version')
ref_package_by = 'id' if api == 2 else 'name'

Expand Down Expand Up @@ -754,7 +754,7 @@ def user_create(context, data_dict):
'''
model = context['model']
schema = context.get('schema') or ckan.logic.schema.default_user_schema()
schema = context.get('schema', ckan.logic.schema.default_user_schema())
session = context['session']

_check_access('user_create', context, data_dict)
Expand Down Expand Up @@ -850,7 +850,7 @@ def vocabulary_create(context, data_dict):
'''
model = context['model']
schema = context.get('schema') or ckan.logic.schema.default_create_vocabulary_schema()
schema = context.get('schema', ckan.logic.schema.default_create_vocabulary_schema())

_check_access('vocabulary_create', context, data_dict)

Expand Down Expand Up @@ -906,7 +906,7 @@ def activity_create(context, activity_dict, ignore_auth=False):
if not ignore_auth:
_check_access('activity_create', context, activity_dict)

schema = context.get('schema') or ckan.logic.schema.default_create_activity_schema()
schema = context.get('schema', ckan.logic.schema.default_create_activity_schema())
data, errors = _validate(activity_dict, schema, context)
if errors:
raise ValidationError(errors)
Expand Down Expand Up @@ -956,7 +956,7 @@ def tag_create(context, tag_dict):

_check_access('tag_create', context, tag_dict)

schema = context.get('schema') or ckan.logic.schema.default_create_tag_schema()
schema = context.get('schema', ckan.logic.schema.default_create_tag_schema())
data, errors = _validate(tag_dict, schema, context)
if errors:
raise ValidationError(errors)
Expand Down Expand Up @@ -992,8 +992,8 @@ def follow_user(context, data_dict):
if not userobj:
raise logic.NotAuthorized(_("You must be logged in to follow users"))

schema = (context.get('schema')
or ckan.logic.schema.default_follow_user_schema())
schema = context.get('schema',
ckan.logic.schema.default_follow_user_schema())

validated_data_dict, errors = _validate(data_dict, schema, context)

Expand Down Expand Up @@ -1052,8 +1052,8 @@ def follow_dataset(context, data_dict):
raise logic.NotAuthorized(
_("You must be logged in to follow a dataset."))

schema = (context.get('schema')
or ckan.logic.schema.default_follow_dataset_schema())
schema = context.get('schema',
ckan.logic.schema.default_follow_dataset_schema())

validated_data_dict, errors = _validate(data_dict, schema, context)

Expand Down
8 changes: 4 additions & 4 deletions ckan/logic/action/delete.py
Expand Up @@ -384,8 +384,8 @@ def unfollow_user(context, data_dict):
:type id: string
'''
schema = context.get('schema') or (
ckan.logic.schema.default_follow_user_schema())
schema = context.get('schema',
ckan.logic.schema.default_follow_user_schema())
_unfollow(context, data_dict, schema, context['model'].UserFollowingUser)

def unfollow_dataset(context, data_dict):
Expand All @@ -395,8 +395,8 @@ def unfollow_dataset(context, data_dict):
:type id: string
'''
schema = context.get('schema') or (
ckan.logic.schema.default_follow_dataset_schema())
schema = context.get('schema',
ckan.logic.schema.default_follow_dataset_schema())
_unfollow(context, data_dict, schema,
context['model'].UserFollowingDataset)

Expand Down
26 changes: 13 additions & 13 deletions ckan/logic/action/get.py
Expand Up @@ -111,7 +111,10 @@ def current_package_list_with_resources(context, data_dict):
page = int(data_dict['page'])
if page < 1:
raise ValidationError(_('Must be larger than 0'))
offset = (page - 1) * limit
if limit:
offset = (page - 1) * limit
else:
offset = 0

_check_access('current_package_list_with_resources', context, data_dict)

Expand All @@ -122,7 +125,7 @@ def current_package_list_with_resources(context, data_dict):
query = query.order_by(model.package_revision_table.c.revision_timestamp.desc())
if limit is not None:
query = query.limit(limit)
query = query.offset(offset)
query = query.offset(offset)
pack_rev = query.all()
return _package_list_with_resources(context, pack_rev)

Expand Down Expand Up @@ -182,7 +185,7 @@ def related_show(context, data_dict=None):

_check_access('related_show',context, data_dict)

schema = context.get('schema') or ckan.logic.schema.default_related_schema()
schema = context.get('schema', ckan.logic.schema.default_related_schema())
related_dict = model_dictize.related_dictize(related, context)
related_dict, errors = _validate(related_dict, schema, context=context)

Expand Down Expand Up @@ -1261,10 +1264,7 @@ def package_search(context, data_dict):
query cannot be changed. CKAN always returns the matched datasets as
dictionary objects.
'''
# sometimes context['schema'] is None
schema = context.get('schema')
if not schema:
schema = logic.schema.default_package_search_schema()
schema = context.get('schema', logic.schema.default_package_search_schema())
if isinstance(data_dict.get('facet.field'), basestring):
data_dict['facet.field'] = ast.literal_eval(data_dict['facet.field'])

This comment has been minimized.

Copy link
@johnglover

johnglover May 30, 2013

Contributor

Github seems to lose comments when you commit which is annoying, but anyway...

Only one test breaks when this is changed to json.loads, and that is because the urlencoded string is not valid JSON. It should pass if the strings are double quoted. I don't think it's unreasonable to require JSON if doing this through a GET request.

data_dict, errors = _validate(data_dict, schema, context)
Expand Down Expand Up @@ -2482,8 +2482,8 @@ 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())
schema = context.get('schema',
ckan.logic.schema.default_follow_user_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)
Expand Down Expand Up @@ -2539,8 +2539,8 @@ def user_followee_list(context, data_dict):
_check_access('user_followee_list', context, data_dict)

if not context.get('skip_validation'):
schema = context.get('schema') or (
ckan.logic.schema.default_follow_user_schema())
schema = context.get('schema',
ckan.logic.schema.default_follow_user_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)
Expand Down Expand Up @@ -2570,8 +2570,8 @@ def dataset_followee_list(context, data_dict):
_check_access('dataset_followee_list', context, data_dict)

if not context.get('skip_validation'):
schema = context.get('schema') or (
ckan.logic.schema.default_follow_user_schema())
schema = context.get('schema',
ckan.logic.schema.default_follow_user_schema())
data_dict, errors = _validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)
Expand Down
14 changes: 8 additions & 6 deletions ckan/logic/action/update.py
Expand Up @@ -130,7 +130,7 @@ def related_update(context, data_dict):
user = context['user']
id = _get_or_bust(data_dict, "id")

schema = context.get('schema') or ckan.logic.schema.default_related_schema()
schema = context.get('schema', ckan.logic.schema.default_related_schema())

related = model.Related.get(id)
context["related"] = related
Expand Down Expand Up @@ -170,7 +170,7 @@ def resource_update(context, data_dict):
model = context['model']
user = context['user']
id = _get_or_bust(data_dict, "id")
schema = context.get('schema') or ckan.logic.schema.default_update_resource_schema()
schema = context.get('schema', ckan.logic.schema.default_update_resource_schema())

resource = model.Resource.get(id)
context["resource"] = resource
Expand Down Expand Up @@ -337,7 +337,8 @@ def package_relationship_update(context, data_dict):
'''
model = context['model']
schema = context.get('schema') or ckan.logic.schema.default_update_relationship_schema()
schema = context.get('schema',
ckan.logic.schema.default_update_relationship_schema())

id, id2, rel = _get_or_bust(data_dict, ['subject', 'object', 'type'])

Expand Down Expand Up @@ -546,7 +547,7 @@ def user_update(context, data_dict):
model = context['model']
user = context['user']
session = context['session']
schema = context.get('schema') or ckan.logic.schema.default_update_user_schema()
schema = context.get('schema', ckan.logic.schema.default_update_user_schema())
id = _get_or_bust(data_dict, 'id')

user_obj = model.User.get(id)
Expand Down Expand Up @@ -614,7 +615,8 @@ def task_status_update(context, data_dict):

user = context['user']
id = data_dict.get("id")
schema = context.get('schema') or ckan.logic.schema.default_task_status_schema()
schema = context.get('schema',
ckan.logic.schema.default_task_status_schema())

if id:
task_status = model.TaskStatus.get(id)
Expand Down Expand Up @@ -824,7 +826,7 @@ def vocabulary_update(context, data_dict):

_check_access('vocabulary_update', context, data_dict)

schema = context.get('schema') or ckan.logic.schema.default_update_vocabulary_schema()
schema = context.get('schema', ckan.logic.schema.default_update_vocabulary_schema())
data, errors = _validate(data_dict, schema, context)
if errors:
model.Session.rollback()
Expand Down
28 changes: 14 additions & 14 deletions ckan/logic/schema.py
Expand Up @@ -30,7 +30,7 @@
user_password_not_empty,
isodate,
int_validator,
pos_int_validator,
natural_number_validator,
boolean_validator,
user_about_validator,
vocabulary_name_validator,
Expand Down Expand Up @@ -490,17 +490,17 @@ def default_follow_group_schema():

def default_package_list_schema():
schema = {
'limit': [ignore_missing, pos_int_validator],
'offset': [ignore_missing, pos_int_validator],
'page': [ignore_missing, pos_int_validator]
'limit': [ignore_missing, natural_number_validator],
'offset': [ignore_missing, natural_number_validator],
'page': [ignore_missing, natural_number_validator]
}
return schema


def default_pagination_schema():
schema = {
'limit': [ignore_missing, pos_int_validator],
'offset': [ignore_missing, pos_int_validator]
'limit': [ignore_missing, natural_number_validator],
'offset': [ignore_missing, natural_number_validator]
}
return schema

Expand All @@ -514,7 +514,7 @@ def default_dashboard_activity_list_schema():
def default_autocomplete_schema():
schema = {
'q': [not_missing, unicode],
'limit': [ignore_missing, pos_int_validator]
'limit': [ignore_missing, natural_number_validator]
}
return schema

Expand All @@ -523,13 +523,13 @@ def default_package_search_schema():
schema = {
'q': [ignore_missing, unicode],
'fq': [ignore_missing, unicode],
'rows': [ignore_missing, pos_int_validator],
'rows': [ignore_missing, natural_number_validator],
'sort': [ignore_missing, unicode],
'start': [ignore_missing, pos_int_validator],
'start': [ignore_missing, natural_number_validator],
'qf': [ignore_missing, unicode],
'facet': [ignore_missing, unicode],
'facet.mincount': [ignore_missing, pos_int_validator],
'facet.limit': [ignore_missing, pos_int_validator],
'facet.mincount': [ignore_missing, natural_number_validator],
'facet.limit': [ignore_missing, natural_number_validator],
'facet.field': [ignore_missing, list_of_strings],
'extras': [ignore_missing] # Not used by Solr, but useful for extensions
}
Expand All @@ -541,13 +541,13 @@ def default_resource_search_schema():
'query': [ignore_missing], # string or list of strings
'fields': [ignore_missing], # dict of fields
'order_by': [ignore_missing, unicode],
'offset': [ignore_missing, pos_int_validator],
'limit': [ignore_missing, pos_int_validator]
'offset': [ignore_missing, natural_number_validator],
'limit': [ignore_missing, natural_number_validator]
}
return schema


def _create_schema_for_required_keys(keys):
def create_schema_for_required_keys(keys):
''' helper function that creates a schema definition where
each key from keys is validated against ``not_missing``.
'''
Expand Down
4 changes: 2 additions & 2 deletions ckan/logic/validators.py
Expand Up @@ -54,10 +54,10 @@ def int_validator(value, context):
except (AttributeError, ValueError), e:
raise Invalid(_('Invalid integer'))

def pos_int_validator(value, context):
def natural_number_validator(value, context):
value = int_validator(value, context)
if value < 0:
raise Invalid(_('Must be positive integer'))
raise Invalid(_('Must be natural number'))
return value

def boolean_validator(value, context):
Expand Down

0 comments on commit 8cf11e4

Please sign in to comment.