Code

Ticket #2535: manipulator_validators.diff

File manipulator_validators.diff, 1.5 KB (added by Tom Tobin <korpios@…>, 8 years ago)

Implements manipulator-aware validators in post-MR.

Line 
1Index: /Users/korpios/checkouts/django/trunk/django/db/models/fields/__init__.py
2===================================================================
3--- /Users/korpios/checkouts/django/trunk/django/db/models/fields/__init__.py   (revision 3571)
4+++ /Users/korpios/checkouts/django/trunk/django/db/models/fields/__init__.py   (working copy)
5@@ -7,7 +7,7 @@
6 from django.utils.functional import curry
7 from django.utils.text import capfirst
8 from django.utils.translation import gettext, gettext_lazy
9-import datetime, os, time
10+import datetime, os, time, inspect
11 
12 class NOT_PROVIDED:
13     pass
14@@ -225,6 +225,18 @@
15         """
16         field_objs, params = self.prepare_field_objs_and_params(manipulator, name_prefix)
17 
18+        # Curry validators that require the manipulator passed in.
19+        for i, validator in enumerate(params['validator_list']):
20+            assert callable(validator), "Validator not callable: %r" % validator
21+            if inspect.isfunction(validator):
22+                wrap = len(inspect.getargspec(validator)[0]) == 3
23+            elif inspect.ismethod(validator):
24+                wrap = len(inspect.getargspec(validator)[0]) == 4
25+            else:
26+                wrap = len(inspect.getargspec(validator.__call__)[0]) == 4
27+            if wrap:               
28+                params['validator_list'][i] = curry(validator, manipulator)
29+
30         # Add the "unique" validator(s).
31         for field_name_list in opts.unique_together:
32             if field_name_list[0] == self.name: