2025-11-27 11:53:46 +05:30
|
|
|
from django import forms
|
|
|
|
|
from django.contrib.auth.forms import UserCreationForm
|
2025-11-28 03:11:38 +05:30
|
|
|
from django.contrib.auth.forms import AuthenticationForm
|
2025-11-28 16:03:23 +05:30
|
|
|
from django import forms
|
2025-11-27 11:53:46 +05:30
|
|
|
|
2025-11-28 18:52:02 +05:30
|
|
|
from django import forms
|
2025-11-28 18:55:33 +05:30
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
|
|
User = get_user_model()
|
2025-11-28 18:52:02 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserForm(forms.ModelForm):
|
2025-12-09 03:59:57 +05:30
|
|
|
full_name = forms.CharField(
|
|
|
|
|
max_length=150,
|
|
|
|
|
required=True,
|
|
|
|
|
label="Full Name"
|
|
|
|
|
)
|
2025-11-28 20:22:14 +05:30
|
|
|
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"
|
|
|
|
|
)
|
2025-11-28 18:52:02 +05:30
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = User
|
2025-12-09 03:59:57 +05:30
|
|
|
fields = ["username","full_name", "email", "phone_number", "role", "password", "confirm_password"]
|
2025-11-28 03:11:38 +05:30
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2025-11-28 16:03:23 +05:30
|
|
|
for field in self.fields.values():
|
|
|
|
|
field.widget.attrs.update({"class": "form-control"})
|
2025-11-28 03:11:38 +05:30
|
|
|
|
2025-11-28 20:22:14 +05:30
|
|
|
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
|
|
|
|
|
|
2025-11-28 18:52:02 +05:30
|
|
|
|
2025-11-27 11:53:46 +05:30
|
|
|
|
|
|
|
|
class LoginForm(AuthenticationForm):
|
2025-11-28 16:03:23 +05:30
|
|
|
username = forms.CharField(
|
|
|
|
|
widget=forms.TextInput(attrs={
|
|
|
|
|
"class": "form-control",
|
|
|
|
|
"placeholder": "Enter username"
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
password = forms.CharField(
|
|
|
|
|
widget=forms.PasswordInput(attrs={
|
|
|
|
|
"class": "form-control",
|
|
|
|
|
"placeholder": "Enter password"
|
|
|
|
|
})
|
|
|
|
|
)
|