Files
eventify_backend/accounts/forms.py

91 lines
2.4 KiB
Python

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from django import forms
from django import forms
from django.contrib.auth import get_user_model
User = get_user_model()
class UserForm(forms.ModelForm):
full_name = forms.CharField(
max_length=150,
required=True,
label="Full Name"
)
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","full_name", "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 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):
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"
})
)