forked from mirrors/bookwyrm
Makes registration user/localname fields more sensible
This commit is contained in:
parent
f6f075fac6
commit
e89bfdc572
6 changed files with 34 additions and 18 deletions
|
@ -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()
|
||||
|
|
19
bookwyrm/migrations/0030_auto_20201224_1939.py
Normal file
19
bookwyrm/migrations/0030_auto_20201224_1939.py
Normal file
|
@ -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]),
|
||||
),
|
||||
]
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
{% csrf_token %}
|
||||
<div class="field">
|
||||
<label class="label" for="id_username_register">Username:</label>
|
||||
<label class="label" for="id_localname_register">Username:</label>
|
||||
<div class="control">
|
||||
<input type="text" name="username" maxlength="150" class="input" required="" id="id_username_register" value="{% if register_form.username.value %}{{ register_form.username.value }} {% endif %}">
|
||||
<input type="text" name="localname" maxlength="150" class="input" required="" id="id_localname_register" value="{% if register_form.localname.value %}{{ register_form.localname.value }}{% endif %}">
|
||||
</div>
|
||||
{% for error in register_form.username.errors %}
|
||||
{% for error in register_form.localname.errors %}
|
||||
<p class="help is-danger">{{ error | escape }}</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue