Skip to content

Commit

Permalink
Added constraint to signup form - No whitespace in username (#519)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Kresten Laust <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 958fa45 commit fccc11d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions stregsystem/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
19 changes: 19 additions & 0 deletions stregsystem/migrations/0020_alter_member_username.py
Original file line number Diff line number Diff line change
@@ -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+$')]),
),
]
8 changes: 7 additions & 1 deletion stregsystem/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit fccc11d

Please sign in to comment.