Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Chore move clean_username logic to validate_username function

birjd0026c906754e9ce
diff --git a/wger/core/forms.py b/wger/core/forms.py
index 18ed8de0a..073973628 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -55,7 +55,7 @@ from django_recaptcha.widgets import ReCaptchaV3
# wger
from wger.core.models import UserProfile
-from wger.utils.username import generate_username_suggestions
+from wger.core.validators import validate_username
class PasswordInputWithToggle(PasswordInput):
@@ -294,12 +294,7 @@ class RegistrationForm(UserCreationForm, UserEmailForm):
def clean_username(self):
username = self.cleaned_data.get('username')
- if User.objects.filter(username__exact=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}'
- )
+ validate_username(username)
return username
def __init__(self, *args, **kwargs):
@@ -331,12 +326,7 @@ class RegistrationFormNoCaptcha(UserCreationForm, UserEmailForm):
def clean_username(self):
username = self.cleaned_data.get('username')
- if User.objects.filter(username__exact=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}'
- )
+ validate_username(username)
return username
def __init__(self, *args, **kwargs):
diff --git a/wger/core/validators.py b/wger/core/validators.py
new file mode 100644
index 000000000..c04dc2715
--- /dev/null
+++ b/wger/core/validators.py
@@ -0,0 +1,32 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Django
+from django import forms
+from django.contrib.auth.models import User
+
+# wger
+from wger.utils.username import generate_username_suggestions
+
+
+def validate_username(username: str):
+ """
+ Checks that the username is not already taken and returns suggestions if it is.
+ """
+ if User.objects.filter(username__exact=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}'
+ )