From 7253ed80a7c59026b247007f19a788d151c3c268 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 9 Oct 2023 11:44:26 +0200 Subject: [PATCH] refactory --- idhub/mixins.py | 27 ++++++++++++++++++++++++++ idhub/urls.py | 10 ++++++---- idhub/views.py | 46 -------------------------------------------- idhub/views_admin.py | 12 ++++++++++++ idhub/views_user.py | 12 ++++++++++++ 5 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 idhub/mixins.py create mode 100644 idhub/views_admin.py create mode 100644 idhub/views_user.py diff --git a/idhub/mixins.py b/idhub/mixins.py new file mode 100644 index 0000000..72c3992 --- /dev/null +++ b/idhub/mixins.py @@ -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) diff --git a/idhub/urls.py b/idhub/urls.py index 3d9b9ec..add552e 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -17,15 +17,17 @@ Including another URLconf from django.contrib.auth import views as auth_views from django.views.generic import RedirectView 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' urlpatterns = [ 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('user/dashboard/', views.UserDashboardView.as_view(), name='user_dashboard'), - path('admin/dashboard/', views.AdminDashboardView.as_view(), name='admin_dashboard'), + path('user/dashboard/', UserDashboardView.as_view(), name='user_dashboard'), + path('admin/dashboard/', AdminDashboardView.as_view(), name='admin_dashboard'), ] diff --git a/idhub/views.py b/idhub/views.py index c788507..83d7ff3 100644 --- a/idhub/views.py +++ b/idhub/views.py @@ -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.utils.html import format_html 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.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): diff --git a/idhub/views_admin.py b/idhub/views_admin.py new file mode 100644 index 0000000..2809eef --- /dev/null +++ b/idhub/views_admin.py @@ -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') + diff --git a/idhub/views_user.py b/idhub/views_user.py new file mode 100644 index 0000000..7945290 --- /dev/null +++ b/idhub/views_user.py @@ -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') +