Added GasthausboxValidator.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrvalidators.py
index d1f2038e9bea4b766f0a6a2f297479d814a17d82..fc432d13af9905c2b00be9ecac2c496224d4f7b2 100644 (file)
@@ -17,10 +17,51 @@ import formencode.national
 
 class OrderedSchema(formencode.Schema):
     def _convert_to_python(self, value, state):
-        result = formencode.Schema._convert_to_python(self, value, state)
-        ordered_result = collections.OrderedDict()
-        for key in value.iterkeys():
-            ordered_result[key] = result[key]
+        pre_validators = self.pre_validators
+        chained_validators = self.chained_validators
+        for validator in pre_validators:
+            value = validator.to_python(value, state)
+        self.pre_validators = []
+        self.chained_validators = []
+        try:
+            result = formencode.Schema._convert_to_python(self, value, state)
+            ordered_result = collections.OrderedDict()
+            for key in value.iterkeys():
+                ordered_result[key] = result[key]
+            for validator in chained_validators:
+                ordered_result = validator.to_python(ordered_result, state)
+        finally:
+            self.pre_validators = pre_validators
+            self.chained_validators = chained_validators
+        return ordered_result
+
+    def _convert_from_python(self, value, state):
+        # store original pre- and chained validators
+        pre_validators = self.pre_validators
+        chained_validators = self.chained_validators[:]
+        # apply chained validators
+        chained = chained_validators[:]
+        chained.reverse()
+        for validator in chained:
+            value = validator.from_python(value, state)
+        # tempoarly remove pre- and chained validators
+        self.pre_validators = []
+        self.chained_validators = []
+        # apply original _convert_from_python method
+        try:
+            result = formencode.Schema._convert_from_python(self, value, state)
+            ordered_result = collections.OrderedDict()
+            for key in value.iterkeys():
+                ordered_result[key] = result[key]
+            # apply pre_validators
+            pre = pre_validators[:]
+            pre.reverse()
+            for validator in pre:
+                ordered_result = validator.from_python(ordered_result, state)
+        finally:
+            # resore original pre- and chained_validators
+            self.pre_validators = pre_validators
+            self.chained_validators = chained_validators
         return ordered_result
 
 
@@ -783,3 +824,21 @@ class RodelbahnboxDictValidator(OrderedSchema):
         self.add_field(u'Bild', UnicodeNone())
         self.add_field(u'In Übersichtskarte', GermanBoolNone())
         self.add_field(u'Forumid', UnsignedNeinNone())
+
+
+class GasthausboxDictValidator(OrderedSchema):
+    """Takes the fields of the Gasthausbox as dict of strings and returns them as dict of appropriet types."""
+    def __init__(self):
+        self.add_field(u'Position', GeoNone()) # '47.583333 N 15.75 E'
+        self.add_field(u'Höhe', UnsignedNone())
+        self.add_field(u'Betreiber', UnicodeNone())
+        self.add_field(u'Sitzplätze', UnsignedNone())
+        self.add_field(u'Übernachtung', BoolUnicodeTupleValidator())
+        self.add_field(u'Rauchfrei', GermanTristateTuple())
+        self.add_field(u'Rodelverleih', BoolUnicodeTupleValidator())
+        self.add_field(u'Handyempfang', ValueCommentListNeinLoopNone())
+        self.add_field(u'Homepage', UrlNeinNone())
+        self.add_field(u'E-Mail', EmailCommentListNeinLoopNone(allow_masked_email=True))
+        self.add_field(u'Telefon', PhoneCommentListNeinLoopNone(comments_are_optional=True))
+        self.add_field(u'Bild', UnicodeNone())
+        self.add_field(u'Rodelbahnen', WikiPageListLoopNone())