Skip to content

Commit

Permalink
fix issue anipiano#4 request.get_username fetch invalidated username
Browse files Browse the repository at this point in the history
  • Loading branch information
WajahatKanju committed Aug 11, 2023
1 parent fa8c54f commit e761a49
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions anipianolist/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,44 @@
from django.contrib.auth.models import User
from .models import Profile


class UserForm(forms.ModelForm):
username = forms.CharField(max_length=150, label="Username", required=True, error_messages={'invalid':'Your username can only contain letters, numbers, periods, hyphens or underscores, baka!'})
class Meta:
model = User
fields = ["username"]
labels = {
"username": "Username",
}

def clean_username(self):
username = self.cleaned_data.get("username")
if not username:
raise forms.ValidationError("This field is required.")

if not username.isalnum() or any(
char
not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_"
for char in username
):
raise forms.ValidationError(
"Your username can only contain letters, numbers, periods, hyphens or underscores."
)

if "@" in username or "+" in username:
raise forms.ValidationError("Your username cannot contain '@' or '+'.")

class Meta:
model = User
fields = ['username']
return username

def clean(self):
cleaned_data = super(UserForm, self).clean()
username = cleaned_data.get('username')
if (('@') or ('+')) in username:
self.add_error('username', 'Your username can only contain letters, numbers, periods, hyphens or underscores, baka!')
return cleaned_data

class ProfileForm(forms.ModelForm):
pfp = forms.ImageField(label="Profile picture", required=False, widget=forms.FileInput) # Django ImageField automatically handles image verification, naisu! https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.ImageField
location = forms.CharField(max_length=69, label="Location", required=False)
bio = forms.CharField(max_length=2000, label="About", widget=forms.Textarea, required=False)
pfp = forms.ImageField(
label="Profile picture", required=False, widget=forms.FileInput
) # Django ImageField automatically handles image verification, naisu! https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.ImageField
location = forms.CharField(max_length=69, label="Location", required=False)
bio = forms.CharField(
max_length=2000, label="About", widget=forms.Textarea, required=False
)

class Meta:
model = Profile
fields = ['pfp', 'location', 'bio']
class Meta:
model = Profile
fields = ["pfp", "location", "bio"]

0 comments on commit e761a49

Please sign in to comment.