diff --git a/ckan/logic/__init__.py b/ckan/logic/__init__.py index b548f28c8cc..c559a76e089 100644 --- a/ckan/logic/__init__.py +++ b/ckan/logic/__init__.py @@ -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) diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index df259500790..6a4ea5e9146 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -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' @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/ckan/logic/action/delete.py b/ckan/logic/action/delete.py index 8c4a7dd8cde..f34c0976406 100644 --- a/ckan/logic/action/delete.py +++ b/ckan/logic/action/delete.py @@ -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): @@ -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) diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index 0baf1e06424..895435dcb45 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -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) @@ -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) @@ -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) @@ -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']) data_dict, errors = _validate(data_dict, schema, context) @@ -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) @@ -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) @@ -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) diff --git a/ckan/logic/action/update.py b/ckan/logic/action/update.py index b379fd7857c..dd75d85a9fa 100644 --- a/ckan/logic/action/update.py +++ b/ckan/logic/action/update.py @@ -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 @@ -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 @@ -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']) @@ -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) @@ -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) @@ -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() diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py index d9a2c55ca84..e00ce2965bf 100644 --- a/ckan/logic/schema.py +++ b/ckan/logic/schema.py @@ -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, @@ -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 @@ -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 @@ -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 } @@ -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``. ''' diff --git a/ckan/logic/validators.py b/ckan/logic/validators.py index 02976dc2872..dc8863774f5 100644 --- a/ckan/logic/validators.py +++ b/ckan/logic/validators.py @@ -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):