From e89bfdc57252c4b479afe5849f0ab99be9598dfd Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 24 Dec 2020 11:39:27 -0800 Subject: [PATCH] Makes registration user/localname fields more sensible --- bookwyrm/forms.py | 2 +- .../migrations/0030_auto_20201224_1939.py | 19 +++++++++++++++++++ bookwyrm/models/fields.py | 6 ++---- bookwyrm/models/user.py | 8 +++----- .../templates/snippets/register_form.html | 6 +++--- bookwyrm/view_actions.py | 11 ++++++----- 6 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 bookwyrm/migrations/0030_auto_20201224_1939.py diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index 686ac8b1..557cb00b 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -45,7 +45,7 @@ class LoginForm(CustomForm): class RegisterForm(CustomForm): class Meta: model = models.User - fields = ['username', 'email', 'password'] + fields = ['localname', 'email', 'password'] help_texts = {f: None for f in fields} widgets = { 'password': PasswordInput() diff --git a/bookwyrm/migrations/0030_auto_20201224_1939.py b/bookwyrm/migrations/0030_auto_20201224_1939.py new file mode 100644 index 00000000..6de5d37f --- /dev/null +++ b/bookwyrm/migrations/0030_auto_20201224_1939.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.7 on 2020-12-24 19:39 + +import bookwyrm.models.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0029_auto_20201221_2014'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='localname', + field=models.CharField(max_length=255, null=True, unique=True, validators=[bookwyrm.models.fields.validate_localname]), + ), + ] diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 6dd3b496..2649bb59 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -26,11 +26,11 @@ def validate_remote_id(value): ) -def validate_username(value): +def validate_localname(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'), + _('%(value)s is not a valid username'), params={'value': value}, ) @@ -156,7 +156,6 @@ class UsernameField(ActivitypubFieldMixin, models.CharField): _('username'), max_length=150, unique=True, - validators=[validate_username], error_messages={ 'unique': _('A user with that username already exists.'), }, @@ -168,7 +167,6 @@ class UsernameField(ActivitypubFieldMixin, models.CharField): del kwargs['verbose_name'] del kwargs['max_length'] del kwargs['unique'] - del kwargs['validators'] del kwargs['error_messages'] return name, path, args, kwargs diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 30eeffbc..81662587 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -48,7 +48,8 @@ class User(OrderedCollectionPageMixin, AbstractUser): localname = models.CharField( max_length=255, null=True, - unique=True + unique=True, + validators=[fields.validate_localname], ) # name is your display name, which you can change at will name = fields.CharField(max_length=100, null=True, blank=True) @@ -157,10 +158,7 @@ class User(OrderedCollectionPageMixin, AbstractUser): return super().save(*args, **kwargs) # populate fields for local users - self.remote_id = 'https://%s/user/%s' % (DOMAIN, self.username) - self.localname = self.username - self.username = '%s@%s' % (self.username, DOMAIN) - self.actor = self.remote_id + self.remote_id = 'https://%s/user/%s' % (DOMAIN, self.localname) self.inbox = '%s/inbox' % self.remote_id self.shared_inbox = 'https://%s/inbox' % DOMAIN self.outbox = '%s/outbox' % self.remote_id diff --git a/bookwyrm/templates/snippets/register_form.html b/bookwyrm/templates/snippets/register_form.html index d6f5b98f..213ad1e7 100644 --- a/bookwyrm/templates/snippets/register_form.html +++ b/bookwyrm/templates/snippets/register_form.html @@ -1,10 +1,10 @@ {% csrf_token %}
- +
- +
- {% for error in register_form.username.errors %} + {% for error in register_form.localname.errors %}

{{ error | escape }}

{% endfor %}
diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index 78f76218..e126115b 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -66,13 +66,13 @@ def register(request): if not form.is_valid(): errors = True - username = form.data['username'].strip() + localname = form.data['localname'].strip() email = form.data['email'] password = form.data['password'] - # check username and email uniqueness - if models.User.objects.filter(localname=username).first(): - form.add_error('username', 'User with this username already exists') + # check localname and email uniqueness + if models.User.objects.filter(localname=localname).first(): + form.add_error('localname', 'User with this username already exists') errors = True if errors: @@ -82,8 +82,9 @@ def register(request): } return TemplateResponse(request, 'login.html', data) + username = '%s@%s' % (localname, DOMAIN) user = models.User.objects.create_user( - username, email, password, local=True) + username, email, password, localname=localname, local=True) if invite: invite.times_used += 1 invite.save()