From afe8f62dfdc789c866270ea54710fe9bc0ba6d65 Mon Sep 17 00:00:00 2001 From: bbearce Date: Wed, 15 Mar 2023 14:40:53 +0000 Subject: [PATCH 1/5] validate usernames --- src/apps/profiles/forms.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/apps/profiles/forms.py b/src/apps/profiles/forms.py index a959acb8d..ee02b71ad 100644 --- a/src/apps/profiles/forms.py +++ b/src/apps/profiles/forms.py @@ -5,7 +5,13 @@ class SignUpForm(UserCreationForm): email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.') - + def clean_username(self): + data = self.cleaned_data['username'] + if not data.islower(): + raise forms.ValidationError("Usernames should be in lowercase") + if not data.isalnum(): + raise forms.ValidationError("Usernames should be alpha numeric and not have special characters.") + return data class Meta: model = User fields = ('username', 'email', 'password1', 'password2', ) From 07f1ea7622f918592c485c7466c7b3e9652b4f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Wed, 15 Mar 2023 15:57:21 +0100 Subject: [PATCH 2/5] Flake8 formatting --- src/apps/profiles/forms.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/apps/profiles/forms.py b/src/apps/profiles/forms.py index ee02b71ad..1898c7046 100644 --- a/src/apps/profiles/forms.py +++ b/src/apps/profiles/forms.py @@ -1,17 +1,26 @@ from django import forms from django.contrib.auth.forms import UserCreationForm -from .models import User +from models import User class SignUpForm(UserCreationForm): - email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.') + + email = forms.EmailField( + max_length=254, help_text="Required. Inform a valid email address." + ) + def clean_username(self): - data = self.cleaned_data['username'] - if not data.islower(): - raise forms.ValidationError("Usernames should be in lowercase") - if not data.isalnum(): - raise forms.ValidationError("Usernames should be alpha numeric and not have special characters.") - return data + data = self.cleaned_data["username"] + if not data.islower(): + raise forms.ValidationError("Usernames should be in lowercase") + if not data.isalnum(): + raise forms.ValidationError( + "Usernames should be alpha numeric and not \ + have special characters." + ) + return data + class Meta: + model = User - fields = ('username', 'email', 'password1', 'password2', ) + fields = ("username", "email", "password1", "password2") From 4172f3afaa87eb4b89a56685b379a31448712901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Wed, 15 Mar 2023 16:31:51 +0100 Subject: [PATCH 3/5] Fix string --- src/apps/profiles/forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/apps/profiles/forms.py b/src/apps/profiles/forms.py index 1898c7046..7b1991d09 100644 --- a/src/apps/profiles/forms.py +++ b/src/apps/profiles/forms.py @@ -15,8 +15,8 @@ def clean_username(self): raise forms.ValidationError("Usernames should be in lowercase") if not data.isalnum(): raise forms.ValidationError( - "Usernames should be alpha numeric and not \ - have special characters." + "Usernames should be alpha numeric and not" + "have special characters." ) return data From d2a662bc4337807840580746c929d1e2785dd0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Wed, 15 Mar 2023 16:50:47 +0100 Subject: [PATCH 4/5] Simply string --- src/apps/profiles/forms.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/apps/profiles/forms.py b/src/apps/profiles/forms.py index 7b1991d09..9f1125d3f 100644 --- a/src/apps/profiles/forms.py +++ b/src/apps/profiles/forms.py @@ -15,8 +15,7 @@ def clean_username(self): raise forms.ValidationError("Usernames should be in lowercase") if not data.isalnum(): raise forms.ValidationError( - "Usernames should be alpha numeric and not" - "have special characters." + "Usernames should not contain special characters." ) return data From 5ba50cfc124f69da8c0c13288a62a832370a9aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Wed, 15 Mar 2023 17:24:58 +0100 Subject: [PATCH 5/5] Fix import --- src/apps/profiles/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apps/profiles/forms.py b/src/apps/profiles/forms.py index 9f1125d3f..c2b0b43cc 100644 --- a/src/apps/profiles/forms.py +++ b/src/apps/profiles/forms.py @@ -1,6 +1,6 @@ from django import forms from django.contrib.auth.forms import UserCreationForm -from models import User +from .models import User class SignUpForm(UserCreationForm):