Skip to content

Commit

Permalink
fixes #753 - ValidationError and CriticalValidationError now accept b…
Browse files Browse the repository at this point in the history
…oth strings and promises from gettext_lazy

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1328 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
Georg Bauer committed Nov 21, 2005
1 parent a49fa74 commit e4e28d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions django/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@

from django.conf.settings import JING_PATH
from django.utils.translation import gettext_lazy, ngettext
from django.utils.functional import Promise

class ValidationError(Exception):
def __init__(self, message):
"ValidationError can be passed a string or a list."
if isinstance(message, list):
self.messages = message
else:
assert isinstance(message, basestring), ("%s should be a string" % repr(message))
assert isinstance(message, (basestring, Promise)), ("%s should be a string" % repr(message))
self.messages = [message]
def __str__(self):
# This is needed because, without a __str__(), printing an exception
Expand All @@ -49,7 +50,7 @@ def __init__(self, message):
if isinstance(message, list):
self.messages = message
else:
assert isinstance(message, basestring), ("'%s' should be a string" % message)
assert isinstance(message, (basestring, Promise)), ("'%s' should be a string" % message)
self.messages = [message]
def __str__(self):
return str(self.messages)
Expand Down
10 changes: 9 additions & 1 deletion django/utils/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ def _curried(*moreargs, **morekwargs):
return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))
return _curried

class Promise:
"""
This is just a base class for the proxy class created in
the closure of the lazy function. It can be used to recognize
promises in code.
"""
pass

def lazy(func, *resultclasses):
"""
Turns any callable into a lazy evaluated callable. You need to give result
classes or types -- at least one is needed so that the automatic forcing of
the lazy evaluation code is triggered. Results are not memoized; the
function is evaluated on every access.
"""
class __proxy__:
class __proxy__(Promise):
# This inner class encapsulates the code that should be evaluated
# lazily. On calling of one of the magic methods it will force
# the evaluation and store the result. Afterwards, the result
Expand Down

0 comments on commit e4e28d9

Please sign in to comment.