Author: Honza_Kral
Date: 2009-06-01 10:38:11 -0500 (Mon, 01 Jun 2009)
New Revision: 10867
Modified:
django/branches/soc2009/model-validation/django/contrib/localflavor/au/forms.py
django/branches/soc2009/model-validation/django/contrib/localflavor/ca/forms.py
django/branches/soc2009/model-validation/django/core/exceptions.py
django/branches/soc2009/model-validation/django/forms/__init__.py
django/branches/soc2009/model-validation/django/forms/fields.py
django/branches/soc2009/model-validation/django/forms/forms.py
django/branches/soc2009/model-validation/django/forms/formsets.py
django/branches/soc2009/model-validation/django/forms/models.py
django/branches/soc2009/model-validation/django/forms/util.py
django/branches/soc2009/model-validation/tests/regressiontests/forms/util.py
Log:
[soc2009/model-validation] Moved ValidationError to django.core.exceptions and
removed ErrorList from within the Error
Modified:
django/branches/soc2009/model-validation/django/contrib/localflavor/au/forms.py
===================================================================
---
django/branches/soc2009/model-validation/django/contrib/localflavor/au/forms.py
2009-06-01 15:37:41 UTC (rev 10866)
+++
django/branches/soc2009/model-validation/django/contrib/localflavor/au/forms.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -4,7 +4,7 @@
from django.forms import ValidationError
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 _
import re
Modified:
django/branches/soc2009/model-validation/django/contrib/localflavor/ca/forms.py
===================================================================
---
django/branches/soc2009/model-validation/django/contrib/localflavor/ca/forms.py
2009-06-01 15:37:41 UTC (rev 10866)
+++
django/branches/soc2009/model-validation/django/contrib/localflavor/ca/forms.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -4,7 +4,7 @@
from django.forms import ValidationError
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 _
import re
Modified: django/branches/soc2009/model-validation/django/core/exceptions.py
===================================================================
--- django/branches/soc2009/model-validation/django/core/exceptions.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/core/exceptions.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -34,4 +34,21 @@
class ValidationError(Exception):
"""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)
Modified: django/branches/soc2009/model-validation/django/forms/__init__.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/__init__.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/forms/__init__.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -10,7 +10,7 @@
"This form field requires foo.js" and form.js_includes()
"""
-from util import ValidationError
+from django.core.exceptions import ValidationError
from widgets import *
from fields import *
from forms import *
Modified: django/branches/soc2009/model-validation/django/forms/fields.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/fields.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/forms/fields.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -23,12 +23,14 @@
except NameError:
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.encoding import smart_unicode, smart_str
-from util import ErrorList, ValidationError
-from widgets import TextInput, PasswordInput, HiddenInput,
MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect,
SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget,
SplitHiddenDateTimeWidget
+from util import ErrorList
+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
__all__ = (
@@ -683,16 +685,9 @@
value = super(TypedChoiceField, self).clean(value)
if value == self.empty_value or value in EMPTY_VALUES:
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:
value = self.coerce(value)
- except (ValueError, TypeError, django.core.exceptions.ValidationError):
+ except (ValueError, TypeError, ValidationError):
raise ValidationError(self.error_messages['invalid_choice'] %
{'value': value})
return value
Modified: django/branches/soc2009/model-validation/django/forms/forms.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/forms.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/forms/forms.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -4,6 +4,7 @@
from copy import deepcopy
+from django.core.exceptions import ValidationError
from django.utils.datastructures import SortedDict
from django.utils.html import conditional_escape
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
@@ -11,7 +12,7 @@
from fields import Field, FileField
from widgets import Media, media_property, TextInput, Textarea
-from util import flatatt, ErrorDict, ErrorList, ValidationError
+from util import flatatt, ErrorDict, ErrorList
__all__ = ('BaseForm', 'Form')
@@ -243,13 +244,13 @@
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
except ValidationError, e:
- self._errors[name] = e.messages
+ self._errors[name] = self.error_class(e.messages)
if name in self.cleaned_data:
del self.cleaned_data[name]
try:
self.cleaned_data = self.clean()
except ValidationError, e:
- self._errors[NON_FIELD_ERRORS] = e.messages
+ self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages)
if self._errors:
delattr(self, 'cleaned_data')
Modified: django/branches/soc2009/model-validation/django/forms/formsets.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/formsets.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/forms/formsets.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -1,10 +1,11 @@
from forms import Form
+from django.core.exceptions import ValidationError
from django.utils.encoding import StrAndUnicode
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from fields import IntegerField, BooleanField
from widgets import Media, HiddenInput
-from util import ErrorList, ErrorDict, ValidationError
+from util import ErrorList
__all__ = ('BaseFormSet', 'all_valid')
Modified: django/branches/soc2009/model-validation/django/forms/models.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/models.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/forms/models.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -8,7 +8,8 @@
from django.utils.text import get_text_list, capfirst
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 fields import Field, ChoiceField, IntegerField, EMPTY_VALUES
from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput
Modified: django/branches/soc2009/model-validation/django/forms/util.py
===================================================================
--- django/branches/soc2009/model-validation/django/forms/util.py
2009-06-01 15:37:41 UTC (rev 10866)
+++ django/branches/soc2009/model-validation/django/forms/util.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -1,5 +1,5 @@
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
def flatatt(attrs):
@@ -48,21 +48,3 @@
def __repr__(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)
Modified:
django/branches/soc2009/model-validation/tests/regressiontests/forms/util.py
===================================================================
---
django/branches/soc2009/model-validation/tests/regressiontests/forms/util.py
2009-06-01 15:37:41 UTC (rev 10866)
+++
django/branches/soc2009/model-validation/tests/regressiontests/forms/util.py
2009-06-01 15:38:11 UTC (rev 10867)
@@ -5,6 +5,7 @@
tests = r"""
>>> from django.forms.util import *
+>>> from django.core.exceptions import ValidationError
>>> from django.utils.translation import ugettext_lazy
###########
@@ -24,36 +25,36 @@
###################
# 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>
# 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>
# 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>
# 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>
# 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>
>>> class VeryBadError:
... def __unicode__(self): return u"A very bad error."
# Can take a non-string.
->>> print ValidationError(VeryBadError()).messages
+>>> print ErrorList(ValidationError(VeryBadError()).messages)
<ul class="errorlist"><li>A very bad error.</li></ul>
# Escapes non-safe input but not input marked safe.
>>> 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>
->>> 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>
"""
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---