Solving the user creation issues and to dashboard navbar to be as role based

This commit is contained in:
Vivek P Prakash
2025-11-28 20:22:14 +05:30
parent f092dd80c4
commit 392f86cfaf
3 changed files with 253 additions and 120 deletions

View File

@@ -10,22 +10,64 @@ User = get_user_model()
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
password = forms.CharField(
widget=forms.PasswordInput,
label="Password"
)
confirm_password = forms.CharField(
widget=forms.PasswordInput,
label="Confirm Password"
)
phone_number = forms.CharField(
max_length=15,
required=True,
label="Phone Number"
)
ROLE_CHOICES = [
('admin', 'Admin'),
('manager', 'Manager'),
('staff', 'Staff'),
]
role = forms.ChoiceField(
choices=ROLE_CHOICES,
required=True,
label="Role"
)
class Meta:
model = User
fields = ["username", "email", "password"]
fields = ["username", "email", "phone_number", "role", "password", "confirm_password"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs.update({"class": "form-control"})
def form_valid(self, form):
user = form.save(commit=False)
user.set_password(form.cleaned_data['password'])
user.save()
return super().form_valid(form)
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get("password")
confirm_password = cleaned_data.get("confirm_password")
if password and confirm_password and password != confirm_password:
self.add_error("confirm_password", "Passwords do not match!")
return cleaned_data
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data['password']) # Hash the password
# Save phone_number and role to the User model (if your User model has these fields)
user.phone_number = self.cleaned_data.get("phone_number")
user.role = self.cleaned_data.get("role")
if commit:
user.save()
return user
class LoginForm(AuthenticationForm):