1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-25 06:22:22 +00:00

Better unique validation errors

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2023-04-10 09:56:16 +02:00
parent b40dce27df
commit a240d7cad5
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 11 additions and 2 deletions

View File

@ -26,7 +26,7 @@ class TeamForm(forms.ModelForm):
def clean_name(self):
if "name" in self.cleaned_data:
name = self.cleaned_data["name"]
if not self.instance.pk and Team.objects.filter(name=name).exists():
if Team.objects.filter(name=name).exclude(pk=self.instance.pk).exists():
raise ValidationError(_("This name is already used."))
return name
@ -36,7 +36,7 @@ class TeamForm(forms.ModelForm):
if not re.match("[A-Z]{3}", trigram):
raise ValidationError(_("The trigram must be composed of three uppercase letters."))
if not self.instance.pk and Team.objects.filter(trigram=trigram).exists():
if Team.objects.filter(trigram=trigram).exclude(pk=self.instance.pk).exists():
raise ValidationError(_("This trigram is already used."))
return trigram

View File

@ -82,6 +82,15 @@ class UserForm(forms.ModelForm):
self.fields["last_name"].required = True
self.fields["email"].required = True
def clean_email(self):
"""
Ensure that the email address is unique.
"""
email = self.data["email"]
if User.objects.filter(email=email).exclude(pk=self.instance.pk).exists():
self.add_error("email", _("This email address is already used."))
return email
class Meta:
model = User
fields = ('first_name', 'last_name', 'email',)