diff --git a/anipianolist/accounts/forms.py b/anipianolist/accounts/forms.py index d3809fa..fd4891f 100644 --- a/anipianolist/accounts/forms.py +++ b/anipianolist/accounts/forms.py @@ -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'] \ No newline at end of file + class Meta: + model = Profile + fields = ["pfp", "location", "bio"]