From 6e71ff2aa1f94bda74def7d6e64ea2c1a8507d48 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 12 Jan 2021 08:08:43 -0800 Subject: [PATCH 01/34] Adds login class view --- bookwyrm/templates/login.html | 2 +- bookwyrm/urls.py | 75 ++++++++++++++++---------------- bookwyrm/view_actions.py | 26 +---------- bookwyrm/views/__init__.py | 2 + bookwyrm/views/authentication.py | 45 +++++++++++++++++++ bookwyrm/{views.py => vviews.py} | 14 ------ 6 files changed, 86 insertions(+), 78 deletions(-) create mode 100644 bookwyrm/views/__init__.py create mode 100644 bookwyrm/views/authentication.py rename bookwyrm/{views.py => vviews.py} (98%) diff --git a/bookwyrm/templates/login.html b/bookwyrm/templates/login.html index 40d57fa4..87a97f3b 100644 --- a/bookwyrm/templates/login.html +++ b/bookwyrm/templates/login.html @@ -8,7 +8,7 @@ {% if login_form.non_field_errors %}

{{ login_form.non_field_errors }}

{% endif %} -
+ {% csrf_token %}
diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 3abcac11..ce0e26b7 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -3,7 +3,7 @@ from django.conf.urls.static import static from django.contrib import admin from django.urls import path, re_path -from bookwyrm import incoming, outgoing, views, settings, wellknown +from bookwyrm import incoming, outgoing, settings, vviews, views, wellknown from bookwyrm import view_actions as actions from bookwyrm.utils import regex @@ -23,8 +23,8 @@ status_path = r'%s/(%s)/(?P\d+)' % \ book_path = r'^book/(?P\d+)' -handler404 = 'bookwyrm.views.not_found_page' -handler500 = 'bookwyrm.views.server_error_page' +handler404 = 'bookwyrm.vviews.not_found_page' +handler500 = 'bookwyrm.vviews.server_error_page' urlpatterns = [ path('admin/', admin.site.urls), @@ -42,56 +42,55 @@ urlpatterns = [ # TODO: re_path(r'^.well-known/host-meta/?$', incoming.host_meta), # TODO: robots.txt - # ui views - re_path(r'^login/?$', views.login_page), - re_path(r'^about/?$', views.about_page), - re_path(r'^password-reset/?$', views.password_reset_request), - re_path(r'^password-reset/(?P[A-Za-z0-9]+)/?$', views.password_reset), - re_path(r'^invite/?$', views.manage_invites), - re_path(r'^invite/(?P[A-Za-z0-9]+)/?$', views.invite_page), + # ui vviews + re_path(r'^login/?$', views.LoginView.as_view()), + re_path(r'^about/?$', vviews.about_page), + re_path(r'^password-reset/?$', vviews.password_reset_request), + re_path(r'^password-reset/(?P[A-Za-z0-9]+)/?$', vviews.password_reset), + re_path(r'^invite/?$', vviews.manage_invites), + re_path(r'^invite/(?P[A-Za-z0-9]+)/?$', vviews.invite_page), - path('', views.home), - re_path(r'^(?Phome|local|federated)/?$', views.home_tab), - re_path(r'^discover/?$', views.discover_page), - re_path(r'^notifications/?$', views.notifications_page), - re_path(r'^direct-messages/?$', views.direct_messages_page), - re_path(r'^import/?$', views.import_page), - re_path(r'^import-status/(\d+)/?$', views.import_status), - re_path(r'^user-edit/?$', views.edit_profile_page), + path('', vviews.home), + re_path(r'^(?Phome|local|federated)/?$', vviews.home_tab), + re_path(r'^discover/?$', vviews.discover_page), + re_path(r'^notifications/?$', vviews.notifications_page), + re_path(r'^direct-messages/?$', vviews.direct_messages_page), + re_path(r'^import/?$', vviews.import_page), + re_path(r'^import-status/(\d+)/?$', vviews.import_status), + re_path(r'^user-edit/?$', vviews.edit_profile_page), # should return a ui view or activitypub json blob as requested # users - re_path(r'%s/?$' % user_path, views.user_page), - re_path(r'%s\.json$' % local_user_path, views.user_page), - re_path(r'%s/?$' % local_user_path, views.user_page), - re_path(r'%s/shelves/?$' % local_user_path, views.user_shelves_page), - re_path(r'%s/followers(.json)?/?$' % local_user_path, views.followers_page), - re_path(r'%s/following(.json)?/?$' % local_user_path, views.following_page), + re_path(r'%s/?$' % user_path, vviews.user_page), + re_path(r'%s\.json$' % local_user_path, vviews.user_page), + re_path(r'%s/?$' % local_user_path, vviews.user_page), + re_path(r'%s/shelves/?$' % local_user_path, vviews.user_shelves_page), + re_path(r'%s/followers(.json)?/?$' % local_user_path, vviews.followers_page), + re_path(r'%s/following(.json)?/?$' % local_user_path, vviews.following_page), # statuses - re_path(r'%s(.json)?/?$' % status_path, views.status_page), - re_path(r'%s/activity/?$' % status_path, views.status_page), - re_path(r'%s/replies(.json)?/?$' % status_path, views.replies_page), + re_path(r'%s(.json)?/?$' % status_path, vviews.status_page), + re_path(r'%s/activity/?$' % status_path, vviews.status_page), + re_path(r'%s/replies(.json)?/?$' % status_path, vviews.replies_page), # books - re_path(r'%s(.json)?/?$' % book_path, views.book_page), - re_path(r'%s/edit/?$' % book_path, views.edit_book_page), - re_path(r'^author/(?P[\w\-]+)/edit/?$', views.edit_author_page), - re_path(r'%s/editions(.json)?/?$' % book_path, views.editions_page), + re_path(r'%s(.json)?/?$' % book_path, vviews.book_page), + re_path(r'%s/edit/?$' % book_path, vviews.edit_book_page), + re_path(r'^author/(?P[\w\-]+)/edit/?$', vviews.edit_author_page), + re_path(r'%s/editions(.json)?/?$' % book_path, vviews.editions_page), - re_path(r'^author/(?P[\w\-]+)(.json)?/?$', views.author_page), - re_path(r'^tag/(?P.+)\.json/?$', views.tag_page), - re_path(r'^tag/(?P.+)/?$', views.tag_page), + re_path(r'^author/(?P[\w\-]+)(.json)?/?$', vviews.author_page), + re_path(r'^tag/(?P.+)\.json/?$', vviews.tag_page), + re_path(r'^tag/(?P.+)/?$', vviews.tag_page), re_path(r'^%s/shelf/(?P[\w-]+)(.json)?/?$' % \ - user_path, views.shelf_page), + user_path, vviews.shelf_page), re_path(r'^%s/shelf/(?P[\w-]+)(.json)?/?$' % \ - local_user_path, views.shelf_page), + local_user_path, vviews.shelf_page), - re_path(r'^search/?$', views.search), + re_path(r'^search/?$', vviews.search), # internal action endpoints re_path(r'^logout/?$', actions.user_logout), - re_path(r'^user-login/?$', actions.user_login), 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), diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index eff70ff6..249e6270 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -22,31 +22,7 @@ from bookwyrm.connectors import connector_manager from bookwyrm.broadcast import broadcast from bookwyrm.emailing import password_reset_email from bookwyrm.settings import DOMAIN -from bookwyrm.views import get_user_from_username, get_edition - - -@require_POST -def user_login(request): - ''' authenticate user login ''' - login_form = forms.LoginForm(request.POST) - - localname = login_form.data['localname'] - username = '%s@%s' % (localname, DOMAIN) - password = login_form.data['password'] - user = authenticate(request, username=username, password=password) - if user is not None: - # successful login - login(request, user) - user.last_active_date = timezone.now() - return redirect(request.GET.get('next', '/')) - - login_form.non_field_errors = 'Username or password are incorrect' - register_form = forms.RegisterForm() - data = { - 'login_form': login_form, - 'register_form': register_form - } - return TemplateResponse(request, 'login.html', data) +from bookwyrm.vviews import get_user_from_username, get_edition @require_POST diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py new file mode 100644 index 00000000..0a5d9900 --- /dev/null +++ b/bookwyrm/views/__init__.py @@ -0,0 +1,2 @@ +''' make sure all our nice views are available ''' +from .authentication import LoginView diff --git a/bookwyrm/views/authentication.py b/bookwyrm/views/authentication.py new file mode 100644 index 00000000..b789e9cf --- /dev/null +++ b/bookwyrm/views/authentication.py @@ -0,0 +1,45 @@ +''' class views for login/register/password management views ''' +from django.contrib.auth import authenticate, login +from django.shortcuts import redirect +from django.template.response import TemplateResponse +from django.utils import timezone +from django.views import View + +from bookwyrm import forms +from bookwyrm.settings import DOMAIN + + +class LoginView(View): + ''' authenticate an existing user ''' + def get(self, request): + if request.user.is_authenticated: + return redirect('/') + # send user to the login page + data = { + 'title': 'Login', + 'login_form': forms.LoginForm(), + 'register_form': forms.RegisterForm(), + } + return TemplateResponse(request, 'login.html', data) + + def post(self, request): + login_form = forms.LoginForm(request.POST) + + localname = login_form.data['localname'] + username = '%s@%s' % (localname, DOMAIN) + password = login_form.data['password'] + user = authenticate(request, username=username, password=password) + if user is not None: + # successful login + login(request, user) + user.last_active_date = timezone.now() + return redirect(request.GET.get('next', '/')) + + # login errors + login_form.non_field_errors = 'Username or password are incorrect' + register_form = forms.RegisterForm() + data = { + 'login_form': login_form, + 'register_form': register_form + } + return TemplateResponse(request, 'login.html', data) diff --git a/bookwyrm/views.py b/bookwyrm/vviews.py similarity index 98% rename from bookwyrm/views.py rename to bookwyrm/vviews.py index faacd2ff..ebae3696 100644 --- a/bookwyrm/views.py +++ b/bookwyrm/vviews.py @@ -323,20 +323,6 @@ def import_status(request, job_id): }) -@require_GET -def login_page(request): - ''' authentication ''' - if request.user.is_authenticated: - return redirect('/') - # send user to the login page - data = { - 'title': 'Login', - 'login_form': forms.LoginForm(), - 'register_form': forms.RegisterForm(), - } - return TemplateResponse(request, 'login.html', data) - - @require_GET def about_page(request): ''' more information about the instance ''' From 05b4cb59b0b28d92ded07e6d34a62abc2fd35131 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 12 Jan 2021 08:19:08 -0800 Subject: [PATCH 02/34] Adds registration view --- bookwyrm/templates/discover.html | 2 +- bookwyrm/templates/invite.html | 2 +- bookwyrm/templates/layout.html | 4 +-- bookwyrm/templates/login.html | 2 +- bookwyrm/urls.py | 5 +-- bookwyrm/view_actions.py | 51 -------------------------- bookwyrm/views/__init__.py | 2 +- bookwyrm/views/authentication.py | 61 ++++++++++++++++++++++++++++++-- 8 files changed, 68 insertions(+), 61 deletions(-) diff --git a/bookwyrm/templates/discover.html b/bookwyrm/templates/discover.html index 79e31f56..2ff091f0 100644 --- a/bookwyrm/templates/discover.html +++ b/bookwyrm/templates/discover.html @@ -16,7 +16,7 @@
{% if site.allow_registration %}

Join {{ site.name }}

- + {% include 'snippets/register_form.html' %} {% else %} diff --git a/bookwyrm/templates/invite.html b/bookwyrm/templates/invite.html index 458ce3df..3345424c 100644 --- a/bookwyrm/templates/invite.html +++ b/bookwyrm/templates/invite.html @@ -7,7 +7,7 @@ {% if valid %}

Create an Account

-
+ {% include 'snippets/register_form.html' %}
diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index fe448585..edd43f97 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -122,10 +122,10 @@
{% else %}