Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add username suggestions to registration forms

birjd002626a2d15b682
diff --git a/wger/core/forms.py b/wger/core/forms.py
index f5237f13b..c071cd679 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -55,6 +55,7 @@ from django_recaptcha.widgets import ReCaptchaV3
# wger
from wger.core.models import UserProfile
+from wger.utils.username import generate_username_suggestions
class PasswordInputWithToggle(PasswordInput):
@@ -291,6 +292,16 @@ class RegistrationForm(UserCreationForm, UserEmailForm):
help_text=gettext_lazy('The form is secured with reCAPTCHA'),
)
+ def clean_username(self):
+ username = self.cleaned_data.get('username')
+ if User.objects.filter(username__iexact=username).exists():
+ suggestions = generate_username_suggestions(username)
+ suggestions_string = ', '.join(suggestions)
+ raise forms.ValidationError(
+ f'A user with this username already exists. Suggestions: {suggestions_string}'
+ )
+ return username
+
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -318,6 +329,16 @@ class RegistrationFormNoCaptcha(UserCreationForm, UserEmailForm):
Registration form without CAPTCHA field
"""
+ def clean_username(self):
+ username = self.cleaned_data.get('username')
+ if User.objects.filter(username__iexact=username).exists():
+ suggestions = generate_username_suggestions(username)
+ suggestions_string = ', '.join(suggestions)
+ raise forms.ValidationError(
+ f'A user with this username already exists. Suggestions: {suggestions_string}'
+ )
+ return username
+
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)