From ad2c1e3d260df8e2ca6e1798e031bdb8de8a9104 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Mon, 23 Mar 2020 12:46:10 +0100 Subject: [PATCH] Case of non existant email (legacy accounts) --- users/forms.py | 57 +++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/users/forms.py b/users/forms.py index cef1e43a..c719fd2a 100644 --- a/users/forms.py +++ b/users/forms.py @@ -144,14 +144,17 @@ class UserCreationForm(FormRevMixin, forms.ModelForm): super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) def clean_email(self): - if not OptionalUser.objects.first().local_email_domain in self.cleaned_data.get( - "email" - ): - return self.cleaned_data.get("email").lower() - else: - raise forms.ValidationError( - _("You can't use an internal address as your external address.") - ) + if self.cleaned_data.get("email"): + if not OptionalUser.objects.first().local_email_domain in self.cleaned_data.get( + "email" + ): + return self.cleaned_data.get("email").lower() + else: + raise forms.ValidationError( + _("You can't use a {} address.").format( + OptionalUser.objects.first().local_email_domain + ) + ) class Meta: model = Adherent @@ -349,16 +352,17 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): ) def clean_email(self): - if not OptionalUser.objects.first().local_email_domain in self.cleaned_data.get( - "email" - ): - return self.cleaned_data.get("email").lower() - else: - raise forms.ValidationError( - _("You can't use a {} address.").format( - OptionalUser.objects.first().local_email_domain + if self.cleaned_data.get("email"): + if not OptionalUser.objects.first().local_email_domain in self.cleaned_data.get( + "email" + ): + return self.cleaned_data.get("email").lower() + else: + raise forms.ValidationError( + _("You can't use a {} address.").format( + OptionalUser.objects.first().local_email_domain + ) ) - ) def clean_telephone(self): """Verifie que le tel est présent si 'option est validée @@ -756,16 +760,17 @@ class EmailSettingsForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): self.fields["local_email_enabled"].label = _("Use local emails") def clean_email(self): - if not OptionalUser.objects.first().local_email_domain in self.cleaned_data.get( - "email" - ): - return self.cleaned_data.get("email").lower() - else: - raise forms.ValidationError( - _("You can't use a {} address.").format( - OptionalUser.objects.first().local_email_domain + if self.cleaned_data.get("email"): + if not OptionalUser.objects.first().local_email_domain in self.cleaned_data.get( + "email" + ): + return self.cleaned_data.get("email").lower() + else: + raise forms.ValidationError( + _("You can't use a {} address.").format( + OptionalUser.objects.first().local_email_domain + ) ) - ) class Meta: model = User