+class MaskedEmail(formencode.FancyValidator):
+ """A masked email address as defined here is an email address that has the `@` character replacted by the text `(at)`.
+ So instead of `abd.def@example.com` it would be `abc.def(at)example.com`.
+ This validator takes either a normal or a masked email address in it's to_python method and returns the normal email address as well
+ as a bool indicating whether the email address was masked.
+ u'' <=> (None, None)
+ u'abc.def@example.com' <=> (u'abc.def@example.com', False)
+ u'abc.def(at)example.com' <=> (u'abc.def@example.com', True)
+
+ """
+ def __init__(self, *args, **kw):
+ if not kw.has_key('strip'): kw['strip'] = True
+ if not kw.has_key('not_empty'): kw['not_empty'] = False
+ if not kw.has_key('if_empty'): kw['if_empty'] = (None, None)
+ self.at = '(at)'
+ formencode.FancyValidator.__init__(self, *args, **kw)
+
+ def _to_python(self, value, state):
+ email = value.replace(self.at, '@')
+ masked = value != email
+ val_email = formencode.validators.Email()
+ return val_email.to_python(email), masked
+
+ def _from_python(self, value, state):
+ email, masked = value
+ if email is None: return u''
+ val_email = formencode.validators.Email()
+ email = val_email.from_python(email)
+ if masked: email = email.replace('@', self.at)
+ return email
+
+