Frage

Ich wollte eine IDN-aware formencode Validator Verwendung in einem meiner Projekte erstellen. Ich benutzen einen Teil des Codes aus dem Django-Projekt (http://code.djangoproject.com/svn/django/trunk/django/core/validators.py), das zu tun, aber es muss ein trivialer Fehler in meinem Code mir sein nicht finden kann:

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

Wenn ich versuche, eine E-Mail mit einer IDN-Domain zu überprüfen (zB: test@wääl.de), die ungültige Ausnahme von dem ersten Anruf ausgelöst wird geworfen, und der Teil des Codes nach dem ersten Ausnahme nie ausgeführt wird ( 'heywo ‘wird nie gedruckt).

Es gibt ein Beispiel:

>>> 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 portion of the email address is invalid (the portion after the @: z\xe4\xe4z.de)

Was habe ich falsch gemacht?

Danke.

War es hilfreich?

Lösung

Okay, fand die Antwort. Ich war Überlastung _to_python statt validate_python. Die Klasse nun wie folgt aussieht:

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

Es funktioniert perfekt:)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top