From 5ac3790588351bbb6e056653e99122f8963039b4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 21 Nov 2023 15:20:15 +0100 Subject: [PATCH] fix login admin --- idhub/views.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/idhub/views.py b/idhub/views.py index 2bfc075..53db736 100644 --- a/idhub/views.py +++ b/idhub/views.py @@ -1,6 +1,8 @@ from django.urls import reverse_lazy from django.utils.translation import gettext_lazy as _ from django.contrib.auth import views as auth_views +from django.contrib.auth import login as auth_login +from django.http import HttpResponseRedirect class LoginView(auth_views.LoginView): @@ -11,10 +13,16 @@ class LoginView(auth_views.LoginView): } def get(self, request, *args, **kwargs): - if request.user.is_admin: - self.extra_context['success_url'] = reverse_lazy( - 'idhub:admin_dashboard' - ) if request.GET.get('next'): self.extra_context['success_url'] = request.GET.get('next') return super().get(request, *args, **kwargs) + + def form_valid(self, form): + user = form.get_user() + if not user.is_anonymous and user.is_admin: + user_dashboard = reverse_lazy('idhub:user_dashboard') + admin_dashboard = reverse_lazy('idhub:admin_dashboard') + if self.extra_context['success_url'] == user_dashboard: + self.extra_context['success_url'] = admin_dashboard + auth_login(self.request, user) + return HttpResponseRedirect(self.extra_context['success_url'])