Updates for the api and bug fixes

This commit is contained in:
Vivek
2025-12-19 19:35:38 +05:30
parent 105da4a876
commit d109df3973
10 changed files with 302 additions and 37 deletions

View File

@@ -15,7 +15,8 @@ class RegisterForm(forms.ModelForm):
def clean_email(self):
email = self.cleaned_data.get('email')
if User.objects.filter(email=email).exists():
# Ensure both email and username do not clash, since we set username = email
if User.objects.filter(email=email).exists() or User.objects.filter(username=email).exists():
raise forms.ValidationError("Email is already registered.")
return email
@@ -27,12 +28,57 @@ class RegisterForm(forms.ModelForm):
def save(self, commit=True):
user = super().save(commit=False)
# Set username equal to email to avoid separate username errors
user.username = self.cleaned_data['email']
user.set_password(self.cleaned_data['password'])
if commit:
user.save()
return user
class WebRegisterForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
confirm_password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['first_name', 'last_name', 'email', 'phone_number', 'password', 'confirm_password']
def clean_email(self):
email = self.cleaned_data.get('email')
# Ensure both email and username do not clash, since we set username = email
if User.objects.filter(email=email).exists() or User.objects.filter(username=email).exists():
raise forms.ValidationError("Email is already registered.")
return email
def clean_phone_number(self):
phone_number = self.cleaned_data.get('phone_number')
if User.objects.filter(phone_number=phone_number).exists():
raise forms.ValidationError("Phone number is already registered.")
return phone_number
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password')
if password != confirm_password:
raise forms.ValidationError("Passwords do not match.")
return cleaned_data
def save(self, commit=True):
user = super().save(commit=False)
# Set username equal to email to avoid separate username errors
user.username = self.cleaned_data['email']
user.set_password(self.cleaned_data['password'])
print('*' * 100)
print(user.username)
print('*' * 100)
if commit:
user.save()
return user
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)