You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
3.1 KiB
88 lines
3.1 KiB
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
from .models import User
|
|
|
|
class UserRegistrationForm(forms.ModelForm):
|
|
password = forms.CharField(
|
|
widget=forms.PasswordInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Enter your password',
|
|
})
|
|
)
|
|
confirm_password = forms.CharField(
|
|
widget=forms.PasswordInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Confirm your password',
|
|
})
|
|
)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ['email', 'first_name', 'last_name', 'profile', 'price',
|
|
'bio', 'service_offered', 'login_profile']
|
|
widgets = {
|
|
'first_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Enter your first name',
|
|
}),
|
|
'last_name': forms.TextInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Enter your last name',
|
|
}),
|
|
'email': forms.EmailInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Enter your email address',
|
|
}),
|
|
'profile': forms.ClearableFileInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Upload your profile image',
|
|
}),
|
|
'bio': forms.Textarea(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Enter your bio',
|
|
'rows': 4,
|
|
}),
|
|
'price': forms.NumberInput(attrs={
|
|
'class': 'form-control',
|
|
'placeholder': 'Enter your price',
|
|
}),
|
|
'service_offered': forms.Select(attrs={
|
|
'class': 'form-control',
|
|
}),
|
|
'login_profile': forms.Select(attrs={
|
|
'class': 'form-control',
|
|
}),
|
|
}
|
|
|
|
def clean_email(self):
|
|
email = self.cleaned_data.get('email')
|
|
if User.objects.filter(email=email).exists():
|
|
raise ValidationError('Email is already registered.')
|
|
return email
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
password = cleaned_data.get('password')
|
|
confirm_password = cleaned_data.get('confirm_password')
|
|
|
|
# Password validation
|
|
if password:
|
|
if len(password) < 8:
|
|
raise ValidationError('Password must be at least 8 characters long.')
|
|
if not any(char.isdigit() for char in password):
|
|
raise ValidationError('Password must contain at least one number.')
|
|
if not any(char.isupper() for char in password):
|
|
raise ValidationError('Password must contain at least one uppercase letter.')
|
|
|
|
# Check if passwords match
|
|
if password and confirm_password and password != confirm_password:
|
|
raise ValidationError('Passwords do not match.')
|
|
|
|
return cleaned_data
|
|
|
|
def save(self, commit=True):
|
|
user = super().save(commit=False)
|
|
user.set_password(self.cleaned_data['password'])
|
|
if commit:
|
|
user.save()
|
|
return user
|
|
|