diff --git a/core/forms/profiles.py b/core/forms/profiles.py index 407b3e5..32a63e1 100644 --- a/core/forms/profiles.py +++ b/core/forms/profiles.py @@ -22,6 +22,16 @@ class AdminEditForm(ModelForm): model = get_user_model() 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): display_name = CharField(required=False, label=_('Internal Display Name')) mobile = PhoneNumberField(required=False, label=_('Mobile Number')) @@ -32,6 +42,12 @@ class AdminCreateForm(ModelForm): model = get_user_model() 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): company = CharField(required=False, label=_('Company Name')) mobile = PhoneNumberField(required=False, label=_('Mobile Number')) @@ -53,6 +69,16 @@ class ClientEditForm(ModelForm): 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') + 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): company = CharField(required=False, label=_('Company Name')) mobile = PhoneNumberField(required=False, label=_('Mobile Number')) @@ -74,3 +100,9 @@ class ClientCreateForm(ModelForm): class Meta: 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') + + 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!")) \ No newline at end of file diff --git a/core/views/backend/profiles.py b/core/views/backend/profiles.py index 473b3f3..47472c9 100644 --- a/core/views/backend/profiles.py +++ b/core/views/backend/profiles.py @@ -30,6 +30,11 @@ class AdminEditView(BackendFormView): context["title"] = "Edit Administrator" 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): initial = super().get_initial() admin = get_user_model().objects.get(id=self.kwargs["pk"]) @@ -128,6 +133,11 @@ class ClientEditView(BackendFormView): context["title"] = "Edit Client" 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): initial = super().get_initial() client = ClientProfile.objects.get(id=self.kwargs["pk"]).user