Andriy Kornatskyy avatar Andriy Kornatskyy committed 9538608

Added not_equal strategy for CompareRule.

Comments (0)

Files changed (5)

doc/userguide.rst

   defined by this rule. Supported range attributes include: ``min``, ``max``.
   See :py:class:`~wheezy.validation.rules.LengthRule`.
 * ``compare``. Compares attribute being validated with some other attribute
-  value. Supported comparison operations include: ``equal``. See
-  :py:class:`~wheezy.validation.rules.CompareRule`.
+  value. Supported comparison operations include: ``equal``,
+  ``not_equal``. See :py:class:`~wheezy.validation.rules.CompareRule`.
 * ``regex``. Search for regular expression pattern. Initialized with
   ``regex`` as a regular expression pattern or a pre-compiled regular
   expression. See :py:class:`~wheezy.validation.rules.RegexRule`.

i18n/en/LC_MESSAGES/validation.po

 "Project-Id-Version: wheezy.validation\n"
 "Report-Msgid-Bugs-To: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "POT-Creation-Date: 2011-11-02 14:13+0200\n"
-"PO-Revision-Date: 2012-02-04 18:52+0200\n"
+"PO-Revision-Date: 2012-04-29 10:47+0200\n"
 "Last-Translator: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "Language: US English\n"
 "MIME-Version: 1.0\n"
 "%Y/%m/%d %H:%M:%S|%m/%d/%Y %H:%M|%m/%d/%Y %H:%M:%S|%Y-%m-%d %H:%M|%Y-%m-%d "
 "%H:%M:%S|%m/%d/%y %H:%M|%m/%d/%y %H:%M:%S"
 
-#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:132
-#: src/wheezy/validation/model.py:135
+#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:135
 msgid "Multiple input was not in a correct format."
 msgstr "Multiple input was not in a correct format."
 
-#: src/wheezy/validation/model.py:146 src/wheezy/validation/model.py:147
-#: src/wheezy/validation/model.py:150
+#: src/wheezy/validation/model.py:146 src/wheezy/validation/model.py:150
 #, python-format
 msgid "The value '%s' is not in one of supported formats."
 msgstr "The value '%s' is not in one of supported formats."
 msgid "Required field cannot be left blank."
 msgstr "Required field cannot be left blank."
 
-#: src/wheezy/validation/rules.py:96 src/wheezy/validation/rules.py:98
+#: src/wheezy/validation/rules.py:95 src/wheezy/validation/rules.py:98
 #, 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:101 src/wheezy/validation/rules.py:103
+#: src/wheezy/validation/rules.py:100 src/wheezy/validation/rules.py:103
 #, 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:108 src/wheezy/validation/rules.py:110
+#: src/wheezy/validation/rules.py:107 src/wheezy/validation/rules.py:110
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
-#: src/wheezy/validation/rules.py:230 src/wheezy/validation/rules.py:235
+#: src/wheezy/validation/rules.py:232 src/wheezy/validation/rules.py:235
 #, python-format
 msgid "The value failed equality comparison with \"%(comparand)s\"."
 msgstr "The value failed equality comparison with \"%(comparand)s\"."
 
-#: src/wheezy/validation/rules.py:291 src/wheezy/validation/rules.py:298
+#: src/wheezy/validation/rules.py:238
+#, python-format
+msgid "The value failed not equal comparison with \"%(comparand)s\"."
+msgstr "The value failed not equal comparison with \"%(comparand)s\"."
+
+#: src/wheezy/validation/rules.py:324
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:321 src/wheezy/validation/rules.py:330
+#: src/wheezy/validation/rules.py:354
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 
-#: src/wheezy/validation/rules.py:351 src/wheezy/validation/rules.py:362
+#: src/wheezy/validation/rules.py:384
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
-#: src/wheezy/validation/rules.py:397 src/wheezy/validation/rules.py:410
+#: src/wheezy/validation/rules.py:430
 #, python-format
 msgid "Required to be greater or equal to %(min)s."
 msgstr "Required to be greater or equal to %(min)s."
 
-#: src/wheezy/validation/rules.py:401 src/wheezy/validation/rules.py:414
+#: src/wheezy/validation/rules.py:434
 #, python-format
 msgid "The value must fall within the range %(min)s - %(max)s"
 msgstr "The value must fall within the range %(min)s - %(max)s"
 
