Add `UserTokenRequiredMixin`.

Create a subclass of `UserPassesTestMixin` that checks user
has an authorized token.
This commit is contained in:
Santiago Lamora 2019-10-30 14:06:55 +01:00
parent c6698ae761
commit 3f07ca7f8a
2 changed files with 24 additions and 4 deletions

View File

@ -1,6 +1,8 @@
from django.contrib.auth.mixins import UserPassesTestMixin
from django.views.generic.base import ContextMixin
from . import get_version
from .auth import SESSION_KEY_TOKEN
class CustomContextMixin(ContextMixin):
@ -12,3 +14,20 @@ class CustomContextMixin(ContextMixin):
})
return context
class UserTokenRequiredMixin(UserPassesTestMixin):
def test_func(self):
"""Check that the user has an authorized token."""
token = self.request.session.get(SESSION_KEY_TOKEN, None)
if token is None:
return False
# initialize orchestra api orm
self.orchestra = api.Orchestra(auth_token=token)
# verify if the token is valid
if self.orchestra.verify_credentials() is None:
return False
return True

View File

@ -1,4 +1,4 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse_lazy
@ -6,12 +6,13 @@ from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.edit import FormView
from . import api, get_version
from .auth import login as auth_login, logout as auth_logout
from .auth import login as auth_login
from .auth import logout as auth_logout
from .forms import LoginForm
from .mixins import CustomContextMixin
from .mixins import CustomContextMixin, UserTokenRequiredMixin
class DashboardView(CustomContextMixin, TemplateView): ## TODO LoginRequiredMixin
class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
template_name = "musician/dashboard.html"