Better username validator and remove trailing whitespace

This commit is contained in:
Mouse Reeve 2020-12-15 15:52:22 -08:00
parent 9d7ad3c492
commit 710fbc949b
2 changed files with 10 additions and 3 deletions

View file

@ -5,7 +5,6 @@ from uuid import uuid4
import dateutil.parser
from dateutil.parser import ParserError
from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import ArrayField as DjangoArrayField
from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
@ -25,6 +24,14 @@ def validate_remote_id(value):
params={'value': value},
)
def validate_username(value):
''' make sure usernames look okay '''
if not re.match(r'^[A-Za-z\-_\.]+$', value):
raise ValidationError(
_('%(value)s is not a valid remote_id'),
params={'value': value},
)
class ActivitypubFieldMixin:
''' make a database field serializable '''
@ -134,7 +141,7 @@ class UsernameField(ActivitypubFieldMixin, models.CharField):
_('username'),
max_length=150,
unique=True,
validators=[AbstractUser.username_validator],
validators=[validate_username],
error_messages={
'unique': _('A user with that username already exists.'),
},

View file

@ -66,7 +66,7 @@ def register(request):
if not form.is_valid():
errors = True
username = form.data['username']
username = form.data['username'].strip()
email = form.data['email']
password = form.data['password']