Skip to content

Commit

Permalink
fix: use mixin instead of base class
Browse files Browse the repository at this point in the history
With base class the inheritance does not always work as expected.
  • Loading branch information
nijel committed Nov 25, 2024
1 parent 712ffdc commit e3ea112
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions weblate_web/crm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -21,37 +21,39 @@
from django.http import HttpRequest


class CRMBaseView(LoginRequiredMixin):
class CRMMixin:
title: str = "CRM"
permission: str | None = None

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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e3ea112

Please sign in to comment.