3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-12-23 15:33:45 +00:00

Unique email

This commit is contained in:
Yoann Pétri 2019-09-25 20:51:33 +01:00
parent 459d7349b7
commit 2167e61c99
2 changed files with 11 additions and 4 deletions

View file

@ -21,11 +21,18 @@ class CreateUserForm(forms.ModelForm):
school = forms.ModelChoiceField (queryset=School.objects.all(), label="École")
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(CreateUserForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
email = cleaned_data.get("email")
if User.objects.filter(email=email).count() > 0:
raise forms.ValidationError("L'email est déjà utilisé")
if User.objects.filter(email=email).count() == 1 and User.objects.get(email=email) == self.user:
pass
else:
raise forms.ValidationError("L'adresse email est déjà utilisée")
class CreateGroupForm(forms.ModelForm):
"""

View file

@ -171,7 +171,7 @@ def createUser(request):
"""
Displays a :class:`~users.forms.CreateUserForm` to create a user (:class:`django.contrib.auth.models.User`).
"""
form = CreateUserForm(request.POST or None)
form = CreateUserForm(request.POST or None, user=User.objects.none())
if(form.is_valid()):
user = form.save(commit=False)
user.save()
@ -272,7 +272,7 @@ def editUser(request, pk):
Displays a :class:`~users.forms.CreateUserForm` to edit a user (:class:`django.contrib.auth.models.User`).
"""
user = get_object_or_404(User, pk=pk)
form = CreateUserForm(request.POST or None, instance=user, initial = {'school': user.profile.school})
form = CreateUserForm(request.POST or None, instance=user, initial = {'school': user.profile.school}, user=user)
if(form.is_valid()):
user.profile.school = form.cleaned_data['school']
user.save()