X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fforms.py;h=dc7a46443fa4179171044d1bb8ad6aa29824cc8a;hb=8639b526494afbe41d27c60e69ce26513c7d3c37;hp=c02d11ef98a7e09d6aca3a83f160f0fa70eba3be;hpb=05d26756d7e7f254d5631a0649aeac5fd3a58ca8;p=patchwork diff --git a/apps/patchwork/forms.py b/apps/patchwork/forms.py index c02d11e..dc7a464 100644 --- a/apps/patchwork/forms.py +++ b/apps/patchwork/forms.py @@ -44,6 +44,11 @@ class RegistrationForm(RegistrationFormUniqueEmail): user.first_name = self.cleaned_data.get('first_name', '') 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() + return user def clean(self): @@ -75,6 +80,11 @@ class CreateBundleForm(forms.ModelForm): % name) return name +class DeleteBundleForm(forms.Form): + name = 'deletebundleform' + form_name = forms.CharField(initial = name, widget = forms.HiddenInput) + bundle_id = forms.IntegerField(widget = forms.HiddenInput) + class DelegateField(forms.ModelChoiceField): def __init__(self, project, *args, **kwargs): queryset = User.objects.filter(userprofile__in = \ @@ -104,6 +114,7 @@ class UserProfileForm(forms.ModelForm): class OptionalDelegateField(DelegateField): no_change_choice = ('*', 'no change') + to_field_name = None def __init__(self, no_change_choice = None, *args, **kwargs): self.filter = None @@ -130,6 +141,7 @@ class OptionalDelegateField(DelegateField): class OptionalModelChoiceField(forms.ModelChoiceField): no_change_choice = ('*', 'no change') + to_field_name = None def __init__(self, no_change_choice = None, *args, **kwargs): self.filter = None @@ -164,13 +176,30 @@ 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 self.is_no_change(value): + return value + elif value == 'True': + return True + elif value == 'False': + return False + else: + raise ValueError('Unknown value: %s' % value) + +class MultiplePatchForm(forms.Form): 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) @@ -180,7 +209,7 @@ class MultiplePatchForm(PatchForm): 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 @@ -190,10 +219,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)