Adds registration view

This commit is contained in:
Mouse Reeve 2021-01-12 08:19:08 -08:00
parent 6e71ff2aa1
commit 05b4cb59b0
8 changed files with 68 additions and 61 deletions

View file

@ -16,7 +16,7 @@
<div class="tile is-child box has-background-primary-light content">
{% if site.allow_registration %}
<h2 class="title">Join {{ site.name }}</h2>
<form name="register" method="post" action="/user-register">
<form name="register" method="post" action="/register">
{% include 'snippets/register_form.html' %}
</form>
{% else %}

View file

@ -7,7 +7,7 @@
{% if valid %}
<h1 class="title">Create an Account</h1>
<div>
<form name="register" method="post" action="/user-register">
<form name="register" method="post" action="/register">
<input type=hidden name="invite_code" value="{{ invite.code }}">
{% include 'snippets/register_form.html' %}
</form>

View file

@ -122,10 +122,10 @@
</div>
{% else %}
<div class="navbar-item">
{% if request.path != '/login' and request.path != '/login/' and request.path != '/user-login' %}
{% if request.path != '/login' and request.path != '/login/' %}
<div class="columns">
<div class="column">
<form name="login" method="post" action="/user-login">
<form name="login" method="post" action="/login">
{% csrf_token %}
<div class="field is-grouped">
<div class="control">

View file

@ -38,7 +38,7 @@
<div class="box has-background-primary-light">
{% if site.allow_registration %}
<h2 class="title">Create an Account</h2>
<form name="register" method="post" action="/user-register">
<form name="register" method="post" action="/register">
{% include 'snippets/register_form.html' %}
</form>
{% else %}

View file

@ -42,8 +42,10 @@ urlpatterns = [
# TODO: re_path(r'^.well-known/host-meta/?$', incoming.host_meta),
# TODO: robots.txt
# ui vviews
# authentication
re_path(r'^login/?$', views.LoginView.as_view()),
re_path(r'^register/?$', views.RegisterView.as_view()),
re_path(r'^about/?$', vviews.about_page),
re_path(r'^password-reset/?$', vviews.password_reset_request),
re_path(r'^password-reset/(?P<code>[A-Za-z0-9]+)/?$', vviews.password_reset),
@ -91,7 +93,6 @@ urlpatterns = [
# internal action endpoints
re_path(r'^logout/?$', actions.user_logout),
re_path(r'^user-register/?$', actions.register),
re_path(r'^reset-password-request/?$', actions.password_reset_request),
re_path(r'^reset-password/?$', actions.password_reset),
re_path(r'^change-password/?$', actions.password_change),

View file

@ -25,57 +25,6 @@ from bookwyrm.settings import DOMAIN
from bookwyrm.vviews import get_user_from_username, get_edition
@require_POST
def register(request):
''' join the server '''
if not models.SiteSettings.get().allow_registration:
invite_code = request.POST.get('invite_code')
if not invite_code:
raise PermissionDenied
invite = get_object_or_404(models.SiteInvite, code=invite_code)
if not invite.valid():
raise PermissionDenied
else:
invite = None
form = forms.RegisterForm(request.POST)
errors = False
if not form.is_valid():
errors = True
localname = form.data['localname'].strip()
email = form.data['email']
password = form.data['password']
# check localname and email uniqueness
if models.User.objects.filter(localname=localname).first():
form.errors['localname'] = ['User with this username already exists']
errors = True
if errors:
data = {
'login_form': forms.LoginForm(),
'register_form': form,
'invite': invite,
'valid': invite.valid() if invite else True,
}
if invite:
return TemplateResponse(request, 'invite.html', data)
return TemplateResponse(request, 'login.html', data)
username = '%s@%s' % (localname, DOMAIN)
user = models.User.objects.create_user(
username, email, password, localname=localname, local=True)
if invite:
invite.times_used += 1
invite.save()
login(request, user)
return redirect('/')
@login_required
@require_GET
def user_logout(request):

View file

@ -1,2 +1,2 @@
''' make sure all our nice views are available '''
from .authentication import LoginView
from .authentication import LoginView, RegisterView

View file

@ -1,17 +1,20 @@
''' class views for login/register/password management views '''
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils import timezone
from django.views import View
from bookwyrm import forms
from bookwyrm import forms, models
from bookwyrm.settings import DOMAIN
# pylint: disable= no-self-use
class LoginView(View):
''' authenticate an existing user '''
def get(self, request):
''' login page '''
if request.user.is_authenticated:
return redirect('/')
# send user to the login page
@ -23,6 +26,7 @@ class LoginView(View):
return TemplateResponse(request, 'login.html', data)
def post(self, request):
''' authentication action '''
login_form = forms.LoginForm(request.POST)
localname = login_form.data['localname']
@ -43,3 +47,56 @@ class LoginView(View):
'register_form': register_form
}
return TemplateResponse(request, 'login.html', data)
class RegisterView(View):
''' register a user '''
def post(self, request):
''' join the server '''
if not models.SiteSettings.get().allow_registration:
invite_code = request.POST.get('invite_code')
if not invite_code:
raise PermissionDenied
invite = get_object_or_404(models.SiteInvite, code=invite_code)
if not invite.valid():
raise PermissionDenied
else:
invite = None
form = forms.RegisterForm(request.POST)
errors = False
if not form.is_valid():
errors = True
localname = form.data['localname'].strip()
email = form.data['email']
password = form.data['password']
# check localname and email uniqueness
if models.User.objects.filter(localname=localname).first():
form.errors['localname'] = [
'User with this username already exists']
errors = True
if errors:
data = {
'login_form': forms.LoginForm(),
'register_form': form,
'invite': invite,
'valid': invite.valid() if invite else True,
}
if invite:
return TemplateResponse(request, 'invite.html', data)
return TemplateResponse(request, 'login.html', data)
username = '%s@%s' % (localname, DOMAIN)
user = models.User.objects.create_user(
username, email, password, localname=localname, local=True)
if invite:
invite.times_used += 1
invite.save()
login(request, user)
return redirect('/')