refactor datatables
This commit is contained in:
@@ -18,7 +18,6 @@ class LoginForm(forms.Form):
|
||||
)
|
||||
|
||||
|
||||
|
||||
class UserForm(forms.ModelForm):
|
||||
password = forms.CharField(
|
||||
widget=forms.PasswordInput(attrs={"autocomplete": "off"}),
|
||||
@@ -44,11 +43,26 @@ class UserForm(forms.ModelForm):
|
||||
"first_name": "Name",
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
instance = kwargs.get("instance")
|
||||
if instance and instance.pk:
|
||||
# Remove password and confirm_password fields if instance exists (update action)
|
||||
self.fields.pop("password")
|
||||
self.fields.pop("confirm_password")
|
||||
# Make email field readonly
|
||||
self.fields["email"].widget.attrs["readonly"] = True
|
||||
|
||||
def clean_email(self):
|
||||
email = self.cleaned_data.get('email')
|
||||
if IAmPrincipal.objects.filter(email=email).exists():
|
||||
raise forms.ValidationError("This email address is already in use.")
|
||||
return email
|
||||
# Prevent email from being updated
|
||||
instance = self.instance
|
||||
if instance and instance.pk:
|
||||
return instance.email
|
||||
else:
|
||||
email = self.cleaned_data.get("email")
|
||||
if IAmPrincipal.objects.filter(email=email).exclude(pk=instance.pk).exists():
|
||||
raise forms.ValidationError("This email address is already in use.")
|
||||
return email
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
|
||||
Reference in New Issue
Block a user