-#: src/wheezy/validation/rules.py:408 src/wheezy/validation/rules.py:421
+#: src/wheezy/validation/rules.py:441
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."
 
-#: src/wheezy/validation/rules.py:624 src/wheezy/validation/rules.py:625
+#: src/wheezy/validation/rules.py:655 src/wheezy/validation/rules.py:688
+msgid "Required to be above a minimum allowed."
+msgstr "Required to be above a minimum allowed."
+
+#: src/wheezy/validation/rules.py:658
 msgid "The value does not belong to the list of known items."
 msgstr "The value does not belong to the list of known items."
 
-#: src/wheezy/validation/rules.py:655
-msgid "Required to be above a minimum allowed."
-msgstr "Required to be above a minimum allowed."
-
-#: src/wheezy/validation/rules.py:659
+#: src/wheezy/validation/rules.py:659 src/wheezy/validation/rules.py:692
 msgid "Must fall within a valid range."
 msgstr "Must fall within a valid range."
 
-#: src/wheezy/validation/rules.py:665
+#: src/wheezy/validation/rules.py:665 src/wheezy/validation/rules.py:698
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

i18n/ru/LC_MESSAGES/validation.po

 "Project-Id-Version: wheezy.validation\n"
 "Report-Msgid-Bugs-To: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "POT-Creation-Date: 2011-11-02 14:13+0200\n"
-"PO-Revision-Date: 2012-02-04 18:52+0200\n"
+"PO-Revision-Date: 2012-04-29 10:49+0200\n"
 "Last-Translator: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "Language: Russian\n"
 "MIME-Version: 1.0\n"
 msgid "The value failed equality comparison with \"%(comparand)s\"."
 msgstr "Значение не равно в сравнении с \"%(comparand)s\"."
 
+#: src/wheezy/validation/rules.py:238
+#, python-format
+msgid "The value failed not equal comparison with \"%(comparand)s\"."
+msgstr "Значение равно в сравнении с \"%(comparand)s\"."
+
 #: src/wheezy/validation/rules.py:290
 msgid "Required to match validation pattern."
 msgstr "Обязательно должно соответствовать проверяемому шаблону."

i18n/validation.po

 "Project-Id-Version: wheezy.validation\n"
 "Report-Msgid-Bugs-To: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "POT-Creation-Date: 2011-11-02 14:13+0200\n"
-"PO-Revision-Date: 2012-02-04 18:52+0200\n"
+"PO-Revision-Date: 2012-04-29 10:47+0200\n"
 "Last-Translator: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "Language: US English\n"
 "MIME-Version: 1.0\n"
 "%Y/%m/%d %H:%M:%S|%m/%d/%Y %H:%M|%m/%d/%Y %H:%M:%S|%Y-%m-%d %H:%M|%Y-%m-%d "
 "%H:%M:%S|%m/%d/%y %H:%M|%m/%d/%y %H:%M:%S"
 
-#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:132
-#: src/wheezy/validation/model.py:135
+#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:135
 msgid "Multiple input was not in a correct format."
 msgstr "Multiple input was not in a correct format."
 
-#: src/wheezy/validation/model.py:146 src/wheezy/validation/model.py:147
-#: src/wheezy/validation/model.py:150
+#: src/wheezy/validation/model.py:146 src/wheezy/validation/model.py:150
 #, python-format
 msgid "The value '%s' is not in one of supported formats."
 msgstr "The value '%s' is not in one of supported formats."
 msgid "Required field cannot be left blank."
 msgstr "Required field cannot be left blank."
 
-#: src/wheezy/validation/rules.py:96 src/wheezy/validation/rules.py:98
+#: src/wheezy/validation/rules.py:95 src/wheezy/validation/rules.py:98
 #, 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:101 src/wheezy/validation/rules.py:103
+#: src/wheezy/validation/rules.py:100 src/wheezy/validation/rules.py:103
 #, 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:108 src/wheezy/validation/rules.py:110
+#: src/wheezy/validation/rules.py:107 src/wheezy/validation/rules.py:110
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
-#: src/wheezy/validation/rules.py:230 src/wheezy/validation/rules.py:235
+#: src/wheezy/validation/rules.py:232 src/wheezy/validation/rules.py:235
 #, python-format
 msgid "The value failed equality comparison with \"%(comparand)s\"."
 msgstr "The value failed equality comparison with \"%(comparand)s\"."
 
