Implemented the from_python part of OrderedSchema. Now test_OrderedSchema passes.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 10 Mar 2014 20:56:34 +0000 (20:56 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Mon, 10 Mar 2014 20:56:34 +0000 (20:56 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@1922 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wrpylib/wrvalidators.py

index 820c0f2f96d28909c187bc5c9a132b385e4a46df..df749f822ea672147a6af3af98f245d291e7a8c9 100644 (file)
@@ -35,6 +35,35 @@ class OrderedSchema(formencode.Schema):
             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
+
 
 class NoneValidator(formencode.FancyValidator):
     """Takes a validator and makes it possible that empty strings are mapped to None."""