Added GasthausboxDictConverter.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 17 Mar 2014 20:15:43 +0000 (20:15 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 17 Mar 2014 20:15:43 +0000 (20:15 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@1932 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wrpylib/wrmwmarkup.py

index 127298ec183eda640438526573edc66b6edc2e4e..ed494640b88910dbdd953c307f2b552cd31afc51 100644 (file)
@@ -150,6 +150,54 @@ def sledrun_to_rodelbahnbox(sledrun, version=None):
     return RodelbahnboxValidator().from_python(sledrun)
 
 
+class GasthausboxDictConverter(formencode.Validator):
+    """Converts a dict with Gasthausbox properties to a Inn 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 inn, this inn class will be populated or updated."""
+        props = value
+        if isinstance(state, object) and hasattr(state, 'inn'):
+            sledrun = state.sledrun
+        else:
+            class Inn(object):
+                pass
+            inn = Inn()
+        for k, v in props.iteritems():
+            if   k == u'Position': inn.position_latitude, inn.position_longitude = v
+            elif k == u'Höhe': inn.position_elevation = v
+            elif k == u'Betreiber': inn.operator = v
+            elif k == u'Sitzplätze': inn.seats = v
+            elif k == u'Übernachtung': inn.overnight, inn.overnight_comment = v
+            elif k == u'Rauchfrei': inn.nonsmoker_area, inn.smoker_area = v
+            elif k == u'Rodelverleih': inn.sled_rental, inn.sled_rental_comment = v
+            elif k == u'Handyempfang': inn.mobile_provider = v
+            elif k == u'Homepage': inn.homepage = v
+            elif k == u'E-Mail': inn.email_list = v
+            elif k == u'Telefon': inn.phone_list = v
+            elif k == u'Bild': inn.image = v
+            elif k == u'Rodelbahnen': inn.sledding_list = v
+        return inn
+
+    def from_python(self, value, state=None):
+        """Converts an inn class to a dict of Gasthausbox properties. value is an Inn instance."""
+        inn = value
+        r = collections.OrderedDict()
+        r[u'Position'] = (inn.position_latitude, inn.position_longitude)
+        r[u'Höhe'] = inn.position_elevation
+        r[u'Betreiber'] = inn.operator
+        r[u'Sitzplätze'] = inn.seats
+        r[u'Übernachtung'] = (inn.overnight, inn.overnight_comment)
+        r[u'Rauchfrei'] = (inn.nonsmoker_area, inn.smoker_area)
+        r[u'Rodelverleih'] = (inn.sled_rental, inn.sled_rental_comment)
+        r[u'Handyempfang'] = inn.mobile_provider
+        r[u'Homepage'] = inn.homepage
+        r[u'E-Mail'] = inn.email_list
+        r[u'Telefon'] = inn.phone_list
+        r[u'Bild'] = inn.image
+        r[u'Rodelbahnen'] = inn.sledding_list
+        return r
+
+
 def gasthausbox_to_inn(wikitext, inn=None):
     """Converts a inn wiki page containing a {{Gasthausbox}} to an inn.
     raises a formencode.Invalid exception if an error occurs.