diff --git a/petitions/forms.py b/petitions/forms.py index cdc73746ff140d20c972240c8cb32f55b0fb7e4f..7321a5178d4cf265d135995e3bc15234147d439c 100644 --- a/petitions/forms.py +++ b/petitions/forms.py @@ -12,20 +12,32 @@ class SignPetitionForm(forms.ModelForm): class Meta: model = PetitionSignatory - fields = ['petition', 'title', 'first_name', 'last_name', + fields = ['title', 'first_name', 'last_name', 'email', 'country_of_employment', 'affiliation'] - widgets = {'petition': forms.HiddenInput()} + def __init__(self, *args, **kwargs): + self.petition = kwargs.pop('petition', False) + self.current_user = kwargs.pop('current_user', False) + super().__init__(*args, **kwargs) def clean_email(self): email = self.cleaned_data['email'] - petition = self.cleaned_data['petition'] + petition = self.petition + if not petition: + return email + if self.instance.id: return email - if Contributor.objects.filter(user__email=email).exists(): - self.add_error('email', ('This email address is associated to a Contributor; please ' - 'login to sign the petition')) - elif PetitionSignatory.objects.filter(petition=petition, email=email).exists(): + if self.current_user.is_authenticated(): + if self.current_user.email != email: + self.add_error('email', 'This email address is not associated to your account') + else: + if Contributor.objects.filter(user__email=email).exists(): + self.add_error('email', ('This email address is associated to a Contributor; please ' + 'login to sign the petition')) + if PetitionSignatory.objects.filter(petition=petition, email=email).exists(): self.add_error('email', ('This email address is already associated to a ' 'signature for this petition')) + + return email diff --git a/petitions/views.py b/petitions/views.py index 56ead1eea6c32dc2bd0487b4f901d5f36629fabf..7caab2ca1571b95c1d31f420e1b8fb1763d6ac1c 100644 --- a/petitions/views.py +++ b/petitions/views.py @@ -29,7 +29,8 @@ def petition(request, slug): 'affiliation': request.user.contributor.affiliation, } - form = SignPetitionForm(request.POST or None, initial=initial) + form = SignPetitionForm(request.POST or None, initial=initial, petition=petition, + current_user=request.user) if form.is_valid(): signature = form.save(commit=False) signature.petition = petition