X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fforms.py;h=f83c27ae75c3e45fc3298e844cdbc0c75a691af8;hb=c2c6a408c7764fa29389ce160f52776c9308d50a;hp=bc746bd11c580deb3031dbaa1925bf36920d29de;hpb=c1b2c0e5787a1ba487cda222596b0ca7ae94f288;p=patchwork diff --git a/apps/patchwork/forms.py b/apps/patchwork/forms.py index bc746bd..f83c27a 100644 --- a/apps/patchwork/forms.py +++ b/apps/patchwork/forms.py @@ -22,34 +22,33 @@ from django.contrib.auth.models import User from django import forms from patchwork.models import Patch, State, Bundle, UserProfile -from registration.forms import RegistrationFormUniqueEmail -from registration.models import RegistrationProfile -class RegistrationForm(RegistrationFormUniqueEmail): +class RegistrationForm(forms.Form): first_name = forms.CharField(max_length = 30, required = False) last_name = forms.CharField(max_length = 30, required = False) - username = forms.CharField(max_length=30, label=u'Username') + username = forms.RegexField(regex = r'^\w+$', max_length=30, + label=u'Username') email = forms.EmailField(max_length=100, label=u'Email address') password = forms.CharField(widget=forms.PasswordInput(), label='Password') - password1 = forms.BooleanField(required = False) - password2 = forms.BooleanField(required = False) - def save(self, profile_callback = None): - user = RegistrationProfile.objects.create_inactive_user( \ - username = self.cleaned_data['username'], - password = self.cleaned_data['password'], - email = self.cleaned_data['email'], - profile_callback = profile_callback) - 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_username(self): + value = self.cleaned_data['username'] + try: + user = User.objects.get(username__iexact = value) + except User.DoesNotExist: + return self.cleaned_data['username'] + raise forms.ValidationError('This username is already taken. ' + \ + 'Please choose another.') + + def clean_email(self): + value = self.cleaned_data['email'] + try: + user = User.objects.get(email__iexact = value) + except User.DoesNotExist: + return self.cleaned_data['email'] + raise forms.ValidationError('This email address is already in use ' + \ + 'for the account "%s".\n' % user.username) def clean(self): return self.cleaned_data @@ -80,6 +79,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 = \ @@ -109,6 +113,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 @@ -135,6 +140,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 @@ -169,13 +175,31 @@ 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) @@ -185,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 @@ -195,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)