X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fforms.py;h=1ff2bd0088c765711a26c176f652337409bc4d56;hb=56e2243f3be7e859666ce0e4e1a8b8b94444f8d4;hp=2c137e3c027618e12ff223755272f4451813689e;hpb=73960c4331e70da50b65ac753558578ada03d9ce;p=patchwork diff --git a/apps/patchwork/forms.py b/apps/patchwork/forms.py index 2c137e3..1ff2bd0 100644 --- a/apps/patchwork/forms.py +++ b/apps/patchwork/forms.py @@ -45,9 +45,9 @@ class RegistrationForm(RegistrationFormUniqueEmail): user.last_name = self.cleaned_data.get('last_name', '') user.save() - # saving the userprofile causes the firstname/lastname to propagate - # to the person objects. - user.get_profile().save() + # saving the userprofile causes the firstname/lastname to propagate + # to the person objects. + user.get_profile().save() return user @@ -176,38 +176,41 @@ class MultipleBooleanField(forms.ChoiceField): def is_no_change(self, value): return value == self.no_change_choice[0] -class MultiplePatchForm(PatchForm): + # 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) + +class MultiplePatchForm(forms.Form): + action = 'update' state = OptionalModelChoiceField(queryset = State.objects.all()) archived = MultipleBooleanField() def __init__(self, project, *args, **kwargs): - super(MultiplePatchForm, self).__init__(project = project, - *args, **kwargs) + super(MultiplePatchForm, self).__init__(*args, **kwargs) self.fields['delegate'] = OptionalDelegateField(project = project, required = False) - def _clean_fields(self): - super(MultiplePatchForm, self)._clean_fields() - # remove optional fields - opts = self.instance._meta - for f in opts.fields: - if not f.name in self.cleaned_data: - continue - - field = self.fields.get(f.name, None) - if field is None: - continue - - if field.is_no_change(self.cleaned_data[f.name]): - del self.cleaned_data[f.name] - def save(self, instance, commit = True): opts = instance.__class__._meta if self.errors: raise ValueError("The %s could not be changed because the data " "didn't validate." % opts.object_name) data = self.cleaned_data - # remove 'no change fields' from the data + # Update the instance for f in opts.fields: if not f.name in data: continue @@ -217,10 +220,13 @@ class MultiplePatchForm(PatchForm): continue if field.is_no_change(data[f.name]): - del data[f.name] + continue + + setattr(instance, f.name, data[f.name]) - return forms.save_instance(self, instance, - self._meta.fields, 'changed', commit) + if commit: + instance.save() + return instance class UserPersonLinkForm(forms.Form): email = forms.EmailField(max_length = 200)