-#: src/wheezy/validation/rules.py:291 src/wheezy/validation/rules.py:298
+#: src/wheezy/validation/rules.py:238
+#, python-format
+msgid "The value failed not equal comparison with \"%(comparand)s\"."
+msgstr "The value failed not equal comparison with \"%(comparand)s\"."
+
+#: src/wheezy/validation/rules.py:324
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:321 src/wheezy/validation/rules.py:330
+#: src/wheezy/validation/rules.py:354
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 
-#: src/wheezy/validation/rules.py:351 src/wheezy/validation/rules.py:362
+#: src/wheezy/validation/rules.py:384
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
-#: src/wheezy/validation/rules.py:397 src/wheezy/validation/rules.py:410
+#: src/wheezy/validation/rules.py:430
 #, python-format
 msgid "Required to be greater or equal to %(min)s."
 msgstr "Required to be greater or equal to %(min)s."
 
-#: src/wheezy/validation/rules.py:401 src/wheezy/validation/rules.py:414
+#: src/wheezy/validation/rules.py:434
 #, python-format
 msgid "The value must fall within the range %(min)s - %(max)s"
 msgstr "The value must fall within the range %(min)s - %(max)s"
 
-#: src/wheezy/validation/rules.py:408 src/wheezy/validation/rules.py:421
+#: src/wheezy/validation/rules.py:441
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."
 
-#: src/wheezy/validation/rules.py:624 src/wheezy/validation/rules.py:625
+#: src/wheezy/validation/rules.py:655 src/wheezy/validation/rules.py:688
+msgid "Required to be above a minimum allowed."
+msgstr "Required to be above a minimum allowed."
+
+#: src/wheezy/validation/rules.py:658
 msgid "The value does not belong to the list of known items."
 msgstr "The value does not belong to the list of known items."
 
-#: src/wheezy/validation/rules.py:655
-msgid "Required to be above a minimum allowed."
-msgstr "Required to be above a minimum allowed."
-
-#: src/wheezy/validation/rules.py:659
+#: src/wheezy/validation/rules.py:659 src/wheezy/validation/rules.py:692
 msgid "Must fall within a valid range."
 msgstr "Must fall within a valid range."
 
-#: src/wheezy/validation/rules.py:665
+#: src/wheezy/validation/rules.py:665 src/wheezy/validation/rules.py:698
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

src/wheezy/validation/rules.py

 from datetime import date
 from datetime import datetime
 from datetime import time
-from datetime import timedelta
 
 from wheezy.validation.comp import ref_getter
 from wheezy.validation.comp import regex_pattern
     """ Compares attribute being validated with some other attribute value.
     """
 
-    def __init__(self, equal=None, message_template=None):
+    def __init__(self, equal=None, not_equal=None, message_template=None):
         """
             Initialization selects the most appropriate validation
             strategy.
 
             >>> r = CompareRule(equal='confirm_password')
             >>> assert r.check == r.check_equal
+
+            >>> r = CompareRule(not_equal='other')
+            >>> assert r.check == r.check_not_equal
         """
         if equal:
             self.comparand = equal
             self.message_template = message_template or _(
                     'The value failed equality comparison'
                     ' with "%(comparand)s".')
+        elif not_equal:
+            self.comparand = not_equal
+            self.check = self.check_not_equal
+            self.message_template = message_template or _(
+                    'The value failed not equal comparison'
+                    ' with "%(comparand)s".')
 
     def check(self, value, name, model, result, gettext):
         return True
             return False
         return True
 
+    def check_not_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
             >>> result = []
             >>> r.validate('z', None, model, result, _)
             False
+
+            ``check_not_equal`` strategy succeed.
+
+            >>> result = []
+            >>> r = CompareRule(not_equal='previous_password')
+            >>> model = {
+            ...         'previous_password': 'x'
+            ... }
+            >>> r.validate('y', None, model, result, _)
+            True
+
+            ``check_not_equal`` strategy fails.
+
+            >>> result = []
+            >>> r.validate('x', None, model, result, _)
+            False
         """
         return self.check(value, None, model, result, gettext)
 
 
         Min range strategy
 
+        >>> from datetime import timedelta
         >>> result = []
         >>> r = relative_date(min=timedelta(days=-7))
         >>> r.validate(date.today(), None, None, result, _)
 
         Min range strategy
 
+        >>> from datetime import timedelta
         >>> result = []
         >>> r = relative_datetime(min=timedelta(days=-7))
         >>> r.validate(datetime.today(), None, None, result, _)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.