From dc67ed5298316cfe420016c23563a98d2610aec1 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 21 Apr 2020 19:27:12 +0200 Subject: [PATCH] Check email in user clean (factorise code) --- users/forms.py | 70 ------------------------------------------------- users/models.py | 41 +++++++++++++++-------------- 2 files changed, 22 insertions(+), 89 deletions(-) diff --git a/users/forms.py b/users/forms.py index 34d5bfa9..d9d68c88 100644 --- a/users/forms.py +++ b/users/forms.py @@ -147,21 +147,6 @@ class UserCreationForm(FormRevMixin, forms.ModelForm): super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields["email"].required = True - def clean_email(self): - new_email = self.cleaned_data.get("email") - - if not new_email: - 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.") - ) - class Meta: model = Adherent fields = ("pseudo", "surname", "email") @@ -358,18 +343,6 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): label=_("Force the move?"), initial=False, required=False ) - 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 - ) - ) - def clean_telephone(self): """Verifie que le tel est présent si 'option est validée dans preferences""" @@ -488,17 +461,6 @@ 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: - 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") @@ -559,19 +521,6 @@ 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 only if the user had an empty email before - if original_email and not new_email: - 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é @@ -873,25 +822,6 @@ class EmailSettingsForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): if "local_email_enabled" in self.fields: self.fields["local_email_enabled"].label = _("Use local emails") - def clean_email(self): - original_email = self.user.email - new_email = self.cleaned_data.get("email") - - # Allow empty emails only if the user had an empty email before - if original_email and not new_email: - 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( - OptionalUser.objects.first().local_email_domain - ) - ) - class Meta: model = User fields = ["email", "local_email_enabled", "local_email_redirect"] diff --git a/users/models.py b/users/models.py index 805e8b69..86d2cb5f 100755 --- a/users/models.py +++ b/users/models.py @@ -1323,32 +1323,35 @@ class User( self.__original_state = self.state self.__original_email = self.email - def clean(self, *args, **kwargs): - """Check if this pseudo is already used by any mailalias. - Better than raising an error in post-save and catching it""" + def clean_pseudo(self, *args, **kwargs): if EMailAddress.objects.filter(local_part=self.pseudo.lower()).exclude( user_id=self.id ): raise ValidationError(_("This username is already used.")) - if ( - not self.local_email_enabled - and not self.email - and not (self.state == self.STATE_FULL_ARCHIVE) - ): - raise ValidationError( - _( - "There is neither a local email address nor an external" - " email address for this user." - ) - ) - if self.local_email_redirect and not self.email: - raise ValidationError( - _( - "You can't redirect your local emails if no external email" - " address has been set." + + def clean_email(self, *args, **kwargs): + # Allow empty emails if the user had an empty email before + if not self.email and (self.__original_email or not self.pk): + raise forms.ValidationError( + _("Email field cannot be empty.") + ) + + self.email = self.email.lower() + + if OptionalUser.get_cached_value("local_email_domain") in self.email: + raise forms.ValidationError( + _("You can't use a {} address as an external contact address.").format( + OptionalUser.get_cached_value("local_email_domain") ) ) + def clean(self, *args, **kwargs): + super(User, self).clean(*args, **kwargs) + """Check if this pseudo is already used by any mailalias. + Better than raising an error in post-save and catching it""" + self.clean_pseudo(*args, **kwargs) + self.clean_email(*args, **kwargs) + def __str__(self): return self.pseudo