Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing users from signing up with the username "follow" #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions Chapter07/bookmarks/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,55 @@

from .models import Profile

User = get_user_model()


class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)


class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(
label='Password',
widget=forms.PasswordInput
)
password2 = forms.CharField(
label='Repeat password',
widget=forms.PasswordInput
)
password = forms.CharField(label="Password", widget=forms.PasswordInput)
password2 = forms.CharField(label="Repeat password", widget=forms.PasswordInput)

class Meta:
model = get_user_model()
fields = ['username', 'first_name', 'email']
model = User
fields = ["username", "first_name", "email"]

def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
if cd["password"] != cd["password2"]:
raise forms.ValidationError("Passwords don't match.")
return cd['password2']
return cd["password2"]

def clean_username(self):
username = self.cleaned_data["username"]
if username.lower() == "follow":
raise forms.ValidationError("This username is not allowed.")
return username

def clean_email(self):
data = self.cleaned_data['email']
data = self.cleaned_data["email"]
if User.objects.filter(email=data).exists():
raise forms.ValidationError('Email already in use.')
raise forms.ValidationError("Email already in use.")
return data


class UserEditForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = ['first_name', 'last_name', 'email']
model = User
fields = ["first_name", "last_name", "email"]

def clean_email(self):
data = self.cleaned_data['email']
qs = User.objects.exclude(
id=self.instance.id
).filter(
email=data
)
data = self.cleaned_data["email"]
qs = User.objects.exclude(id=self.instance.id).filter(email=data)
if qs.exists():
raise forms.ValidationError('Email already in use.')
raise forms.ValidationError("Email already in use.")
return data


class ProfileEditForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['date_of_birth', 'photo']
fields = ["date_of_birth", "photo"]