1. Andriy Kornatskyy
  2. wheezy.validation

Commits

Andriy Kornatskyy  committed 676641a

Added ability to update model with list like attribute (binding several values to a single attribute)

  • Participants
  • Parent commits 7b653c7
  • Branches default

Comments (0)

Files changed (4)

File i18n/en/LC_MESSAGES/validation.po

View file
 msgstr "%Y/%m/%d %H:%M"
 
 #. fallback datetime input formats: 2008/5/18 16:34:52. Use | to separate.
-#: src/wheezy/validation/format.py:22 src/wheezy/validation/format.py:26
-#: src/wheezy/validation/format.py:27
+#: src/wheezy/validation/format.py:22 src/wheezy/validation/format.py:27
 msgid ""
 "%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"
 "%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:109
+#: src/wheezy/validation/model.py:109 src/wheezy/validation/model.py:153
 #, python-format
 msgid "The value '%s' is invalid."
 msgstr "The value '%s' is invalid."
 
-#: src/wheezy/validation/model.py:112
+#: src/wheezy/validation/model.py:112 src/wheezy/validation/model.py:156
 msgid "Input was not in a correct format."
 msgstr "Input was not in a correct format."
 
-#: src/wheezy/validation/model.py:113 src/wheezy/validation/model.py:117
+#: src/wheezy/validation/model.py:130
+msgid "Multiple input was not in a correct format."
+msgstr "Multiple input was not in a correct format."
+
+#: src/wheezy/validation/model.py:145
 #, 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 "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
-#: src/wheezy/validation/rules.py:232 src/wheezy/validation/rules.py:235
+#: src/wheezy/validation/rules.py:229 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:298
+#: src/wheezy/validation/rules.py:290 src/wheezy/validation/rules.py:298
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:330
+#: src/wheezy/validation/rules.py:320 src/wheezy/validation/rules.py:330
 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:362
+#: src/wheezy/validation/rules.py:350 src/wheezy/validation/rules.py:362
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
-#: src/wheezy/validation/rules.py:410
+#: src/wheezy/validation/rules.py:396 src/wheezy/validation/rules.py:410
 #, 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:414
+#: src/wheezy/validation/rules.py:400 src/wheezy/validation/rules.py:414
 #, 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:421
+#: src/wheezy/validation/rules.py:407 src/wheezy/validation/rules.py:421
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."

File i18n/ru/LC_MESSAGES/validation.po

View file
 msgid "Input was not in a correct format."
 msgstr "Ввод не в правильном формате."
 
+#: src/wheezy/validation/model.py:130
+msgid "Multiple input was not in a correct format."
+msgstr "Ввод с несколькими значениями был не в правильном формате."
+
 #: src/wheezy/validation/rules.py:12
 msgid "Required field cannot be left blank."
 msgstr "Обязательное поле не может быть пустым."

File i18n/validation.po

View file
 msgstr "%Y/%m/%d %H:%M"
 
 #. fallback datetime input formats: 2008/5/18 16:34:52. Use | to separate.
-#: src/wheezy/validation/format.py:22 src/wheezy/validation/format.py:26
-#: src/wheezy/validation/format.py:27
+#: src/wheezy/validation/format.py:22 src/wheezy/validation/format.py:27
 msgid ""
 "%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"
 "%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:109
+#: src/wheezy/validation/model.py:109 src/wheezy/validation/model.py:153
 #, python-format
 msgid "The value '%s' is invalid."
 msgstr "The value '%s' is invalid."
 
-#: src/wheezy/validation/model.py:112
+#: src/wheezy/validation/model.py:112 src/wheezy/validation/model.py:156
 msgid "Input was not in a correct format."
 msgstr "Input was not in a correct format."
 
-#: src/wheezy/validation/model.py:113 src/wheezy/validation/model.py:117
+#: src/wheezy/validation/model.py:130
+msgid "Multiple input was not in a correct format."
+msgstr "Multiple input was not in a correct format."
+
+#: src/wheezy/validation/model.py:145
 #, 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 "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
