Catch exception when creating accounts with email address already in use

This commit is contained in:
Kumi 2020-06-22 06:32:22 +02:00
parent 68c0488462
commit c15de35a54
2 changed files with 42 additions and 0 deletions

View file

@ -22,6 +22,16 @@ class AdminEditForm(ModelForm):
model = get_user_model() model = get_user_model()
fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image", "remove_image") fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image", "remove_image")
def __init__(self, *args, **kwargs):
self.current_email = kwargs.pop('current_email')
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
if cleaned_data["email"] != self.current_email and get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))
class AdminCreateForm(ModelForm): class AdminCreateForm(ModelForm):
display_name = CharField(required=False, label=_('Internal Display Name')) display_name = CharField(required=False, label=_('Internal Display Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number')) mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
@ -32,6 +42,12 @@ class AdminCreateForm(ModelForm):
model = get_user_model() model = get_user_model()
fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image") fields = ('first_name', 'last_name', "display_name", "email", 'mobile', "role", "image")
def clean(self):
cleaned_data = super().clean()
if get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))
class ClientEditForm(ModelForm): class ClientEditForm(ModelForm):
company = CharField(required=False, label=_('Company Name')) company = CharField(required=False, label=_('Company Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number')) mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
@ -53,6 +69,16 @@ class ClientEditForm(ModelForm):
model = get_user_model() model = get_user_model()
fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'client_groups', 'marketing_opt_in', 'pgp_key') fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'client_groups', 'marketing_opt_in', 'pgp_key')
def __init__(self, *args, **kwargs):
self.current_email = kwargs.pop('current_email')
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
if cleaned_data["email"] != self.current_email and get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))
class ClientCreateForm(ModelForm): class ClientCreateForm(ModelForm):
company = CharField(required=False, label=_('Company Name')) company = CharField(required=False, label=_('Company Name'))
mobile = PhoneNumberField(required=False, label=_('Mobile Number')) mobile = PhoneNumberField(required=False, label=_('Mobile Number'))
@ -74,3 +100,9 @@ class ClientCreateForm(ModelForm):
class Meta: class Meta:
model = get_user_model() model = get_user_model()
fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'client_groups', 'marketing_opt_in', 'pgp_key', 'send_password') fields = ('first_name', 'last_name', "company", "email", 'mobile', 'address1', 'address2', 'zip', 'city', 'state', 'country', 'vat_id', 'company_id', 'default_currency', 'brands', 'client_groups', 'marketing_opt_in', 'pgp_key', 'send_password')
def clean(self):
cleaned_data = super().clean()
if get_user_model().objects.filter(username=cleaned_data["email"]):
self.add_error("email", _("An account with this email address already exists!"))

View file

@ -30,6 +30,11 @@ class AdminEditView(BackendFormView):
context["title"] = "Edit Administrator" context["title"] = "Edit Administrator"
return context return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update({'current_email': ClientProfile.objects.get(id=self.kwargs["pk"]).user.username})
return kwargs
def get_initial(self): def get_initial(self):
initial = super().get_initial() initial = super().get_initial()
admin = get_user_model().objects.get(id=self.kwargs["pk"]) admin = get_user_model().objects.get(id=self.kwargs["pk"])
@ -128,6 +133,11 @@ class ClientEditView(BackendFormView):
context["title"] = "Edit Client" context["title"] = "Edit Client"
return context return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.update({'current_email': ClientProfile.objects.get(id=self.kwargs["pk"]).user.username})
return kwargs
def get_initial(self): def get_initial(self):
initial = super().get_initial() initial = super().get_initial()
client = ClientProfile.objects.get(id=self.kwargs["pk"]).user client = ClientProfile.objects.get(id=self.kwargs["pk"]).user