From 9e1cd17a1b4ac42f64377b45088ac953e3e75063 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Tue, 21 Apr 2020 16:47:09 +0200 Subject: [PATCH] Handle empty emails --- users/forms.py | 60 ++++++++++++++++++++++++++++++++++++++++++------- users/models.py | 2 -- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/users/forms.py b/users/forms.py index 3440f2f9..2f3c2e86 100644 --- a/users/forms.py +++ b/users/forms.py @@ -147,10 +147,15 @@ 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() + new_email = self.cleaned_data.get("email") + + if not new_email or len(new_email) == 0: + raise forms.ValidationError( + _("Email field cannot be empty.") + ) + + if not OptionalUser.objects.first().local_email_domain in new_email: + return new_email.lower() else: raise forms.ValidationError( _("You can't use an internal address as your external address.") @@ -481,6 +486,17 @@ class AdherentCreationForm(AdherentForm): self.fields.pop("password1") self.fields.pop("password2") + def clean_email(self): + """Forbid empty email""" + new_email = self.cleaned_data.get("email") + + if not new_email or len(new_email) == 0: + raise forms.ValidationError( + _("Email field cannot be empty.") + ) + + return new_email + def clean_password2(self): """Verifie que password1 et 2 sont identiques (si nécessaire)""" send_email = self.cleaned_data.get("init_password_by_mail") @@ -520,6 +536,7 @@ class AdherentEditForm(AdherentForm): self.fields["gpg_fingerprint"].widget.attrs["placeholder"] = _( "Leave empty if you don't have any GPG key." ) + self.user = kwargs["instance"] if "shell" in self.fields: self.fields["shell"].empty_label = _("Default shell") @@ -539,6 +556,22 @@ class AdherentEditForm(AdherentForm): "shortcuts_enabled", ] + def clean_email(self): + """Forbid empty email""" + original_email = self.user.email + new_email = self.cleaned_data.get("email") + + # Allow empty emails if the user had an empty email before + if original_email is None or len(original_email) == 0: + return new_email + + if new_email is None or len(new_email) == 0: + raise forms.ValidationError( + _("Email field cannot be empty.") + ) + + return new_email + class ClubForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): """Formulaire de base d'edition d'un user. Formulaire de base, utilisé @@ -832,6 +865,7 @@ class EmailSettingsForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop("prefix", self.Meta.model.__name__) super(EmailSettingsForm, self).__init__(*args, prefix=prefix, **kwargs) + self.user = kwargs["instance"] self.fields["email"].label = _("Main email address") if "local_email_redirect" in self.fields: self.fields["local_email_redirect"].label = _("Redirect local emails") @@ -839,10 +873,20 @@ 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() + original_email = self.user.email + new_email = self.cleaned_data.get("email") + + # Allow empty emails if the user had an empty email before + if original_email is None or len(original_email) == 0: + return new_email + + if new_email is None or len(new_email) == 0: + raise forms.ValidationError( + _("Email field cannot be empty.") + ) + + if not OptionalUser.objects.first().local_email_domain in new_email: + return new_email.lower() else: raise forms.ValidationError( _("You can't use a {} address.").format( diff --git a/users/models.py b/users/models.py index 42df3f5a..8958a95d 100755 --- a/users/models.py +++ b/users/models.py @@ -205,8 +205,6 @@ class User( validators=[linux_user_validator], ) email = models.EmailField( - blank=True, - null=True, help_text=_("External email address allowing us to contact you."), ) local_email_redirect = models.BooleanField(