]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
forms: Use TypedChoiceField
authorStephen Finucane <stephen@that.guru>
Sat, 19 Nov 2016 14:02:31 +0000 (14:02 +0000)
committerStephen Finucane <stephen@that.guru>
Sun, 18 Dec 2016 22:42:27 +0000 (22:42 +0000)
This resolves a TODO.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/forms.py

index b14094d469b8b5cca6464ec6ddae46bdeba6140c..6c61616a8eab30e8d55ed68f4c950178af3b90f6 100644 (file)
@@ -163,40 +163,19 @@ class OptionalModelChoiceField(forms.ModelChoiceField):
         return super(OptionalModelChoiceField, self).clean(value)
 
 
-class MultipleBooleanField(forms.ChoiceField):
-    no_change_choice = ('*', 'no change')
-
-    def __init__(self, *args, **kwargs):
-        super(MultipleBooleanField, self).__init__(*args, **kwargs)
-        self.choices = [self.no_change_choice] + \
-            [(True, 'Archived'), (False, 'Unarchived')]
+class OptionalBooleanField(forms.TypedChoiceField):
 
     def is_no_change(self, value):
-        return value == self.no_change_choice[0]
-
-    # TODO: Check whether it'd be worth to use a TypedChoiceField here; I
-    # think that'd allow us to get rid of the custom valid_value() and
-    # to_python() methods.
-    def valid_value(self, value):
-        if value in [v1 for (v1, v2) in self.choices]:
-            return True
-        return False
-
-    def to_python(self, value):
-        if value is None or self.is_no_change(value):
-            return self.no_change_choice[0]
-        elif value == 'True':
-            return True
-        elif value == 'False':
-            return False
-        else:
-            raise ValueError('Unknown value: %s' % value)
+        return value == self.empty_value
 
 
 class MultiplePatchForm(forms.Form):
     action = 'update'
     state = OptionalModelChoiceField(queryset=State.objects.all())
-    archived = MultipleBooleanField()
+    archived = OptionalBooleanField(
+        choices=[('*', 'no change'), (True, 'Archived'),
+                 (False, 'Unarchived')],
+        coerce=bool, empty_value='*')
 
     def __init__(self, project, *args, **kwargs):
         super(MultiplePatchForm, self).__init__(*args, **kwargs)