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