8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-30 08:32:26 +00:00

Check email in user clean (factorise code)

This commit is contained in:
Gabriel Detraz 2020-04-21 19:27:12 +02:00
parent 6235909a9a
commit dc67ed5298
2 changed files with 22 additions and 89 deletions

View file

@ -147,21 +147,6 @@ class UserCreationForm(FormRevMixin, forms.ModelForm):
super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields["email"].required = True 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: class Meta:
model = Adherent model = Adherent
fields = ("pseudo", "surname", "email") fields = ("pseudo", "surname", "email")
@ -358,18 +343,6 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
label=_("Force the move?"), initial=False, required=False 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): def clean_telephone(self):
"""Verifie que le tel est présent si 'option est validée """Verifie que le tel est présent si 'option est validée
dans preferences""" dans preferences"""
@ -488,17 +461,6 @@ class AdherentCreationForm(AdherentForm):
self.fields.pop("password1") self.fields.pop("password1")
self.fields.pop("password2") 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): def clean_password2(self):
"""Verifie que password1 et 2 sont identiques (si nécessaire)""" """Verifie que password1 et 2 sont identiques (si nécessaire)"""
send_email = self.cleaned_data.get("init_password_by_mail") send_email = self.cleaned_data.get("init_password_by_mail")
@ -559,19 +521,6 @@ class AdherentEditForm(AdherentForm):
"shortcuts_enabled", "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): class ClubForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
"""Formulaire de base d'edition d'un user. Formulaire de base, utilisé """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: if "local_email_enabled" in self.fields:
self.fields["local_email_enabled"].label = _("Use local emails") 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: class Meta:
model = User model = User
fields = ["email", "local_email_enabled", "local_email_redirect"] fields = ["email", "local_email_enabled", "local_email_redirect"]

View file

@ -1323,32 +1323,35 @@ class User(
self.__original_state = self.state self.__original_state = self.state
self.__original_email = self.email self.__original_email = self.email
def clean(self, *args, **kwargs): def clean_pseudo(self, *args, **kwargs):
"""Check if this pseudo is already used by any mailalias.
Better than raising an error in post-save and catching it"""
if EMailAddress.objects.filter(local_part=self.pseudo.lower()).exclude( if EMailAddress.objects.filter(local_part=self.pseudo.lower()).exclude(
user_id=self.id user_id=self.id
): ):
raise ValidationError(_("This username is already used.")) raise ValidationError(_("This username is already used."))
if (
not self.local_email_enabled def clean_email(self, *args, **kwargs):
and not self.email # Allow empty emails if the user had an empty email before
and not (self.state == self.STATE_FULL_ARCHIVE) if not self.email and (self.__original_email or not self.pk):
): raise forms.ValidationError(
raise ValidationError( _("Email field cannot be empty.")
_( )
"There is neither a local email address nor an external"
" email address for this user." self.email = self.email.lower()
)
) if OptionalUser.get_cached_value("local_email_domain") in self.email:
if self.local_email_redirect and not self.email: raise forms.ValidationError(
raise ValidationError( _("You can't use a {} address as an external contact address.").format(
_( OptionalUser.get_cached_value("local_email_domain")
"You can't redirect your local emails if no external email"
" address has been set."
) )
) )
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): def __str__(self):
return self.pseudo return self.pseudo