refactory

This commit is contained in:
Cayo Puigdefabregas 2023-10-09 11:44:26 +02:00
parent 0d88f03355
commit 7253ed80a7
5 changed files with 57 additions and 50 deletions

27
idhub/mixins.py Normal file
View File

@ -0,0 +1,27 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import views as auth_views
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from django.shortcuts import redirect
class UserView(LoginRequiredMixin, TemplateView):
login_url = "/login/"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title,
})
return context
class AdminView(UserView):
def get(self, request):
if not request.user.is_superuser:
url = reverse_lazy('idhub:user_dashboard')
return redirect(url)
return super().get(request)

View File

@ -17,15 +17,17 @@ Including another URLconf
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from django.views.generic import RedirectView from django.views.generic import RedirectView
from django.urls import path, reverse_lazy from django.urls import path, reverse_lazy
from . import views from .views import LoginView
from .views_admin import AdminDashboardView
from .views_user import UserDashboardView
app_name = 'idhub' app_name = 'idhub'
urlpatterns = [ urlpatterns = [
path("", RedirectView.as_view(url=reverse_lazy('idhub:login'), permanent=False)), path("", RedirectView.as_view(url=reverse_lazy('idhub:login'), permanent=False)),
path('login/', views.LoginView.as_view(), name='login'), path('login/', LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'), path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('user/dashboard/', views.UserDashboardView.as_view(), name='user_dashboard'), path('user/dashboard/', UserDashboardView.as_view(), name='user_dashboard'),
path('admin/dashboard/', views.AdminDashboardView.as_view(), name='admin_dashboard'), path('admin/dashboard/', AdminDashboardView.as_view(), name='admin_dashboard'),
] ]

View File

@ -1,52 +1,6 @@
import logging
import datetime
import json
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views import View
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.edit import DeleteView, FormView
from django.views.generic.list import ListView
from django.contrib.auth import views as auth_views from django.contrib.auth import views as auth_views
from django.contrib.auth.models import User
from django.shortcuts import redirect
logger = logging.getLogger(__name__)
class UserDashboardView(LoginRequiredMixin, TemplateView):
template_name = "idhub/user_dashboard.html"
title = _('Dashboard')
login_url = "/login/"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'title': self.title,
})
return context
class AdminDashboardView(UserDashboardView):
template_name = "idhub/admin_dashboard.html"
def get(self, request):
if not request.user.is_superuser:
url = reverse_lazy('idhub:user_dashboard')
return redirect(url)
return super().get(request)
class LoginView(auth_views.LoginView): class LoginView(auth_views.LoginView):

12
idhub/views_admin.py Normal file
View File

@ -0,0 +1,12 @@
import logging
from django.utils.translation import gettext_lazy as _
from django.urls import reverse_lazy
from django.contrib import messages
from .mixins import AdminView
class AdminDashboardView(AdminView):
template_name = "idhub/admin_dashboard.html"
title = _('Dashboard')

12
idhub/views_user.py Normal file
View File

@ -0,0 +1,12 @@
import logging
from django.utils.translation import gettext_lazy as _
from django.urls import reverse_lazy
from django.contrib import messages
from .mixins import UserView
class UserDashboardView(UserView):
template_name = "idhub/user_dashboard.html"
title = _('Dashboard')