django-musician/musician/views.py

53 lines
1.5 KiB
Python
Raw Normal View History

from django.http import HttpResponseRedirect
2019-10-10 07:18:34 +00:00
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.edit import FormView
2019-10-10 07:18:34 +00:00
from . import api, get_version
from .auth import login as auth_login
from .auth import logout as auth_logout
from .forms import LoginForm
from .mixins import CustomContextMixin, UserTokenRequiredMixin
2019-10-25 11:33:37 +00:00
class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
2019-10-25 11:33:37 +00:00
template_name = "musician/dashboard.html"
class LoginView(FormView):
template_name = 'auth/login.html'
form_class = LoginForm
success_url = reverse_lazy('musician:dashboard')
extra_context = {'version': get_version()}
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
def form_valid(self, form):
"""Security check complete. Log the user in."""
auth_login(self.request, form.username, form.token)
return HttpResponseRedirect(self.get_success_url())
class LogoutView(RedirectView):
"""
Log out the user.
"""
permanent = False
pattern_name = 'musician:login'
def get_redirect_url(self, *args, **kwargs):
"""
Logs out the user.
"""
auth_logout(self.request)
return super().get_redirect_url(*args, **kwargs)
def post(self, request, *args, **kwargs):
"""Logout may be done via POST."""
return self.get(request, *args, **kwargs)