Check email address domain when signing up

This commit is contained in:
Alexandre Iooss 2021-10-11 19:06:29 +02:00
parent 5728b9a18e
commit f992c88152
6 changed files with 11 additions and 48 deletions

View file

@ -1,31 +0,0 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
class RegistrationForm(UserCreationForm):
email = forms.EmailField(
label=_("Email address"),
widget=forms.TextInput(),
required=True,
help_text=_(
"Please enter a valid email address ending with `@crans.org` or "
"`@ens-paris-saclay.fr`."
),
)
def clean_email(self):
"""
Check that the email address ends with a trusted domain.
"""
email = self.cleaned_data.get("email")
if not email.endswith("@crans.org") and not email.endswith("@ens-paris-saclay.fr"):
raise forms.ValidationError(
_("Must end with `@crans.org` or `@ens-paris-saclay.fr`.")
)
return email
class Meta:
model = User
fields = ["username", "password1", "password2", "email"]