开发者

Error when the Email formencode validator

I wanted to create an IDN-aware formencode validator to use in one of my projects. I used a portion of code from the Django project (http://code.djangoproject.com/svn/django/trunk/django/core/validators.py) to do that, but there must be a trivial error in my code I can't find :

class Email(formencode.validators.Email):
    def _to_python(self, value, state):
        try:
            return super(Email, self)._to_python(value, state)
        except formencode.Invalid as e:
            # Trivial case failed. Try for possible IDN domain-part

            print 'heywo !'

            if value and u'@' in value:
                parts = value.split(u'@')
                try:
                    parts[-1] = parts[-1].encode('idna')
                except UnicodeError:
                    raise e

                try:
                    super(Email, self)._to_python(u'@'.join(parts), state)
                except formencode.Invalid as ex:
                    raise ex

                return value
            else:
                raise e

When I try to validate an email with an IDN domain (ex: test@wääl.de), the Invalid exception raised by the first call is thrown, and the portion of code after the first except is never executed ('heywo !' is never printed).

There is an example :

>>> from test.lib.validators import Email
>>> Email().to_python(u'test@zääz.de')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/FormEncode-1.2.3dev-py2.6.egg/formencode    /api.py", line 416, in to_python
    vp(value, state)
  File "/usr/local/lib/python2.6/dist-packages/FormEncode-1.2.3dev-py2.6.egg/formencode    /validators.py", line 1352, in validate_python
    value, state)
Invalid: The domain por开发者_运维技巧tion of the email address is invalid (the portion after the @: z\xe4\xe4z.de)

What did I do wrong ?

Thanks.


Okay, found the answer. I was overloading _to_python instead of validate_python. The class now looks like :

class Email(formencode.validators.Email):
    def validate_python(self, value, state):
        try:
            super(Email, self).validate_python(value, state)
        except formencode.Invalid as e:
            # Trivial case failed. Try for possible IDN domain-part
            if value and u'@' in value:
                parts = value.split(u'@')
                try:
                    parts[-1] = parts[-1].encode('idna')
                except UnicodeError:
                    raise e

                try:
                    super(Email, self).validate_python(u'@'.join(parts), state)
                except formencode.Invalid as ex:
                    raise ex
            else:
                raise e

It's working perfectly :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