Refactored ValidationError to allow persisting error params and error codes as the exception bubbles up

This commit is contained in:
Loic Bistuer 2013-04-05 02:21:57 +07:00 committed by Tim Graham
parent efe6e16008
commit f34cfec0fa
6 changed files with 67 additions and 55 deletions

View File

@ -3,6 +3,9 @@ Global Django exception and warning classes.
""" """
import logging import logging
from functools import reduce from functools import reduce
import operator
from django.utils.encoding import force_text
class DjangoRuntimeWarning(RuntimeWarning): class DjangoRuntimeWarning(RuntimeWarning):
@ -74,46 +77,67 @@ NON_FIELD_ERRORS = '__all__'
class ValidationError(Exception): class ValidationError(Exception):
"""An error while validating data.""" """An error while validating data."""
def __init__(self, message, code=None, params=None): def __init__(self, message, code=None, params=None):
import operator
from django.utils.encoding import force_text
""" """
ValidationError can be passed any object that can be printed (usually ValidationError can be passed any object that can be printed (usually
a string), a list of objects or a dictionary. a string), a list of objects or a dictionary.
""" """
if isinstance(message, dict): if isinstance(message, dict):
self.message_dict = message self.error_dict = message
# Reduce each list of messages into a single list. elif isinstance(message, list):
message = reduce(operator.add, message.values()) self.error_list = message
if isinstance(message, list):
self.messages = [force_text(msg) for msg in message]
else: else:
self.code = code self.code = code
self.params = params self.params = params
message = force_text(message) self.message = message
self.messages = [message] self.error_list = [self]
@property
def message_dict(self):
message_dict = {}
for field, messages in self.error_dict.items():
message_dict[field] = []
for message in messages:
if isinstance(message, ValidationError):
message_dict[field].extend(message.messages)
else:
message_dict[field].append(force_text(message))
return message_dict
@property
def messages(self):
if hasattr(self, 'error_dict'):
message_list = reduce(operator.add, self.error_dict.values())
else:
message_list = self.error_list
messages = []
for message in message_list:
if isinstance(message, ValidationError):
params = message.params
message = message.message
if params:
message %= params
message = force_text(message)
else:
message = force_text(message)
messages.append(message)
return messages
def __str__(self): def __str__(self):
# This is needed because, without a __str__(), printing an exception if hasattr(self, 'error_dict'):
# instance would result in this:
# AttributeError: ValidationError instance has no attribute 'args'
# See http://www.python.org/doc/current/tut/node10.html#handling
if hasattr(self, 'message_dict'):
return repr(self.message_dict) return repr(self.message_dict)
return repr(self.messages) return repr(self.messages)
def __repr__(self): def __repr__(self):
if hasattr(self, 'message_dict'): return 'ValidationError(%s)' % self
return 'ValidationError(%s)' % repr(self.message_dict)
return 'ValidationError(%s)' % repr(self.messages)
def update_error_dict(self, error_dict): def update_error_dict(self, error_dict):
if hasattr(self, 'message_dict'): if hasattr(self, 'error_dict'):
if error_dict: if error_dict:
for k, v in self.message_dict.items(): for k, v in self.error_dict.items():
error_dict.setdefault(k, []).extend(v) error_dict.setdefault(k, []).extend(v)
else: else:
error_dict = self.message_dict error_dict = self.error_dict
else: else:
error_dict[NON_FIELD_ERRORS] = self.messages error_dict[NON_FIELD_ERRORS] = self.error_list
return error_dict return error_dict

View File

@ -910,7 +910,7 @@ class Model(six.with_metaclass(ModelBase)):
'field_label': six.text_type(field_labels) 'field_label': six.text_type(field_labels)
} }
def full_clean(self, exclude=None): def full_clean(self, exclude=None, validate_unique=True):
""" """
Calls clean_fields, clean, and validate_unique, on the model, Calls clean_fields, clean, and validate_unique, on the model,
and raises a ``ValidationError`` for any errors that occurred. and raises a ``ValidationError`` for any errors that occurred.
@ -932,13 +932,14 @@ class Model(six.with_metaclass(ModelBase)):
errors = e.update_error_dict(errors) errors = e.update_error_dict(errors)
# Run unique checks, but only for fields that passed validation. # Run unique checks, but only for fields that passed validation.
for name in errors.keys(): if validate_unique:
if name != NON_FIELD_ERRORS and name not in exclude: for name in errors.keys():
exclude.append(name) if name != NON_FIELD_ERRORS and name not in exclude:
try: exclude.append(name)
self.validate_unique(exclude=exclude) try:
except ValidationError as e: self.validate_unique(exclude=exclude)
errors = e.update_error_dict(errors) except ValidationError as e:
errors = e.update_error_dict(errors)
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)
@ -963,7 +964,7 @@ class Model(six.with_metaclass(ModelBase)):
try: try:
setattr(self, f.attname, f.clean(raw_value, self)) setattr(self, f.attname, f.clean(raw_value, self))
except ValidationError as e: except ValidationError as e:
errors[f.name] = e.messages errors[f.name] = e.error_list
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)