-#: src/wheezy/validation/rules.py:232 src/wheezy/validation/rules.py:235
+#: src/wheezy/validation/rules.py:229 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:298
+#: src/wheezy/validation/rules.py:290 src/wheezy/validation/rules.py:298
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:330
+#: src/wheezy/validation/rules.py:320 src/wheezy/validation/rules.py:330
 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:362
+#: src/wheezy/validation/rules.py:350 src/wheezy/validation/rules.py:362
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
-#: src/wheezy/validation/rules.py:410
+#: src/wheezy/validation/rules.py:396 src/wheezy/validation/rules.py:410
 #, 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:414
+#: src/wheezy/validation/rules.py:400 src/wheezy/validation/rules.py:414
 #, 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:421
+#: src/wheezy/validation/rules.py:407 src/wheezy/validation/rules.py:421
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."

File src/wheezy/validation/model.py

View file
         ...         self.lunch_time = time.min
         ...         self.last_visit = datetime.min
         ...         self.accepted_policy = False
+        ...         self.prefs = []
+        ...         self.prefs2 = [0]
         >>> user = User()
         >>> values = {'name': 'abc', 'balance': ['0.1'],
         ...     'age': ['33'], 'birthday': ['1978/4/9'],
         ...     'lunch_time': ['13:05'], 'last_visit': ['2012/2/4 16:14:52'],
-        ...     'accepted_policy': ['1']}
+        ...     'accepted_policy': ['1'], 'prefs': ['1', '2'],
+        ...     'prefs2': ['1', '2']}
         >>> results = {}
         >>> try_update_model(user, values, results)
         True
         datetime.datetime(2012, 2, 4, 16, 14, 52)
         >>> user.accepted_policy
         True
+        >>> user.prefs
+        ['1', '2']
+        >>> user.prefs2
+        [1, 2]
 
         ``model`` can be dict.
 
 
         Invalid values:
 
-        >>> values = {'balance': ['x'], 'age': [''], 'birthday': ['4.2.12']}
+        >>> values = {'balance': ['x'], 'age': [''], 'birthday': ['4.2.12'],
+        ...         'prefs2': ['1', 'x']}
         >>> user = User()
         >>> try_update_model(user, values, results)
         False
         1
         >>> user.age
         0
+        >>> user.prefs2
+        [0]
     """
     if translations is None:
         translations = null_translations
         setter = setattr
     succeed = True
     for name in attribute_names:
+        try:
+            value = values[name]
+        except KeyError:
+            continue
         attr = getter(model, name)
-        provider_name = type(attr).__name__
-        try:
-            value_provider = value_providers[provider_name]
-            value = values[name]
+        # Check if we have a deal with list like attribute
+        if hasattr(attr, '__setitem__'):
+            # Guess type of list by checking the first item,
+            # fallback to str provider that leaves value unchanged.
+            if attr:
+                provider_name = type(attr[0]).__name__
+                try:
+                    value_provider = value_providers[provider_name]
+                except KeyError:  # pragma: nocover
+                    continue
+            else:
+                value_provider = value_providers['str']
+            items = []
+            try:
+                for item in value:
+                    items.append(value_provider(item, gettext))
+                attr[:] = items
+            except (ArithmeticError, ValueError):
+                results[name] = [gettext(
+                        "Multiple input was not in a correct format.")]
+                succeed = False
+        else:  # A simple value attribute
+            provider_name = type(attr).__name__
+            try:
+                value_provider = value_providers[provider_name]
+            except KeyError:  # pragma: nocover
+                continue
             try:
                 if isinstance(value, list):
                     value = value and value[-1] or ''
                 original_value = value
                 value = value_provider(value, gettext)
+                if value is None:
+                    results[name] = [gettext(
+                        "The value '%s' is not in one of supported formats."
+                        % original_value)]
+                    succeed = False
+                else:
+                    setter(model, name, value)
             except (ArithmeticError, ValueError):
                 if original_value:
                     results[name] = [gettext(
                     results[name] = [gettext(
                         "Input was not in a correct format.")]
                 succeed = False
-            else:
-                if value is None:
-                    results[name] = [gettext(
-                        "The value '%s' is not in one of supported formats."
-                        % original_value)]
-                    succeed = False
-                else:
-                    setter(model, name, value)
-        except KeyError:
-            pass
     return succeed