From fdcd9cb42c7f4f3963248847f56f78ed726792e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Tue, 2 Jul 2019 09:34:12 +0200 Subject: [PATCH] Implement login and logout views --- bookmarks/styles/index.scss | 1 + bookmarks/styles/login.scss | 6 ++++ bookmarks/templates/bookmarks/layout.html | 17 +++++---- bookmarks/templates/registration/login.html | 38 +++++++++++++++++++++ bookmarks/views/api.py | 2 ++ bookmarks/views/bookmarks.py | 5 +++ siteroot/settings.py | 4 +++ siteroot/urls.py | 3 ++ 8 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 bookmarks/styles/login.scss create mode 100644 bookmarks/templates/registration/login.html diff --git a/bookmarks/styles/index.scss b/bookmarks/styles/index.scss index 408f411..986cf45 100644 --- a/bookmarks/styles/index.scss +++ b/bookmarks/styles/index.scss @@ -22,3 +22,4 @@ $alternative-color-dark: darken($alternative-color, 5%); @import "util"; @import "shared"; @import "bookmarks"; +@import "login"; diff --git a/bookmarks/styles/login.scss b/bookmarks/styles/login.scss new file mode 100644 index 0000000..e4d04b3 --- /dev/null +++ b/bookmarks/styles/login.scss @@ -0,0 +1,6 @@ +.login-page { + > .columns { + align-items: center; + justify-content: center; + } +} diff --git a/bookmarks/templates/bookmarks/layout.html b/bookmarks/templates/bookmarks/layout.html index 9f9c28c..1f24076 100644 --- a/bookmarks/templates/bookmarks/layout.html +++ b/bookmarks/templates/bookmarks/layout.html @@ -7,7 +7,7 @@ linkdings {# Include SASS styles, files are resolved from bookmarks/styles #} - +
{% block content %} diff --git a/bookmarks/templates/registration/login.html b/bookmarks/templates/registration/login.html new file mode 100644 index 0000000..7c753ba --- /dev/null +++ b/bookmarks/templates/registration/login.html @@ -0,0 +1,38 @@ +{% extends 'bookmarks/layout.html' %} +{% load widget_tweaks %} + +{% block title %}Login{% endblock %} + +{% block content %} + + + + +{% endblock %} diff --git a/bookmarks/views/api.py b/bookmarks/views/api.py index fb8313e..556d972 100644 --- a/bookmarks/views/api.py +++ b/bookmarks/views/api.py @@ -1,8 +1,10 @@ +from django.contrib.auth.decorators import login_required from django.http import JsonResponse from services.website_loader import load_website_metadata +@login_required def website_metadata(request): url = request.GET.get('url') metadata = load_website_metadata(url) diff --git a/bookmarks/views/bookmarks.py b/bookmarks/views/bookmarks.py index 4dde1df..153f107 100644 --- a/bookmarks/views/bookmarks.py +++ b/bookmarks/views/bookmarks.py @@ -1,3 +1,4 @@ +from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.http import HttpResponseRedirect from django.shortcuts import render @@ -10,6 +11,7 @@ from bookmarks.services.bookmarks import create_bookmark, update_bookmark _default_page_size = 30 +@login_required def index(request): page = request.GET.get('page') query_string = request.GET.get('q') @@ -31,6 +33,7 @@ def index(request): return render(request, 'bookmarks/index.html', context) +@login_required def new(request): if request.method == 'POST': form = BookmarkForm(request.POST) @@ -44,6 +47,7 @@ def new(request): return render(request, 'bookmarks/new.html', {'form': form}) +@login_required def edit(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) if request.method == 'POST': @@ -58,6 +62,7 @@ def edit(request, bookmark_id: int): return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id}) +@login_required def remove(request, bookmark_id: int): bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark.delete() diff --git a/siteroot/settings.py b/siteroot/settings.py index fe47206..5a63570 100644 --- a/siteroot/settings.py +++ b/siteroot/settings.py @@ -98,6 +98,10 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] +LOGIN_URL = '/login' +LOGIN_REDIRECT_URL = '/bookmarks' +LOGOUT_REDIRECT_URL = '/login' + # Internationalization # https://docs.djangoproject.com/en/2.2/topics/i18n/ diff --git a/siteroot/urls.py b/siteroot/urls.py index 7029fe7..a52365c 100644 --- a/siteroot/urls.py +++ b/siteroot/urls.py @@ -14,9 +14,12 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin +from django.contrib.auth import views as auth_views from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'), + path('logout/', auth_views.LogoutView.as_view(), name='logout'), path('', include('bookmarks.urls')), ]