View File

@ -208,12 +208,9 @@ class Field(object):
v(value) v(value)
except exceptions.ValidationError as e: except exceptions.ValidationError as e:
if hasattr(e, 'code') and e.code in self.error_messages: if hasattr(e, 'code') and e.code in self.error_messages:
message = self.error_messages[e.code] e.message = self.error_messages[e.code]
if e.params: errors.extend(e.error_list)
message = message % e.params
errors.append(message)
else:
errors.extend(e.messages)
if errors: if errors:
raise exceptions.ValidationError(errors) raise exceptions.ValidationError(errors)

View File

@ -136,12 +136,8 @@ class Field(object):
v(value) v(value)
except ValidationError as e: except ValidationError as e:
if hasattr(e, 'code') and e.code in self.error_messages: if hasattr(e, 'code') and e.code in self.error_messages:
message = self.error_messages[e.code] e.message = self.error_messages[e.code]
if e.params: errors.extend(e.error_list)
message = message % e.params
errors.append(message)
else:
errors.extend(e.messages)
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)
@ -974,7 +970,7 @@ class MultiValueField(Field):
# Collect all validation errors in a single list, which we'll # Collect all validation errors in a single list, which we'll
# raise at the end of clean(), rather than raising a single # raise at the end of clean(), rather than raising a single
# exception for the first error we encounter. # exception for the first error we encounter.
errors.extend(e.messages) errors.extend(e.error_list)
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)

View File

@ -389,17 +389,11 @@ class BaseModelForm(BaseForm):
if isinstance(field, InlineForeignKeyField): if isinstance(field, InlineForeignKeyField):
exclude.append(f_name) exclude.append(f_name)
# Clean the model instance's fields.
try: try:
self.instance.clean_fields(exclude=exclude) self.instance.full_clean(exclude=exclude,
validate_unique=False)
except ValidationError as e: except ValidationError as e:
self._update_errors(e.message_dict) self._update_errors(e)
# Call the model instance's clean method.
try:
self.instance.clean()
except ValidationError as e:
self._update_errors({NON_FIELD_ERRORS: e.messages})
# Validate uniqueness if needed. # Validate uniqueness if needed.
if self._validate_unique: if self._validate_unique:
@ -414,7 +408,7 @@ class BaseModelForm(BaseForm):
try: try:
self.instance.validate_unique(exclude=exclude) self.instance.validate_unique(exclude=exclude)
except ValidationError as e: except ValidationError as e:
self._update_errors(e.message_dict) self._update_errors(e)
def save(self, commit=True): def save(self, commit=True):
""" """

View File

@ -213,7 +213,7 @@ class TestSimpleValidators(TestCase):
self.assertEqual(repr(v), str_prefix("ValidationError([%(_)s'First Problem', %(_)s'Second Problem'])")) self.assertEqual(repr(v), str_prefix("ValidationError([%(_)s'First Problem', %(_)s'Second Problem'])"))
def test_message_dict(self): def test_message_dict(self):
v = ValidationError({'first': 'First Problem'}) v = ValidationError({'first': ['First Problem']})
self.assertEqual(str(v), str_prefix("{%(_)s'first': %(_)s'First Problem'}")) self.assertEqual(str(v), str_prefix("{%(_)s'first': %(_)s'First Problem'}"))
self.assertEqual(repr(v), str_prefix("ValidationError({%(_)s'first': %(_)s'First Problem'})")) self.assertEqual(repr(v), str_prefix("ValidationError({%(_)s'first': %(_)s'First Problem'})"))