X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/blobdiff_plain/a33141494660bee6dda0adbabb3281b520c2f797..89ce18f172baf648a6aec45910419a12d26e99ef:/wrpylib/wrvalidators.py diff --git a/wrpylib/wrvalidators.py b/wrpylib/wrvalidators.py index 825050e..d721da0 100644 --- a/wrpylib/wrvalidators.py +++ b/wrpylib/wrvalidators.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2.6 +#!/usr/bin/python2.7 # -*- coding: iso-8859-15 -*- # $Id$ # $HeadURL$ @@ -6,12 +6,12 @@ of properties used in the "Rodelbahnbox" and "Gasthausbox". The "to_python" method has to get a unicode argument. """ -import formencode -import formencode.national import datetime import re import xml.dom.minidom as minidom from xml.parsers.expat import ExpatError +import formencode +import formencode.national class NoneValidator(formencode.FancyValidator): @@ -149,7 +149,8 @@ class DictValidator(formencode.FancyValidator): def from_python(self, value): for k, v in self.dict.iteritems(): - if type(v) == type(value) and v == value: return k + if v == value: + return k raise formencode.Invalid('Invalid value', value, None) @@ -598,6 +599,38 @@ class PhoneCommentListNeinLoopNone(NoneValidator): 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. @@ -606,9 +639,12 @@ class EmailCommentListNeinLoopNone(NoneValidator): 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):