IdHub/idhub/mixins.py
Cayo Puigdefabregas b031f2ac08 admin2fauth
2024-01-19 10:59:35 +01:00

37 lines
1.1 KiB
Python

from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import views as auth_views
from django.urls import reverse_lazy, resolve
from django.utils.translation import gettext_lazy as _
from django.shortcuts import redirect
class UserView(LoginRequiredMixin):
login_url = "/login/"
wallet = False
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title,
'subtitle': self.subtitle,
'icon': self.icon,
'section': self.section,
'path': resolve(self.request.path).url_name,
'user': self.request.user,
'wallet': self.wallet,
})
return context
class AdminView(UserView):
def get(self, request, *args, **kwargs):
if not request.user.is_admin:
url = reverse_lazy('idhub:user_dashboard')
return redirect(url)
if self.request.session.get("2fauth"):
return redirect(reverse_lazy("idhub:login"))
return super().get(request, *args, **kwargs)