From 43a36506f0347a2186e6a3afb644179db0e3342b Mon Sep 17 00:00:00 2001
From: Quentin VERMANDE <quentin.vermande@ens.fr>
Date: Thu, 7 Jan 2021 20:09:24 +0100
Subject: [PATCH] fixed logout view and templates

---
 accounts/urls.py                       |  2 +-
 accounts/views.py                      |  4 ++-
 home/templates/base.html               |  7 +++++-
 home/templates/registration/login.html | 35 ++++++++++++++++++++++++++
 home/urls.py                           |  5 ++--
 interludes/settings.py                 |  6 ++++-
 6 files changed, 53 insertions(+), 6 deletions(-)
 create mode 100644 home/templates/registration/login.html

diff --git a/accounts/urls.py b/accounts/urls.py
index 60de325..199c1c4 100644
--- a/accounts/urls.py
+++ b/accounts/urls.py
@@ -1,6 +1,6 @@
 from django.urls import include, path
 import django.contrib.auth.views as dj_auth_views
-from .views import login, logout 
+from .views import logout 
 
 app_name = "accounts"
 
diff --git a/accounts/views.py b/accounts/views.py
index 386c73a..9d3a0ab 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -1,6 +1,8 @@
 from django.shortcuts import render, redirect
+from django.contrib.auth import logout as auth_logout
+from django.contrib.auth.decorators import login_required
 
 @login_required
 def logout(req):
     auth_logout(req)
-    return redirect("home:home")
+    return redirect("home")
diff --git a/home/templates/base.html b/home/templates/base.html
index 47aecd9..9905f46 100644
--- a/home/templates/base.html
+++ b/home/templates/base.html
@@ -40,6 +40,11 @@
 				class={% if slug == "faq" %}"current"{% else %}"internal"{% endif %}>
 				FAQ
 			</a>
+			{% if request.user.is_authenticated %}
+				<a class="logout" href="{% url "accounts:logout" %}">Déconnexion</a>
+			{% else %}
+				<a class="login" href="{% url "accounts:login" %}?next={{ request.get_full_path }}">Connexion</a>
+			{% endif %}
 		</nav>
 
 		<main>
@@ -65,4 +70,4 @@
 		</footer>
 
 	</body>
-</html>
\ No newline at end of file
+</html>
diff --git a/home/templates/registration/login.html b/home/templates/registration/login.html
new file mode 100644
index 0000000..b044e95
--- /dev/null
+++ b/home/templates/registration/login.html
@@ -0,0 +1,35 @@
+{% extends "base.html" %}
+
+{% block "content" %}
+<div id="content-area">
+	{% if form.errors %}
+	<p>Login ou mot de passe incorrect</p>
+	{% endif %}
+
+	{% if next %}
+	{% if user.is_authenticated %}
+	<p>Accès non autorisé.</p>
+	{% else %}
+	<p>Merci de vous connecter.</p>
+	{% endif %}
+	{% endif %}
+
+	<form method="post" action="{% url "accounts:login" %}?next={{ next|urlencode }}">
+		{% csrf_token %}
+		<table>
+			<tr>
+				<td>{{ form.username.label_tag }}</td>
+				<td>{{ form.username }}</td>
+			</tr>
+			<tr>
+				<td>{{ form.password.label_tag }}</td>
+				<td>{{ form.password }}</td>
+			</tr>
+		</table>
+
+		<input type="submit" value="connexion" />
+		<input type="hidden" name="next" value="{{ next }}" />
+	</form>
+</div>
+
+{% endblock %}
diff --git a/home/urls.py b/home/urls.py
index bf87cf7..7d5eb71 100644
--- a/home/urls.py
+++ b/home/urls.py
@@ -1,6 +1,6 @@
 from django.contrib.sitemaps.views import sitemap
 from django.views.generic import RedirectView
-from django.urls import path
+from django.urls import path, include
 from . import views
 
 sitemaps = {"static_pages": views.StaticViewSitemap}
@@ -15,4 +15,5 @@ urlpatterns = [
 		'sitemap.xml', sitemap, {'sitemaps': sitemaps},
 		name='django.contrib.sitemaps.views.sitemap'
 	),
-]
\ No newline at end of file
+        path('accounts/', include("accounts.urls")),
+]
diff --git a/interludes/settings.py b/interludes/settings.py
index 67b227c..57549ae 100644
--- a/interludes/settings.py
+++ b/interludes/settings.py
@@ -38,7 +38,8 @@ INSTALLED_APPS = [
 	'django.contrib.messages',
 	'django.contrib.staticfiles',
 	'django.contrib.sitemaps',
-  'home.apps.HomeConfig',
+        'home.apps.HomeConfig',
+        'accounts',
 ]
 
 MIDDLEWARE = [
@@ -82,6 +83,9 @@ DATABASES = {
 	}
 }
 
+AUTHENTICATION_BACKENDS = (
+    'django.contrib.auth.backends.ModelBackend',
+)
 
 # Password validation
 # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
-- 
GitLab