+class RodelbahnboxDictConverter(formencode.Validator):
+ """Converts a dict with Rodelbahnbox properties to a Sledrun class. Does no validation."""
+
+ def to_python(self, value, state=None):
+ """value is a dict of properties. If state is an object with the attribute sledrun, this sledrun class will be populated or updated."""
+ props = value
+ if isinstance(state, object) and hasattr(state, 'sledrun'):
+ sledrun = state.sledrun
+ else:
+ class Sledrun(object):
+ pass
+ sledrun = Sledrun()
+ for k, v in props.items():
+ if k == 'Position': sledrun.position_latitude, sledrun.position_longitude = v
+ elif k == 'Position oben': sledrun.top_latitude, sledrun.top_longitude = v
+ elif k == 'Höhe oben': sledrun.top_elevation = v
+ elif k == 'Position unten': sledrun.bottom_latitude, sledrun.bottom_longitude = v
+ elif k == 'Höhe unten': sledrun.bottom_elevation = v
+ elif k == 'Länge': sledrun.length = v
+ elif k == 'Schwierigkeit': sledrun.difficulty = v
+ elif k == 'Lawinen': sledrun.avalanches = v
+ elif k == 'Betreiber': sledrun.operator = v
+ elif k == 'Öffentliche Anreise': sledrun.public_transport = v
+ elif k == 'Aufstieg möglich': sledrun.walkup_possible = v
+ elif k == 'Aufstieg getrennt': sledrun.walkup_separate, sledrun.walkup_separate_comment = v
+ elif k == 'Gehzeit': sledrun.walkup_time = v
+ elif k == 'Aufstiegshilfe': sledrun.lift, sledrun.lift_details = v
+ elif k == 'Beleuchtungsanlage': sledrun.night_light, sledrun.night_light_comment = v
+ elif k == 'Beleuchtungstage': sledrun.night_light_days, sledrun.night_light_days_comment = v
+ elif k == 'Rodelverleih': sledrun.sled_rental, sledrun.sled_rental_comment = v
+ elif k == 'Gütesiegel': sledrun.cachet = v
+ elif k == 'Webauskunft': sledrun.information_web = v
+ elif k == 'Telefonauskunft': sledrun.information_phone = v
+ elif k == 'Bild': sledrun.image = v
+ elif k == 'In Übersichtskarte': sledrun.show_in_overview = v
+ elif k == 'Forumid': sledrun.forum_id = v
+ return sledrun
+
+ def from_python(self, value, state=None):
+ """Converts a sledrun class to a dict of Rodelbahnbox properties. value is a sledrun instance."""
+ sledrun = value
+ r = collections.OrderedDict()
+ r['Position'] = (sledrun.position_latitude, sledrun.position_longitude)
+ r['Position oben'] = (sledrun.top_latitude, sledrun.top_longitude)
+ r['Höhe oben'] = sledrun.top_elevation
+ r['Position unten'] = (sledrun.bottom_latitude, sledrun.bottom_longitude)
+ r['Höhe unten'] = sledrun.bottom_elevation
+ r['Länge'] = sledrun.length
+ r['Schwierigkeit'] = sledrun.difficulty
+ r['Lawinen'] = sledrun.avalanches
+ r['Betreiber'] = sledrun.operator
+ r['Öffentliche Anreise'] = sledrun.public_transport
+ r['Aufstieg möglich'] = sledrun.walkup_possible
+ r['Aufstieg getrennt'] = (sledrun.walkup_separate, sledrun.walkup_separate_comment)
+ r['Gehzeit'] = sledrun.walkup_time
+ r['Aufstiegshilfe'] = (sledrun.lift, sledrun.lift_details)
+ r['Beleuchtungsanlage'] = (sledrun.night_light, sledrun.night_light_comment)
+ r['Beleuchtungstage'] = (sledrun.night_light_days, sledrun.night_light_days_comment)
+ r['Rodelverleih'] = (sledrun.sled_rental, sledrun.sled_rental_comment)
+ r['Gütesiegel'] = sledrun.cachet
+ r['Webauskunft'] = sledrun.information_web
+ r['Telefonauskunft'] = sledrun.information_phone
+ r['Bild'] = sledrun.image
+ r['In Übersichtskarte'] = sledrun.show_in_overview
+ r['Forumid'] = sledrun.forum_id
+ return r
+
+
+class WinterrodelnTemplateDict(formencode.Validator):
+ """Private helper class for RodelbahnboxValidator or GasthausboxValidator"""
+ def __init__(self, template_title):
+ self.template_title = template_title
+
+ def to_python(self, value, state):
+ title, anonym_params, named_params = value
+ if title != self.template_title:
+ raise formencode.Invalid('Template title has to be "{}".'.format(self.template_title), value, state)
+ if len(anonym_params) > 0:
+ raise formencode.Invalid('No anonymous parameters are allowed in "{}".'.format(self.template_title), value, state)
+ return named_params
+
+ def from_python(self, value, state):
+ return self.template_title, [], value
+
+
+class RodelbahnboxValidator(wrpylib.wrvalidators.RodelbahnboxDictValidator):
+ def __init__(self):
+ wrpylib.wrvalidators.RodelbahnboxDictValidator.__init__(self)
+ self.pre_validators=[wrpylib.mwmarkup.TemplateValidator(as_table=True, as_table_keylen=20), WinterrodelnTemplateDict('Rodelbahnbox')]
+ self.chained_validators = [RodelbahnboxDictConverter()]