From e3ea112df2d7f1bc1089482575c623795a69b5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 25 Nov 2024 14:23:23 +0100 Subject: [PATCH] fix: use mixin instead of base class With base class the inheritance does not always work as expected. --- weblate_web/crm/views.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/weblate_web/crm/views.py b/weblate_web/crm/views.py index d03d60344..c1ca6653a 100644 --- a/weblate_web/crm/views.py +++ b/weblate_web/crm/views.py @@ -2,8 +2,8 @@ from typing import TYPE_CHECKING +from django.conf import settings from django.contrib import messages -from django.contrib.auth.mixins import LoginRequiredMixin from django.core.exceptions import ( PermissionDenied, ) @@ -21,7 +21,7 @@ from django.http import HttpRequest -class CRMBaseView(LoginRequiredMixin): +class CRMMixin: title: str = "CRM" permission: str | None = None @@ -29,29 +29,31 @@ def get_title(self) -> str: return self.title def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) # type: ignore[misc] + context = super().get_context_data(**kwargs) # type:ignore[misc] context["title"] = self.get_title() return context def dispatch(self, request: HttpRequest, *args, **kwargs): + if not request.user.is_authenticated: + return redirect(settings.LOGIN_URL) if not request.user.is_staff: raise PermissionDenied if self.permission is not None and not request.user.has_perm(self.permission): raise PermissionDenied - return super().dispatch(request, *args, **kwargs) + return super().dispatch(request, *args, **kwargs) # type:ignore[misc] -class IndexView(CRMBaseView, TemplateView): +class IndexView(CRMMixin, TemplateView): template_name = "crm/index.html" -class ServiceListView(CRMBaseView, ListView): +class ServiceListView(CRMMixin, ListView): model = Service permission = "weblate_web.view_service" title = "Services" -class ServiceDetailView(CRMBaseView, DetailView): +class ServiceDetailView(CRMMixin, DetailView): model = Service permission = "weblate_web.change_service" title = "Service detail" @@ -81,7 +83,7 @@ def get_context_data(self, **kwargs): return context -class InvoiceListView(CRMBaseView, ListView): +class InvoiceListView(CRMMixin, ListView): model = Invoice permission = "invoices.view_invoice" title = "Invoices"