Introduced "MaskedEmail" and allow a masked email in the Gasthausbox.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Wed, 8 Feb 2012 11:00:58 +0000 (11:00 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Wed, 8 Feb 2012 11:00:58 +0000 (11:00 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@1079 7aebc617-e5e2-0310-91dc-80fb5f6d2477

setup.py
tests/test_wrvalidators.py
wrpylib/wrmwcache.py
wrpylib/wrmwmarkup.py
wrpylib/wrvalidators.py

index f22045a620147eb950051f4233d1bb983c1d201a..49ce611217aca7d455cfe3363b5dd0efd47ea587 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
 from setuptools import setup
 
 setup(name='wrpylib',
-    version='0.0.18',
+    version='0.0.19',
     description='Winterrodeln Python Library',
     author='Philipp Spitzer',
     author_email='philipp.spitzer@winterrodeln.org',
index 3620adb3fcac847238cca79c35508ef01736e2e7..fb1ab7e5432caae1ba218fe4c4248c3426b54de4 100644 (file)
@@ -95,6 +95,16 @@ def test_PhoneCommentListNeinLoopNone():
     assert v.from_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456'
 
 
+def test_MaskedEmail():
+    v = wrpylib.wrvalidators.MaskedEmail()
+    assert v.to_python(u'') == (None, None)
+    assert v.to_python(u'abc.def@example.com') == (u'abc.def@example.com', False)
+    assert v.to_python(u'abc.def(at)example.com') == (u'abc.def@example.com', True)
+    assert v.from_python((None, None)) == u''
+    assert v.from_python((u'abc.def@example.com', False)) == u'abc.def@example.com'
+    assert v.from_python((u'abc.def@example.com', True)) == u'abc.def(at)example.com'
+
+
 def test_EmailCommentListNeinLoopNone():
     v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone()
     assert v.to_python(u'') == None
@@ -105,6 +115,20 @@ def test_EmailCommentListNeinLoopNone():
     assert v.from_python(u'Nein') == u'Nein'
     assert v.from_python(u'first@example.com') == u'first@example.com'
     assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com'
+    testvalue = u'abc.def(at)example.com (comment)'
+    try:
+        v.to_python(testvalue)
+        assert False
+    except formencode.Invalid:
+        pass
+    try:
+        v.from_python(testvalue)
+        assert False
+    except formencode.Invalid:
+        pass
+    v = wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True)
+    assert v.to_python(testvalue) == testvalue
+    assert v.from_python(testvalue) == testvalue
 
 
 def test_GermanLift():
index 7966c56f892359333b22ecd445b675fd5f581aab..d8c2caec738ee33a5c254f61002a98b91b37aa35 100644 (file)
@@ -19,6 +19,7 @@ def update_wrsledruncache(connection):
     
     >>> from sqlalchemy.engine import create_engine
     >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=0')
+    >>> # see: https://sourceforge.net/tracker/?func=detail&aid=2837134&group_id=22307&atid=374932
     >>> update_wrsledruncache(engine.connect())
     """
     metadata = schema.MetaData()
@@ -61,6 +62,7 @@ def update_wrinncache(connection):
     
     >>> from sqlalchemy.engine import create_engine
     >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=0')
+    >>> # see: https://sourceforge.net/tracker/?func=detail&aid=2837134&group_id=22307&atid=374932
     >>> update_wrinncache(engine.connect())
     """
     metadata = schema.MetaData()
@@ -103,6 +105,7 @@ def update_wrreportcache(connection, page_id=None):
 
     >>> from sqlalchemy.engine import create_engine
     >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1')
+    >>> # see: https://sourceforge.net/tracker/?func=detail&aid=2837134&group_id=22307&atid=374932
     >>> update_wrreportcache(engine.connect())
     """
     metadata = schema.MetaData()
@@ -143,6 +146,7 @@ def update_wrmapcache(connection):
     >>> from sqlalchemy.engine import create_engine
     >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=0')
     >>> # or: engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=0&passwd=XXXXX')
+    >>> # see: https://sourceforge.net/tracker/?func=detail&aid=2837134&group_id=22307&atid=374932
     >>> connection = engine.connect()
     >>> update_wrmapcache(connection)
     """
index 483c2dea1b3a80fc4c650c987676770cf1d553fc..62bf20a4e1f0030f129be282568f42ee190e8348 100644 (file)
@@ -143,7 +143,7 @@ def gasthausbox_to_inn(wikitext, inn=None):
         elif key == u'Rodelverleih': inn.sled_rental, inn.sled_rental_comment = _conv(wrpylib.wrvalidators.BoolUnicodeTupleValidator().to_python, value, key)
         elif key == u'Handyempfang': inn.mobile_provider = _conv(wrpylib.wrvalidators.ValueCommentListNeinLoopNone().to_python, value, key)
         elif key == u'Homepage': inn.homepage = _conv(wrpylib.wrvalidators.UrlNeinNone().to_python, value, key)
-        elif key == u'E-Mail': inn.email_list = _conv(wrpylib.wrvalidators.EmailCommentListNeinLoopNone().to_python, value, key)
+        elif key == u'E-Mail': inn.email_list = _conv(wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True).to_python, value, key)
         elif key == u'Telefon': inn.phone_list = _conv(wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True).to_python, value, key)
         elif key == u'Bild': inn.image = _conv(wrpylib.wrvalidators.UnicodeNone().to_python, value, key)
         elif key == u'Rodelbahnen': inn.sledding_list = _conv(wrpylib.wrvalidators.WikiPageListLoopNone().to_python, value, key)
@@ -174,7 +174,7 @@ def inn_to_gasthausbox(inn):
     keys.append(u'Homepage')
     values.append(wrpylib.wrvalidators.UrlNeinNone().from_python(inn.homepage))
     keys.append(u'E-Mail')
-    values.append(wrpylib.wrvalidators.EmailCommentListNeinLoopNone().from_python(inn.email_list))
+    values.append(wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True).from_python(inn.email_list))
     keys.append(u'Telefon')
     values.append(wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True).from_python(inn.phone_list))
     keys.append(u'Bild')
index 825050e15b9a2073d83547ea78ac24c9333ccf84..7d8b67023ace36d8418d9217c4119c464613413d 100644 (file)
@@ -598,6 +598,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 +638,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):