From fccc11dbaba55491c26ae16394e43b976b6da5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20J=C3=B8nsson?= <67438093+pimber@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:49:24 +0200 Subject: [PATCH] Added constraint to signup form - No whitespace in username (#519) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added constraint to username so that no whitespaces can be in the username. Django handles trailing and leading whitespaces it selfs, so that '-test10' all leadingwhite spaces is ingored by django when inserted. The same with 'test10-' all trailing is also ingored * fixup! Added constraint to username so that no whitespaces can be in the username. Django handles trailing and leading whitespaces it selfs, so that '-test10' all leadingwhite spaces is ingored by django when inserted. The same with 'test10-' all trailing is also ingored * Update comment --------- Co-authored-by: Christoffer Trebbien Jønsson Co-authored-by: Kresten Laust --- stregsystem/forms.py | 1 + .../migrations/0020_alter_member_username.py | 19 +++++++++++++++++++ stregsystem/models.py | 8 +++++++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 stregsystem/migrations/0020_alter_member_username.py diff --git a/stregsystem/forms.py b/stregsystem/forms.py index 55e225ce..65e16b94 100644 --- a/stregsystem/forms.py +++ b/stregsystem/forms.py @@ -108,6 +108,7 @@ class Meta: 'username': { 'required': 'Udfyldning af `Brugernavn` er påkrævet.', 'max_length': 'Længden af `Brugernavn` må ikke overstige 16 tegn.', + 'invalid_username': 'Det indtastede `Brugernavn` må ikke indholde whitespaces', }, 'email': { 'required': 'Udfyldning af `E-Mail` er påkrævet.', diff --git a/stregsystem/migrations/0020_alter_member_username.py b/stregsystem/migrations/0020_alter_member_username.py new file mode 100644 index 00000000..68323473 --- /dev/null +++ b/stregsystem/migrations/0020_alter_member_username.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1.13 on 2024-10-24 19:07 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stregsystem', '0019_theme'), + ] + + operations = [ + migrations.AlterField( + model_name='member', + name='username', + field=models.CharField(max_length=16, validators=[django.core.validators.RegexValidator(code='invalid_username', regex='^\\S+$')]), + ), + ] diff --git a/stregsystem/models.py b/stregsystem/models.py index a1cb808f..ec72bbac 100644 --- a/stregsystem/models.py +++ b/stregsystem/models.py @@ -163,7 +163,13 @@ class Member(models.Model): # id automatisk... ('F', 'Female'), ) active = models.BooleanField(default=True) - username = models.CharField(max_length=16) + + no_whitespace_validator = RegexValidator( + # This regex checks for whitespace in the username + regex=r'^\S+$', + code='invalid_username', + ) + username = models.CharField(max_length=16, validators=[no_whitespace_validator]) year = models.CharField(max_length=4, default=get_current_year) # Put the current year as default firstname = models.CharField(max_length=20) # for 'firstname' lastname = models.CharField(max_length=30) # for 'lastname'