NoneValidator.__init__(self, NeinValidator(Loop(ValueCommentList(PhoneNumber(default_cc=43), comments_are_optional=comments_are_optional))))
+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
+
+
class EmailCommentListNeinLoopNone(NoneValidator):
"""Converts a semicolon-separated list of email addresses with optional comments to itself.
The special value of u'Nein' indicates that there are no email addresses.
u'Nein' <=> u'Nein'
u'first@example.com' <=> u'first@example.com'
u'first@example.com (Nur Winter); second@example.com' <=> u'first@example.com (Nur Winter); second@example.com'
+
+ If the parameter allow_masked_email is true, the following gives no error:
+ u'abc.def(at)example.com (comment)' <=> u'abc.def(at)example.com (comment)'
"""
- def __init__(self):
- NoneValidator.__init__(self, NeinValidator(Loop(ValueCommentList(formencode.validators.Email()))))
+ def __init__(self, allow_masked_email=False):
+ NoneValidator.__init__(self, NeinValidator(Loop(ValueCommentList(MaskedEmail() if allow_masked_email else formencode.validators.Email()))))
class WikiPage(formencode.FancyValidator):