mirror of
https://github.com/nanoy42/coope
synced 2024-11-24 20:33:12 +00:00
Unique email
This commit is contained in:
parent
459d7349b7
commit
2167e61c99
2 changed files with 11 additions and 4 deletions
|
@ -19,13 +19,20 @@ class CreateUserForm(forms.ModelForm):
|
||||||
model = User
|
model = User
|
||||||
fields = ("username", "last_name", "first_name", "email")
|
fields = ("username", "last_name", "first_name", "email")
|
||||||
|
|
||||||
school = forms.ModelChoiceField(queryset=School.objects.all(), label="École")
|
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):
|
def clean(self):
|
||||||
cleaned_data = super().clean()
|
cleaned_data = super().clean()
|
||||||
email = cleaned_data.get("email")
|
email = cleaned_data.get("email")
|
||||||
if User.objects.filter(email=email).count() > 0:
|
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):
|
class CreateGroupForm(forms.ModelForm):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -171,7 +171,7 @@ def createUser(request):
|
||||||
"""
|
"""
|
||||||
Displays a :class:`~users.forms.CreateUserForm` to create a user (:class:`django.contrib.auth.models.User`).
|
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()):
|
if(form.is_valid()):
|
||||||
user = form.save(commit=False)
|
user = form.save(commit=False)
|
||||||
user.save()
|
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`).
|
Displays a :class:`~users.forms.CreateUserForm` to edit a user (:class:`django.contrib.auth.models.User`).
|
||||||
"""
|
"""
|
||||||
user = get_object_or_404(User, pk=pk)
|
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()):
|
if(form.is_valid()):
|
||||||
user.profile.school = form.cleaned_data['school']
|
user.profile.school = form.cleaned_data['school']
|
||||||
user.save()
|
user.save()
|
||||||
|
|
Loading…
Reference in a new issue