Implement login and logout views

This commit is contained in:
Sascha Ißbrücker 2019-07-02 09:34:12 +02:00
parent d2f896966a
commit fdcd9cb42c
8 changed files with 69 additions and 7 deletions

View File

@ -22,3 +22,4 @@ $alternative-color-dark: darken($alternative-color, 5%);
@import "util"; @import "util";
@import "shared"; @import "shared";
@import "bookmarks"; @import "bookmarks";
@import "login";

View File

@ -0,0 +1,6 @@
.login-page {
> .columns {
align-items: center;
justify-content: center;
}
}

View File

@ -7,7 +7,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>linkdings</title> <title>linkdings</title>
{# Include SASS styles, files are resolved from bookmarks/styles #} {# Include SASS styles, files are resolved from bookmarks/styles #}
<link href="{% sass_src 'index.scss' %}" rel="stylesheet" type="text/css" /> <link href="{% sass_src 'index.scss' %}" rel="stylesheet" type="text/css"/>
</head> </head>
<body> <body>
<header class="navbar container grid-lg"> <header class="navbar container grid-lg">
@ -18,12 +18,15 @@
<h1>linkding</h1> <h1>linkding</h1>
</a> </a>
</section> </section>
<section class="navbar-section"> {# Only nav items menu when logged in #}
<a href="{% url 'bookmarks:new' %}" class="btn btn-primary mr-2">Add bookmark</a> {% if request.user.is_authenticated %}
<a href="/bookmarks" class="btn btn-link">Bookmarks</a> <section class="navbar-section">
<a href="/settings" class="btn btn-link">Settings</a> <a href="{% url 'bookmarks:new' %}" class="btn btn-primary mr-2">Add bookmark</a>
<a href="/logout" class="btn btn-link">Logout</a> <a href="/bookmarks" class="btn btn-link">Bookmarks</a>
</section> <a href="/settings" class="btn btn-link">Settings</a>
<a href="/logout" class="btn btn-link">Logout</a>
</section>
{% endif %}
</header> </header>
<div class="content container grid-lg"> <div class="content container grid-lg">
{% block content %} {% block content %}

View File

@ -0,0 +1,38 @@
{% extends 'bookmarks/layout.html' %}
{% load widget_tweaks %}
{% block title %}Login{% endblock %}
{% block content %}
<div class="login-page">
<div class="columns">
<section class="content-area column col-5 col-sm-12">
<div class="content-area-header">
<h2>Login</h2>
</div>
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{% if form.errors %}
<div class="form-group has-error">
<p class="form-input-hint">Your username and password didn't match. Please try again.</p>
</div>
{% endif %}
<div class="form-group">
<label class="form-label" for="{{ form.username.id_for_label }}">Username</label>
{{ form.username|add_class:'form-input' }}
</div>
<div class="form-group">
<label class="form-label" for="{{ form.username.id_for_label }}">Password</label>
{{ form.password|add_class:'form-input' }}
</div>
<input type="submit" value="Login" class="btn btn-primary mt-2 col-sm-12">
<input type="hidden" name="next" value="{{ next }}">
</form>
</section>
</div>
</div>
{% endblock %}

View File

@ -1,8 +1,10 @@
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse from django.http import JsonResponse
from services.website_loader import load_website_metadata from services.website_loader import load_website_metadata
@login_required
def website_metadata(request): def website_metadata(request):
url = request.GET.get('url') url = request.GET.get('url')
metadata = load_website_metadata(url) metadata = load_website_metadata(url)

View File

@ -1,3 +1,4 @@
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
@ -10,6 +11,7 @@ from bookmarks.services.bookmarks import create_bookmark, update_bookmark
_default_page_size = 30 _default_page_size = 30
@login_required
def index(request): def index(request):
page = request.GET.get('page') page = request.GET.get('page')
query_string = request.GET.get('q') query_string = request.GET.get('q')
@ -31,6 +33,7 @@ def index(request):
return render(request, 'bookmarks/index.html', context) return render(request, 'bookmarks/index.html', context)
@login_required
def new(request): def new(request):
if request.method == 'POST': if request.method == 'POST':
form = BookmarkForm(request.POST) form = BookmarkForm(request.POST)
@ -44,6 +47,7 @@ def new(request):
return render(request, 'bookmarks/new.html', {'form': form}) return render(request, 'bookmarks/new.html', {'form': form})
@login_required
def edit(request, bookmark_id: int): def edit(request, bookmark_id: int):
bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark = Bookmark.objects.get(pk=bookmark_id)
if request.method == 'POST': 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}) return render(request, 'bookmarks/edit.html', {'form': form, 'bookmark_id': bookmark_id})
@login_required
def remove(request, bookmark_id: int): def remove(request, bookmark_id: int):
bookmark = Bookmark.objects.get(pk=bookmark_id) bookmark = Bookmark.objects.get(pk=bookmark_id)
bookmark.delete() bookmark.delete()

View File

@ -98,6 +98,10 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
LOGIN_URL = '/login'
LOGIN_REDIRECT_URL = '/bookmarks'
LOGOUT_REDIRECT_URL = '/login'
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/ # https://docs.djangoproject.com/en/2.2/topics/i18n/

View File

@ -14,9 +14,12 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), 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')), path('', include('bookmarks.urls')),
] ]