mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-23 15:33:45 +00:00
Check email in user clean (factorise code)
This commit is contained in:
parent
24abff4106
commit
e47b555343
2 changed files with 22 additions and 89 deletions
|
@ -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"]
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue