]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/commitdiff
AustrianPhoneNumber converter written
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 30 Mar 2009 20:54:56 +0000 (20:54 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 30 Mar 2009 20:54:56 +0000 (20:54 +0000)
git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wradmin@439 7aebc617-e5e2-0310-91dc-80fb5f6d2477

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

index 5351d4b6bff59f7b825e08ae88fb234f65b2ecb6..1da5ffc3dce88bafc5fc2f5beb582f687c03d0e4 100644 (file)
@@ -56,8 +56,7 @@ def to_url(value):
 
 
 def to_phone(value):
-    # return formencode.national.InternationalPhoneNumber(default_cc=43, messages={'phoneFormat': u"Telefonnummer muss das Format 0123/456789 oder +43/123/456789 haben"}).to_python(value)
-    return value # I should write my own PhoneNumber check.
+    return model.validators.AustrianPhoneNumber(messages={'phoneFormat': u"Telefonnummer %%(value)s muss das Format 0123/456789 oder +43/123/456789 haben"}).to_python(value)
 
 
 def conv(fnct, value, fieldname):
index 94955b729b3372ca1c207f9c9595791ed9b00b47..af761646a7a7deb9a8d693c917994c5b4cb3439b 100644 (file)
@@ -110,3 +110,49 @@ class Geo(formencode.FancyValidator):
         if value == (None, None): return ''
         latitude, longitude = value
         return u'%.6f N %.6f E' % (latitude, longitude)
+
+
+class AustrianPhoneNumber(formencode.FancyValidator):
+    """
+    Validates and converts phone numbers to +##/###/####### or +##/###/#######-### (having an extension)
+    @param  default_cc      country code for prepending if none is provided, defaults to 43 (Austria)
+    ::
+        >>> v = AustrianPhoneNumber()
+        >>> v.to_python(u'0512/12345678')
+        u'+43/512/12345678'
+        >>> v.to_python(u'+43/512/12345678')
+        u'+43/512/12345678'
+        >>> v.to_python(u'0512/1234567-89') # 89 is the extension
+        u'+43/512/1234567-89'
+        >>> v.to_python(u'+43/512/1234567-89')
+        u'+43/512/1234567-89'
+        >>> v.to_python(u'0512 / 12345678') # Exception
+        >>> v.to_python(u'0512-12345678') # Exception
+    """
+    # Inspired by formencode.national.InternationalPhoneNumber
+
+    default_cc = 43 # Default country code
+    messages = {'phoneFormat': "'%%(value)s' is an invalid format. Please enter a number in the form +43/###/####### or 0###/########."}
+
+    def _to_python(self, value, state):
+        self.assert_string(value, state)
+        m = re.match(u'(?:\+(\d+)/)?([\d/]+)(?:-(\d+))?', value)
+        # This will separate 
+        #     u'+43/512/1234567-89'  => (u'43', u'512/1234567', u'89')
+        #     u'+43/512/1234/567-89' => (u'43', u'512/1234/567', u'89')
+        #     u'+43/512/1234/567'    => (u'43', u'512/1234/567', None)
+        #     u'0512/1234567'        => (None, u'0512/1234567', None)
+        if m is None: raise formencode.Invalid(self.message('phoneFormat', state) % {'value': value}, value, state)
+        (country, phone, extension) = m.groups()
+        
+        # Phone
+        if phone.find(u'//') > -1: raise formencode.Invalid(self.message('phoneFormat', state) % {'value': value}, value, state)
+        
+        # Country
+        if country is None:
+            if phone[0] != '0': raise formencode.Invalid(self.message('phoneFormat', state) % {'value': value}, value, state)
+            phone = phone[1:]
+            country = unicode(self.default_cc)
+        
+        if extension is None: return '+%s/%s' % (country, phone)
+        return '+%s/%s-%s' % (country, phone, extension)
index 75946faf5a8001ebd92d37e119d45732de8194ce..d9ec59542574f8ea0b71fdff1d075d10f88cae17 100644 (file)
@@ -46,3 +46,16 @@ def test_geo():
     assert v.from_python((lat, lon)) == coord
     assert v.from_python((None, None)) == u''
 
+
+def test_AustrianPhoneNumber():
+    v = wradmin.model.validators.AustrianPhoneNumber()
+    assert v.to_python('') 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'
+    assert v.to_python(u'+43/512/1234567-89') == u'+43/512/1234567-89'
+    for n in [u'0512 / 12345678', u'0512-12345678']:
+        try:
+            v.to_python(n) # has to throw an exception
+            assert True, u"The telephone number '%s' should throw an exception." % v
+        except formencode.Invalid: pass