Commit a72297f5 authored by Michal Čihař's avatar Michal Čihař

Validate unique email when adding users

Issue #1086
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent ccc3dd4c
......@@ -24,6 +24,7 @@ from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from weblate.accounts.forms import UniqueEmailMixin
from weblate.accounts.models import Profile, VerifiedEmail, AutoGroup
......@@ -61,7 +62,7 @@ class WeblateUserChangeForm(UserChangeForm):
self.fields['email'].required = True
class WeblateUserCreationForm(UserCreationForm):
class WeblateUserCreationForm(UserCreationForm, UniqueEmailMixin):
class Meta(object):
fields = ('username', 'email')
......
......@@ -69,6 +69,23 @@ def sort_choices(choices):
)
class UniqueEmailMixin(object):
def clean_email(self):
"""
Validate that the supplied email address is unique for the
site.
"""
if User.objects.filter(email__iexact=self.cleaned_data['email']):
raise forms.ValidationError(
_(
"This email address is already in use. "
"Please supply a different email address."
)
)
return self.cleaned_data['email']
class NoStripEmailField(forms.EmailField):
"""
Email field which does no stripping.
......@@ -311,7 +328,7 @@ class ContactForm(forms.Form):
return ''
class EmailForm(forms.Form):
class EmailForm(forms.Form, UniqueEmailMixin):
'''
Email change form.
'''
......@@ -325,21 +342,6 @@ class EmailForm(forms.Form):
)
content = forms.CharField(required=False)
def clean_email(self):
"""
Validate that the supplied email address is unique for the
site.
"""
if User.objects.filter(email__iexact=self.cleaned_data['email']):
raise forms.ValidationError(
_(
"This email address is already in use. "
"Please supply a different email address."
)
)
return self.cleaned_data['email']
def clean_content(self):
'''
Check if content is empty.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment