add logout view

This commit is contained in:
Jens Langhammer 2018-11-23 09:44:30 +01:00
parent e9e6f632e3
commit 00cf64ef31
3 changed files with 21 additions and 21 deletions

View File

@ -48,7 +48,7 @@
</li> </li>
<li class="divider"></li> <li class="divider"></li>
<li> <li>
<a href="#0">One more separated link</a> <a href="{% url 'passbook_core:auth-logout' %}">{% trans 'Logout' %}</a>
</li> </li>
</ul> </ul>
</li> </li>

View File

@ -15,6 +15,7 @@ admin.site.login = RedirectView.as_view(pattern_name='passbook_core:auth-login')
core_urls = [ core_urls = [
path('auth/login/', authentication.LoginView.as_view(), name='auth-login'), path('auth/login/', authentication.LoginView.as_view(), name='auth-login'),
path('auth/logout/', authentication.LogoutView.as_view(), name='auth-logout'),
path('', overview.OverviewView.as_view(), name='overview'), path('', overview.OverviewView.as_view(), name='overview'),
] ]

View File

@ -3,11 +3,12 @@ from logging import getLogger
from typing import Dict from typing import Dict
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.mixins import UserPassesTestMixin from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect, reverse from django.shortcuts import redirect, reverse
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views import View
from django.views.generic import FormView from django.views.generic import FormView
from passbook.core.forms.authentication import LoginForm from passbook.core.forms.authentication import LoginForm
@ -45,7 +46,7 @@ class LoginView(UserPassesTestMixin, FormView):
pre_user = self.get_user(form.cleaned_data.get('uid_field')) pre_user = self.get_user(form.cleaned_data.get('uid_field'))
if not pre_user: if not pre_user:
# No user found # No user found
return LoginView.invalid_login(self.request) return self.invalid_login(self.request)
user = authenticate( user = authenticate(
email=pre_user.email, email=pre_user.email,
username=pre_user.username, username=pre_user.username,
@ -53,12 +54,11 @@ class LoginView(UserPassesTestMixin, FormView):
request=self.request) request=self.request)
if user: if user:
# User authenticated successfully # User authenticated successfully
return LoginView.login(self.request, user, form.cleaned_data) return self.login(self.request, user, form.cleaned_data)
# User was found but couldn't authenticate # User was found but couldn't authenticate
return LoginView.invalid_login(self.request, disabled_user=pre_user) return self.invalid_login(self.request, disabled_user=pre_user)
@staticmethod def login(self, request: HttpRequest, user: User, cleaned_data: Dict) -> HttpResponse:
def login(request: HttpRequest, user: User, cleaned_data: Dict) -> HttpResponse:
"""Handle actual login """Handle actual login
Actually logs user in, sets session expiry and redirects to ?next parameter Actually logs user in, sets session expiry and redirects to ?next parameter
@ -86,17 +86,16 @@ class LoginView(UserPassesTestMixin, FormView):
# Otherwise just index # Otherwise just index
return redirect(reverse('passbook_core:overview')) return redirect(reverse('passbook_core:overview'))
@staticmethod def invalid_login(self, request: HttpRequest, disabled_user: User = None) -> HttpResponse:
def invalid_login(request: HttpRequest, disabled_user: User = None) -> HttpResponse:
"""Handle login for disabled users/invalid login attempts""" """Handle login for disabled users/invalid login attempts"""
# if disabled_user: messages.error(request, _('Failed to authenticate.'))
# context = { return self.render_to_response(self.get_context_data())
# 'reason': 'disabled',
# 'user': disabled_user class LogoutView(LoginRequiredMixin, View):
# } """Log current user out"""
# else:
# context = { def dispatch(self, request):
# 'reason': 'invalid', """Log current user out"""
# } logout(request)
raise NotImplementedError() messages.success(request, _("You've successfully been logged out."))
# return render(request, 'login/invalid.html', context) return redirect(reverse('passbook_core:auth-login'))