From f597b6dbd89ee63bcfef91b917d34f5e2842f782 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 28 Jul 2020 23:16:38 +0200 Subject: [PATCH] Prevent creating club when there exists an alias that is similar to the name of the club --- apps/member/forms.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/member/forms.py b/apps/member/forms.py index 4bf3b738..9dc818b4 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -5,7 +5,7 @@ from django import forms from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ -from note.models import NoteSpecial +from note.models import NoteSpecial, Alias from note_kfet.inputs import Autocomplete, AmountInput, DatePickerInput from permission.models import PermissionMask, Role @@ -38,6 +38,15 @@ class ProfileForm(forms.ModelForm): class ClubForm(forms.ModelForm): + def clean(self): + cleaned_data = super().clean() + + if not self.instance.pk: # Creating a club + if Alias.objects.filter(normalized_name=Alias.normalize(self.cleaned_data["name"])).exists(): + self.add_error('name', _("An alias with a similar name already exists.")) + + return cleaned_data + class Meta: model = Club fields = '__all__'