From 8748219690a6c5c0d2cce36e8641f3005bf31b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Cant=C3=B9?= Date: Tue, 3 Dec 2024 10:35:41 +0100 Subject: [PATCH] add link to dashboard, protect staff views --- src/apps/users/models.py | 3 +++ src/genlab_bestilling/staff/templates/staff/base.html | 2 +- src/genlab_bestilling/staff/urls.py | 2 ++ src/genlab_bestilling/staff/views.py | 7 +++++-- src/templates/core/partials/navigation.html | 7 ++++++- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/apps/users/models.py b/src/apps/users/models.py index d789672..d469b77 100644 --- a/src/apps/users/models.py +++ b/src/apps/users/models.py @@ -34,3 +34,6 @@ def get_absolute_url(self: Self) -> str: """ return reverse("users:detail", kwargs={"pk": self.id}) + + def is_genlab_staff(self) -> bool: + return self.groups.filter(name="genlab").exists() diff --git a/src/genlab_bestilling/staff/templates/staff/base.html b/src/genlab_bestilling/staff/templates/staff/base.html index f17d985..dcae70b 100644 --- a/src/genlab_bestilling/staff/templates/staff/base.html +++ b/src/genlab_bestilling/staff/templates/staff/base.html @@ -5,7 +5,7 @@
Menu
diff --git a/src/genlab_bestilling/staff/urls.py b/src/genlab_bestilling/staff/urls.py index fe454f8..0080dc9 100644 --- a/src/genlab_bestilling/staff/urls.py +++ b/src/genlab_bestilling/staff/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django.views.generic import TemplateView from ..models import AnalysisOrder, EquipmentOrder from .views import ( @@ -14,6 +15,7 @@ app_name = "genlab.staff" urlpatterns = [ + path("", TemplateView.as_view(template_name="staff/base.html"), name="dashboard"), path( "orders/analysis/", AnalysisOrderListView.as_view(), name="order-analysis-list" ), diff --git a/src/genlab_bestilling/staff/views.py b/src/genlab_bestilling/staff/views.py index b4664ac..e0b85bc 100644 --- a/src/genlab_bestilling/staff/views.py +++ b/src/genlab_bestilling/staff/views.py @@ -1,7 +1,7 @@ from typing import Any from django.contrib import messages -from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.http import HttpResponse, HttpResponseRedirect from django.urls import reverse_lazy from django.utils.translation import gettext as _ @@ -16,11 +16,14 @@ from .tables import AnalysisOrderTable, EquipmentOrderTable, SampleTable -class StaffMixin(LoginRequiredMixin): +class StaffMixin(LoginRequiredMixin, UserPassesTestMixin): def get_template_names(self) -> list[str]: names = super().get_template_names() return [name.replace("genlab_bestilling", "staff") for name in names] + def test_func(self): + return self.request.user.is_genlab_staff() + class AnalysisOrderListView(StaffMixin, SingleTableMixin, FilterView): model = AnalysisOrder diff --git a/src/templates/core/partials/navigation.html b/src/templates/core/partials/navigation.html index 73e772d..403b173 100644 --- a/src/templates/core/partials/navigation.html +++ b/src/templates/core/partials/navigation.html @@ -12,8 +12,13 @@
-
+
{% if user.is_authenticated %} + {% if user.is_genlab_staff %} +
+ Staff +
+ {% endif %} {% include 'core/partials/user-dropdown.html' %} {% else %} Login