IdHub/idhub/mixins.py

37 lines
1.1 KiB
Python
Raw Normal View History

2023-10-09 09:44:26 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import views as auth_views
2023-10-09 11:43:34 +00:00
from django.urls import reverse_lazy, resolve
2023-10-09 09:44:26 +00:00
from django.utils.translation import gettext_lazy as _
from django.shortcuts import redirect
2023-10-11 07:52:05 +00:00
class UserView(LoginRequiredMixin):
2023-10-09 09:44:26 +00:00
login_url = "/login/"
2023-10-09 16:38:28 +00:00
wallet = False
2023-10-09 09:44:26 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title,
2023-10-09 11:43:34 +00:00
'subtitle': self.subtitle,
'icon': self.icon,
'section': self.section,
'path': resolve(self.request.path).url_name,
2023-10-09 16:38:28 +00:00
'user': self.request.user,
'wallet': self.wallet,
2023-10-09 09:44:26 +00:00
})
return context
class AdminView(UserView):
2023-10-11 16:08:20 +00:00
def get(self, request, *args, **kwargs):
2023-10-25 15:49:17 +00:00
if not request.user.is_admin:
2023-10-09 09:44:26 +00:00
url = reverse_lazy('idhub:user_dashboard')
return redirect(url)
2024-01-19 09:59:35 +00:00
if self.request.session.get("2fauth"):
return redirect(reverse_lazy("idhub:login"))
2023-10-11 16:08:20 +00:00
return super().get(request, *args, **kwargs)