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)
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