diff options
author | Stephen Finucane <stephen@that.guru> | 2016-11-19 14:02:31 +0000 |
---|---|---|
committer | Stephen Finucane <stephen@that.guru> | 2016-12-18 22:42:27 +0000 |
commit | 0abde97aa397d6b061ee358f2cdd353f7602fc5b (patch) | |
tree | 6b52c6ff8eed57592b8ff4da3ff3d18976317c0a /patchwork/forms.py | |
parent | 513b13677e44c6e6cdb58dab0282ca56db6c0311 (diff) | |
download | patchwork-0abde97aa397d6b061ee358f2cdd353f7602fc5b.tar patchwork-0abde97aa397d6b061ee358f2cdd353f7602fc5b.tar.gz |
forms: Use TypedChoiceField
This resolves a TODO.
Signed-off-by: Stephen Finucane <stephen@that.guru>
Diffstat (limited to 'patchwork/forms.py')
-rw-r--r-- | patchwork/forms.py | 33 |
1 files changed, 6 insertions, 27 deletions
diff --git a/patchwork/forms.py b/patchwork/forms.py index b14094d..6c61616 100644 --- a/patchwork/forms.py +++ b/patchwork/forms.py @@ -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) |