Skip to content

Commit

Permalink
Fixed prettyfication of ValidationErrors and typo
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Aug 20, 2012
1 parent 6e376d5 commit fceeed0
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions ckanext/datastore/db.py
Expand Up @@ -35,7 +35,7 @@ def _validate_int(i, field_name):
int(i)
except ValueError:
raise p.toolkit.ValidationError({
'field_name': '{} is not an integer'.format(i)
'field_name': ['{} is not an integer'.format(i)]
})


Expand Down Expand Up @@ -69,7 +69,6 @@ def _cache_types(context):
True)



def _get_type(context, oid):
_cache_types(context)
return _pg_types[oid]
Expand Down Expand Up @@ -139,11 +138,11 @@ def check_fields(context, fields):
for field in fields:
if field.get('type') and not field['type'] in _type_names:
raise p.toolkit.ValidationError({
'fields': '{0} is not a valid field type'.format(field['type'])
'fields': ['{0} is not a valid field type'.format(field['type'])]
})
elif not _is_valid_field_name(field['id']):
raise p.toolkit.ValidationError({
'fields': '{0} is not a valid field name'.format(field['id'])
'fields': ['{0} is not a valid field name'.format(field['id'])]
})

def convert(data, type):
Expand Down Expand Up @@ -177,15 +176,15 @@ def create_table(context, data_dict):
if 'type' not in field:
if not records or field['id'] not in records[0]:
raise p.toolkit.ValidationError({
'fields': '{} type not guessable'.format(field['id'])
'fields': ['{} type not guessable'.format(field['id'])]
})
field['type'] = _guess_type(records[0][field['id']])

if records:
# check record for sanity
if not isinstance(records[0], dict):
raise p.toolkit.ValidationError({
'records': 'The first row is not a json object'
'records': ['The first row is not a json object']
})
supplied_field_ids = records[0].keys()
for field_id in supplied_field_ids:
Expand Down Expand Up @@ -224,16 +223,16 @@ def alter_table(context, data_dict):
if num < len(current_fields):
if field['id'] != current_fields[num]['id']:
raise p.toolkit.ValidationError({
'fields': ('Supplied field "{}" not '
'present or in wrong order').format(field['id'])
'fields': [('Supplied field "{}" not '
'present or in wrong order').format(field['id'])]
})
## no need to check type as field already defined.
continue

if 'type' not in field:
if not records or field['id'] not in records[0]:
raise p.toolkit.ValidationError({
'fields': '{} type not guessable'.format(field['id'])
'fields': ['{} type not guessable'.format(field['id'])]
})
field['type'] = _guess_type(records[0][field['id']])
new_fields.append(field)
Expand All @@ -242,7 +241,7 @@ def alter_table(context, data_dict):
# check record for sanity
if not isinstance(records[0], dict):
raise p.toolkit.ValidationError({
'records': 'The first row is not a json object'
'records': ['The first row is not a json object']
})
supplied_field_ids = records[0].keys()
for field_id in supplied_field_ids:
Expand Down Expand Up @@ -277,16 +276,16 @@ def insert_data(context, data_dict):
# check record for sanity
if not isinstance(record, dict):
raise p.toolkit.ValidationError({
'records': 'row {} is not a json object'.format(num)
'records': ['row {} is not a json object'.format(num)]
})
## check for extra fields in data
extra_keys = set(record.keys()) - set(field_names)
if extra_keys:
raise p.toolkit.ValidationError({
'records': 'row {} has extra keys "{}"'.format(
'records': ['row {} has extra keys "{}"'.format(
num + 1,
', '.join(list(extra_keys))
)
)]
})

full_text = []
Expand Down Expand Up @@ -319,7 +318,7 @@ def _where(field_ids, data_dict):

if not isinstance(filters, dict):
raise p.toolkit.ValidationError({
'filters': 'Not a json object'}
'filters': ['Not a json object']}
)

where_clauses = []
Expand All @@ -328,7 +327,7 @@ def _where(field_ids, data_dict):
for field, value in filters.iteritems():
if field not in field_ids:
raise p.toolkit.ValidationError({
'filters': 'field "{}" not in table'}
'filters': ['field "{}" not in table']}
)
where_clauses.append('"{}" = %s'.format(field))
values.append(value)
Expand All @@ -354,7 +353,7 @@ def _sort(context, sort, field_ids):
clauses = sort
else:
raise p.toolkit.ValidationError({
'sort': 'sort is not a list or a string'
'sort': ['sort is not a list or a string']
})

clause_parsed = []
Expand All @@ -367,15 +366,15 @@ def _sort(context, sort, field_ids):
field, sort = clause_parts
else:
raise p.toolkit.ValidationError({
'sort': 'not valid syntax for sort clause'
'sort': ['not valid syntax for sort clause']
})
if field not in field_ids:
raise p.toolkit.ValidationError({
'sort': 'field {} not it table'.format(field)
'sort': ['field {} not in table'.format(field)]
})
if sort.lower() not in ('asc', 'desc'):
raise p.toolkit.ValidationError({
'sort': 'sorting can only be asc or desc'
'sort': ['sorting can only be asc or desc']
})
clause_parsed.append('"{}" {}'.format(field, sort))

Expand Down Expand Up @@ -410,7 +409,7 @@ def search_data(context, data_dict):
for field in field_ids:
if not field in all_field_ids:
raise p.toolkit.ValidationError({
'fields': 'field "{}" not in table'.format(field)}
'fields': ['field "{}" not in table'.format(field)]}
)
else:
field_ids = all_field_ids
Expand Down Expand Up @@ -501,7 +500,7 @@ def create(context, data_dict):
except Exception, e:
if 'due to statement timeout' in str(e):
raise p.toolkit.ValidationError({
'query': 'Query took too long'
'query': ['Query took too long']
})
raise
finally:
Expand All @@ -522,8 +521,8 @@ def delete(context, data_dict):
).fetchone()
if not result:
raise p.toolkit.ValidationError({
'resource_id': 'table for resource {0} does not exist'.format(
data_dict['resource_id'])
'resource_id': ['table for resource {0} does not exist'.format(
data_dict['resource_id'])]
})
if not 'filters' in data_dict:
context['connection'].execute(
Expand Down Expand Up @@ -557,14 +556,14 @@ def search(context, data_dict):
).fetchone()
if not result:
raise p.toolkit.ValidationError({
'resource_id': 'table for resource {0} does not exist'.format(
data_dict['resource_id'])
'resource_id': ['table for resource {0} does not exist'.format(
data_dict['resource_id'])]
})
return search_data(context, data_dict)
except Exception, e:
if 'due to statement timeout' in str(e):
raise p.toolkit.ValidationError({
'query': 'Search took too long'
'query': ['Search took too long']
})
raise
finally:
Expand Down

0 comments on commit fceeed0

Please sign in to comment.