mirror of
https://github.com/django/django.git
synced 2025-07-07 11:19:12 +00:00
[soc2009/model-validation] Moved ValidationError to django.core.exceptions and removed ErrorList from within the Error
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10867 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a2b2152635
commit
55e23d5efa
@ -4,7 +4,7 @@ Australian-specific Form helpers
|
|||||||
|
|
||||||
from django.forms import ValidationError
|
from django.forms import ValidationError
|
||||||
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
|
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
|
||||||
from django.forms.util import smart_unicode
|
from django.utils.encoding import smart_unicode
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ Canada-specific Form helpers
|
|||||||
|
|
||||||
from django.forms import ValidationError
|
from django.forms import ValidationError
|
||||||
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
|
from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
|
||||||
from django.forms.util import smart_unicode
|
from django.utils.encoding import smart_unicode
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -34,4 +34,21 @@ class FieldError(Exception):
|
|||||||
|
|
||||||
class ValidationError(Exception):
|
class ValidationError(Exception):
|
||||||
"""An error while validating data."""
|
"""An error while validating data."""
|
||||||
pass
|
def __init__(self, message):
|
||||||
|
from django.utils.encoding import force_unicode
|
||||||
|
"""
|
||||||
|
ValidationError can be passed any object that can be printed (usually
|
||||||
|
a string) or a list of objects.
|
||||||
|
"""
|
||||||
|
if isinstance(message, list):
|
||||||
|
self.messages = [force_unicode(msg) for msg in message]
|
||||||
|
else:
|
||||||
|
message = force_unicode(message)
|
||||||
|
self.messages = [message]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
# This is needed because, without a __str__(), printing an exception
|
||||||
|
# instance would result in this:
|
||||||
|
# AttributeError: ValidationError instance has no attribute 'args'
|
||||||
|
# See http://www.python.org/doc/current/tut/node10.html#handling
|
||||||
|
return repr(self.messages)
|
||||||
|
@ -10,7 +10,7 @@ TODO:
|
|||||||
"This form field requires foo.js" and form.js_includes()
|
"This form field requires foo.js" and form.js_includes()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from util import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from widgets import *
|
from widgets import *
|
||||||
from fields import *
|
from fields import *
|
||||||
from forms import *
|
from forms import *
|
||||||
|
@ -23,12 +23,14 @@ try:
|
|||||||
except NameError:
|
except NameError:
|
||||||
from sets import Set as set
|
from sets import Set as set
|
||||||
|
|
||||||
import django.core.exceptions
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.utils.encoding import smart_unicode, smart_str
|
from django.utils.encoding import smart_unicode, smart_str
|
||||||
|
|
||||||
from util import ErrorList, ValidationError
|
from util import ErrorList
|
||||||
from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget
|
from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, \
|
||||||
|
FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, \
|
||||||
|
DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -683,16 +685,9 @@ class TypedChoiceField(ChoiceField):
|
|||||||
value = super(TypedChoiceField, self).clean(value)
|
value = super(TypedChoiceField, self).clean(value)
|
||||||
if value == self.empty_value or value in EMPTY_VALUES:
|
if value == self.empty_value or value in EMPTY_VALUES:
|
||||||
return self.empty_value
|
return self.empty_value
|
||||||
|
|
||||||
# Hack alert: This field is purpose-made to use with Field.to_python as
|
|
||||||
# a coercion function so that ModelForms with choices work. However,
|
|
||||||
# Django's Field.to_python raises
|
|
||||||
# django.core.exceptions.ValidationError, which is a *different*
|
|
||||||
# exception than django.forms.util.ValidationError. So we need to catch
|
|
||||||
# both.
|
|
||||||
try:
|
try:
|
||||||
value = self.coerce(value)
|
value = self.coerce(value)
|
||||||
except (ValueError, TypeError, django.core.exceptions.ValidationError):
|
except (ValueError, TypeError, ValidationError):
|
||||||
raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
|
raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ Form classes
|
|||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.datastructures import SortedDict
|
from django.utils.datastructures import SortedDict
|
||||||
from django.utils.html import conditional_escape
|
from django.utils.html import conditional_escape
|
||||||
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
|
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
|
||||||
@ -11,7 +12,7 @@ from django.utils.safestring import mark_safe
|
|||||||
|
|
||||||
from fields import Field, FileField
|
from fields import Field, FileField
|
||||||
from widgets import Media, media_property, TextInput, Textarea
|
from widgets import Media, media_property, TextInput, Textarea
|
||||||
from util import flatatt, ErrorDict, ErrorList, ValidationError
|
from util import flatatt, ErrorDict, ErrorList
|
||||||
|
|
||||||
__all__ = ('BaseForm', 'Form')
|
__all__ = ('BaseForm', 'Form')
|
||||||
|
|
||||||
@ -243,13 +244,13 @@ class BaseForm(StrAndUnicode):
|
|||||||
value = getattr(self, 'clean_%s' % name)()
|
value = getattr(self, 'clean_%s' % name)()
|
||||||
self.cleaned_data[name] = value
|
self.cleaned_data[name] = value
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
self._errors[name] = e.messages
|
self._errors[name] = self.error_class(e.messages)
|
||||||
if name in self.cleaned_data:
|
if name in self.cleaned_data:
|
||||||
del self.cleaned_data[name]
|
del self.cleaned_data[name]
|
||||||
try:
|
try:
|
||||||
self.cleaned_data = self.clean()
|
self.cleaned_data = self.clean()
|
||||||
except ValidationError, e:
|
except ValidationError, e:
|
||||||
self._errors[NON_FIELD_ERRORS] = e.messages
|
self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages)
|
||||||
if self._errors:
|
if self._errors:
|
||||||
delattr(self, 'cleaned_data')
|
delattr(self, 'cleaned_data')
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
from forms import Form
|
from forms import Form
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
from django.utils.encoding import StrAndUnicode
|
from django.utils.encoding import StrAndUnicode
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from fields import IntegerField, BooleanField
|
from fields import IntegerField, BooleanField
|
||||||
from widgets import Media, HiddenInput
|
from widgets import Media, HiddenInput
|
||||||
from util import ErrorList, ErrorDict, ValidationError
|
from util import ErrorList
|
||||||
|
|
||||||
__all__ = ('BaseFormSet', 'all_valid')
|
__all__ = ('BaseFormSet', 'all_valid')
|
||||||
|
|
||||||
|
@ -8,7 +8,8 @@ from django.utils.datastructures import SortedDict
|
|||||||
from django.utils.text import get_text_list, capfirst
|
from django.utils.text import get_text_list, capfirst
|
||||||
from django.utils.translation import ugettext_lazy as _, ugettext
|
from django.utils.translation import ugettext_lazy as _, ugettext
|
||||||
|
|
||||||
from util import ValidationError, ErrorList
|
from django.core.exceptions import ValidationError
|
||||||
|
from util import ErrorList
|
||||||
from forms import BaseForm, get_declared_fields, NON_FIELD_ERRORS
|
from forms import BaseForm, get_declared_fields, NON_FIELD_ERRORS
|
||||||
from fields import Field, ChoiceField, IntegerField, EMPTY_VALUES
|
from fields import Field, ChoiceField, IntegerField, EMPTY_VALUES
|
||||||
from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput
|
from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from django.utils.html import conditional_escape
|
from django.utils.html import conditional_escape
|
||||||
from django.utils.encoding import smart_unicode, StrAndUnicode, force_unicode
|
from django.utils.encoding import StrAndUnicode, force_unicode
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
def flatatt(attrs):
|
def flatatt(attrs):
|
||||||
@ -48,21 +48,3 @@ class ErrorList(list, StrAndUnicode):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr([force_unicode(e) for e in self])
|
return repr([force_unicode(e) for e in self])
|
||||||
|
|
||||||
class ValidationError(Exception):
|
|
||||||
def __init__(self, message):
|
|
||||||
"""
|
|
||||||
ValidationError can be passed any object that can be printed (usually
|
|
||||||
a string) or a list of objects.
|
|
||||||
"""
|
|
||||||
if isinstance(message, list):
|
|
||||||
self.messages = ErrorList([smart_unicode(msg) for msg in message])
|
|
||||||
else:
|
|
||||||
message = smart_unicode(message)
|
|
||||||
self.messages = ErrorList([message])
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
# This is needed because, without a __str__(), printing an exception
|
|
||||||
# instance would result in this:
|
|
||||||
# AttributeError: ValidationError instance has no attribute 'args'
|
|
||||||
# See http://www.python.org/doc/current/tut/node10.html#handling
|
|
||||||
return repr(self.messages)
|
|
||||||
|
@ -5,6 +5,7 @@ Tests for forms/util.py module.
|
|||||||
|
|
||||||
tests = r"""
|
tests = r"""
|
||||||
>>> from django.forms.util import *
|
>>> from django.forms.util import *
|
||||||
|
>>> from django.core.exceptions import ValidationError
|
||||||
>>> from django.utils.translation import ugettext_lazy
|
>>> from django.utils.translation import ugettext_lazy
|
||||||
|
|
||||||
###########
|
###########
|
||||||
@ -24,36 +25,36 @@ u''
|
|||||||
###################
|
###################
|
||||||
|
|
||||||
# Can take a string.
|
# Can take a string.
|
||||||
>>> print ValidationError("There was an error.").messages
|
>>> print ErrorList(ValidationError("There was an error.").messages)
|
||||||
<ul class="errorlist"><li>There was an error.</li></ul>
|
<ul class="errorlist"><li>There was an error.</li></ul>
|
||||||
|
|
||||||
# Can take a unicode string.
|
# Can take a unicode string.
|
||||||
>>> print ValidationError(u"Not \u03C0.").messages
|
>>> print ErrorList(ValidationError(u"Not \u03C0.").messages)
|
||||||
<ul class="errorlist"><li>Not π.</li></ul>
|
<ul class="errorlist"><li>Not π.</li></ul>
|
||||||
|
|
||||||
# Can take a lazy string.
|
# Can take a lazy string.
|
||||||
>>> print ValidationError(ugettext_lazy("Error.")).messages
|
>>> print ErrorList(ValidationError(ugettext_lazy("Error.")).messages)
|
||||||
<ul class="errorlist"><li>Error.</li></ul>
|
<ul class="errorlist"><li>Error.</li></ul>
|
||||||
|
|
||||||
# Can take a list.
|
# Can take a list.
|
||||||
>>> print ValidationError(["Error one.", "Error two."]).messages
|
>>> print ErrorList(ValidationError(["Error one.", "Error two."]).messages)
|
||||||
<ul class="errorlist"><li>Error one.</li><li>Error two.</li></ul>
|
<ul class="errorlist"><li>Error one.</li><li>Error two.</li></ul>
|
||||||
|
|
||||||
# Can take a mixture in a list.
|
# Can take a mixture in a list.
|
||||||
>>> print ValidationError(["First error.", u"Not \u03C0.", ugettext_lazy("Error.")]).messages
|
>>> print ErrorList(ValidationError(["First error.", u"Not \u03C0.", ugettext_lazy("Error.")]).messages)
|
||||||
<ul class="errorlist"><li>First error.</li><li>Not π.</li><li>Error.</li></ul>
|
<ul class="errorlist"><li>First error.</li><li>Not π.</li><li>Error.</li></ul>
|
||||||
|
|
||||||
>>> class VeryBadError:
|
>>> class VeryBadError:
|
||||||
... def __unicode__(self): return u"A very bad error."
|
... def __unicode__(self): return u"A very bad error."
|
||||||
|
|
||||||
# Can take a non-string.
|
# Can take a non-string.
|
||||||
>>> print ValidationError(VeryBadError()).messages
|
>>> print ErrorList(ValidationError(VeryBadError()).messages)
|
||||||
<ul class="errorlist"><li>A very bad error.</li></ul>
|
<ul class="errorlist"><li>A very bad error.</li></ul>
|
||||||
|
|
||||||
# Escapes non-safe input but not input marked safe.
|
# Escapes non-safe input but not input marked safe.
|
||||||
>>> example = 'Example of link: <a href="http://www.example.com/">example</a>'
|
>>> example = 'Example of link: <a href="http://www.example.com/">example</a>'
|
||||||
>>> print ValidationError(example).messages
|
>>> print ErrorList([example])
|
||||||
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
|
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
|
||||||
>>> print ValidationError(mark_safe(example)).messages
|
>>> print ErrorList([mark_safe(example)])
|
||||||
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
|
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user