Commits

Andriy Kornatskyy committed 4f54570

Added compare rule

Comments (0)

Files changed (6)

i18n/en/LC_MESSAGES/validation.po

 msgid "The value '%s' is invalid."
 msgstr "The value '%s' is invalid."
 
-#: src/wheezy/validation/rules.py:12
+#: src/wheezy/validation/rules.py:12 src/wheezy/validation/rules.py:15
 msgid "Required field cannot be left blank."
 msgstr "Required field cannot be left blank."
 
-#: src/wheezy/validation/rules.py:79
+#: src/wheezy/validation/rules.py:79 src/wheezy/validation/rules.py:82
 #, python-format
 msgid "Required to be a minimum of %(min)d characters in length."
 msgstr "Required to be a minimum of %(min)d characters in length."
 
-#: src/wheezy/validation/rules.py:84
+#: src/wheezy/validation/rules.py:84 src/wheezy/validation/rules.py:87
 #, python-format
 msgid "The length must fall within the range %(min)d - %(max)d characters."
 msgstr "The length must fall within the range %(min)d - %(max)d characters."
 
-#: src/wheezy/validation/rules.py:91
+#: src/wheezy/validation/rules.py:91 src/wheezy/validation/rules.py:94
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
+
+#: src/wheezy/validation/rules.py:214 src/wheezy/validation/rules.py:217
+#, python-format
+msgid "The value failed equality comparison with \"%(comparand)s\"."
+msgstr "The value failed equality comparison with \"%(comparand)s\"."

i18n/ru/LC_MESSAGES/validation.po

 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Превышена максимально допустимая длина %(max)d."
+
+#: src/wheezy/validation/rules.py:214 src/wheezy/validation/rules.py:217
+#, python-format
+msgid "The value failed equality comparison with \"%(comparand)s\"."
+msgstr "Значение не равно в сравнении с \"%(comparand)s\"."

i18n/validation.po

 msgid "The value '%s' is invalid."
 msgstr "The value '%s' is invalid."
 
-#: src/wheezy/validation/rules.py:12
+#: src/wheezy/validation/rules.py:12 src/wheezy/validation/rules.py:15
 msgid "Required field cannot be left blank."
 msgstr "Required field cannot be left blank."
 
-#: src/wheezy/validation/rules.py:79
+#: src/wheezy/validation/rules.py:79 src/wheezy/validation/rules.py:82
 #, python-format
 msgid "Required to be a minimum of %(min)d characters in length."
 msgstr "Required to be a minimum of %(min)d characters in length."
 
-#: src/wheezy/validation/rules.py:84
+#: src/wheezy/validation/rules.py:84 src/wheezy/validation/rules.py:87
 #, python-format
 msgid "The length must fall within the range %(min)d - %(max)d characters."
 msgstr "The length must fall within the range %(min)d - %(max)d characters."
 
-#: src/wheezy/validation/rules.py:91
+#: src/wheezy/validation/rules.py:91 src/wheezy/validation/rules.py:94
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
+
+#: src/wheezy/validation/rules.py:214 src/wheezy/validation/rules.py:217
+#, python-format
+msgid "The value failed equality comparison with \"%(comparand)s\"."
+msgstr "The value failed equality comparison with \"%(comparand)s\"."

src/wheezy/validation/comp.py

     ref_gettext = lambda t: t.gettext
 else:  # pragma: nocover
     ref_gettext = lambda t: t.ugettext
+
+
+def ref_getter(model):
+    # if model is a dict
+    if hasattr(model, '__iter__'):
+        return type(model).__getitem__
+    else:
+        return getattr

src/wheezy/validation/rules.py

 """ ``rules`` module.
 """
 
+from wheezy.validation.comp import ref_getter
+
+
 _ = lambda s: s
 
 
 
 
 length = LengthRule
+
+
+class CompareRule(object):
+
+    def __init__(self, equal=None, message_template=None):
+        """
+            Initialization selects the most appropriate validation
+            strategy.
+
+            >>> r = CompareRule(equal='confirm_password')
+            >>> assert r.check == r.check_equal
+        """
+        if equal:
+            self.comparand = equal
+            self.check = self.check_equal
+            self.message_template = message_template or _(
+                    'The value failed equality comparison'
+                    ' with "%(comparand)s".')
+
+    def check(self, value, name, model, result, gettext):
+        return True
+
+    def check_equal(self, value, name, model, result, gettext):
+        getter = ref_getter(model)
+        comparand_value = getter(model, self.comparand)
+        if value != comparand_value:
+            result.append(gettext(self.message_template)
+                    % {'comparand': self.comparand})
+            return False
+        return True
+
+    def validate(self, value, name, model, result, gettext):
+        """
+            No any comparer selected
+
+            >>> r = CompareRule()
+            >>> r.validate('abc', None, None, None, _)
+            True
+
+            ``check_equal`` strategy succeed.
+
+            >>> result = []
+            >>> r = CompareRule(equal='confirm_password')
+            >>> model = {
+            ...         'confirm_password': 'x'
+            ... }
+            >>> r.validate('x', None, model, result, _)
+            True
+
+            ``check_equal`` strategy fails.
+
+            >>> result = []
+            >>> r.validate('z', None, model, result, _)
+            False
+        """
+        return self.check(value, None, model, result, gettext)
+
+compare = CompareRule

src/wheezy/validation/validator.py

 from wheezy.validation.comp import iterkeys
 from wheezy.validation.comp import null_translations
 from wheezy.validation.comp import ref_gettext
+from wheezy.validation.comp import ref_getter
 
 
 class Validator(object):
             translations = null_translations
         gettext = ref_gettext(translations)
         succeed = True
-        # if model is a dict
-        if hasattr(model, '__iter__'):
-            getter = type(model).__getitem__
-        else:
-            getter = getattr
+        getter = ref_getter(model)
         for (name, rules) in iteritems(self.mapping):
             value = getter(model, name)
             result = []