]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/commitdiff
Implemented PhoneInfo validator (validates telephone numbers with info, like "0512...
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Fri, 3 Apr 2009 19:31:36 +0000 (19:31 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Fri, 3 Apr 2009 19:31:36 +0000 (19:31 +0000)
git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wradmin@441 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wradmin/wradmin/controllers/rodelbahn.py
wradmin/wradmin/lib/mediawiki.py
wradmin/wradmin/model/validators.py
wradmin/wradmin/tests/test_models.py

index a558e04f49202725bfb9f8ed5cca96c7c7754548..1aff3f22037208c78f532ab28e7c5ae898831ca5 100644 (file)
@@ -9,8 +9,9 @@ import webhelpers.paginate as paginate
 from wradmin.lib.base import BaseController, render
 import wradmin.model as model
 import sqlalchemy as sa
+import formencode
 import re
-from wradmin.lib.mediawiki import to_bool, to_unsigned, to_date, to_geo, to_title, to_tristate, to_email, to_url, to_phone, conv, unicode_e
+from wradmin.lib.mediawiki import to_bool, to_unsigned, to_date, to_geo, to_title, to_tristate, to_email, to_url, to_phone, to_phone_info, conv, unicode_e
 
 log = logging.getLogger(__name__)
 
@@ -73,7 +74,7 @@ class RodelbahnController(BaseController):
             elif key == u'Öffentliche Anreise': sl.public_transport = conv(to_bool, value, u'Öffentliche Anreise')
             elif key == u'Bild': sl.image = value
             elif key == u'Position': (sl.position_latitude, sl.position_longitude) = conv(to_geo, value, u'Position') # '47.583333 N 15.75 E'
-            elif key == u'Auskunft': sl.information = value
+            elif key == u'Auskunft': sl.information = conv(to_phone_info, value, u'Auskunft')
             elif key == u'In Übersichtskarte': sl.show_in_overview = conv(to_bool, value, u'In Übersichtskarte')
             elif key == u'Aufnahmedatum': sl.creation_date = conv(to_date, value, u'Aufnahmedatum') # '2006-03-15'
             else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None)
index 1da5ffc3dce88bafc5fc2f5beb582f687c03d0e4..5c1e0be09b873718a49297970c610a964e8169a7 100644 (file)
@@ -59,6 +59,10 @@ def to_phone(value):
     return model.validators.AustrianPhoneNumber(messages={'phoneFormat': u"Telefonnummer %%(value)s muss das Format 0123/456789 oder +43/123/456789 haben"}).to_python(value)
 
 
+def to_phone_info(value):
+    return model.validators.PhoneInfo(messages={'phoneInfo': u"Bitte verwenden Sie ein Format wie '0512/123456 (Schnee Alm)'."}).to_python(value)
+
+
 def conv(fnct, value, fieldname):
     "Like one of the to_xxx functions (e.g. to_bool), but adds the field name to the error message"
     try: return fnct(value)
index f3f8fa6ba72148c3bf896057966710e613070fb1..e8e9d88649a7aa9a706c70caac425b33c3adf4bf 100644 (file)
@@ -156,3 +156,19 @@ class AustrianPhoneNumber(formencode.FancyValidator):
         
         if extension is None: return '+%s/%s' % (country, phone)
         return '+%s/%s-%s' % (country, phone, extension)
+
+
+class PhoneInfo(formencode.FancyValidator):
+    "Validates a info of the form '0644/1234567 (Schnee Alm)'"
+    messages = {'infoFormat': "'%%(value)s' is no valid format, please use a form like '0644/1234567 (Schnee Alm)'"}
+    
+    def _to_python(self, value, state):
+        self.assert_string(value, state)
+        m = re.match('^([-\d/\+]{5,}) \((.+)\)', value)
+        if m is None: raise formencode.Invalid(self.message('infoFormat', state) % {'value': value}, value, state)
+        (phone, info) = m.groups()
+        
+        # check phone
+        phone = AustrianPhoneNumber().to_python(phone)
+        
+        return "%s (%s)" % (phone, info)
index 127d89f1632d6afb5efcdcfeb2afab8f645fce40..542e0ff1171932d92b15ded71b969ce0c14b9281 100644 (file)
@@ -49,7 +49,7 @@ def test_geo():
 
 def test_AustrianPhoneNumber():
     v = wradmin.model.validators.AustrianPhoneNumber()
-    assert v.to_python('') is None
+    assert v.to_python(u'') is None
     assert v.to_python(u'0512/12345678') == u'+43/512/12345678'
     assert v.to_python(u'+43/512/12345678') == u'+43/512/12345678'
     assert v.to_python(u'0512/1234567-89') == u'+43/512/1234567-89'
@@ -59,3 +59,17 @@ def test_AustrianPhoneNumber():
             v.to_python(n) # has to throw an exception
             assert False, u"The telephone number '%s' should throw an exception." % n
         except formencode.Invalid: pass
+
+
+def test_PhoneInfo():
+    v = wradmin.model.validators.PhoneInfo()
+    assert v.to_python(u'') is None
+    assert v.to_python(u'0512/12345678 (Schnee Alm)') == u'+43/512/12345678 (Schnee Alm)'
+    assert v.to_python(u'+43/512/12345678 (Schnee Alm)') == u'+43/512/12345678 (Schnee Alm)'
+    assert v.to_python(u'0512/12345678 (Schnee (Winter) Alm)') == u'+43/512/12345678 (Schnee (Winter) Alm)'
+    for n in [u'0512/12345678', u'+43/512/12345678', u'+43 (Schnee Alm)', u'0512/12345678 ()', u'(Schnee Alm)', u'(Schnee Alm) +43/512/12345678', u'+43/512/12345678 Schnee Alm']:
+        try:
+            v.to_python(n) # has to throw an exception
+            assert False, u"The telephone info '%s' should throw an exception." % n
+        except formencode.Invalid: pass        
+    
\ No newline at end of file