diff --git a/docs/source/version_history.rst b/docs/source/version_history.rst index 3547d14..be4ee5f 100644 --- a/docs/source/version_history.rst +++ b/docs/source/version_history.rst @@ -20,3 +20,4 @@ Version History "0.0.10", "updating template css" "0.0.11", "updated unittest for process view; fix release tag" "0.0.12", "updated card height" + "0.0.13", "adding RBAC option for views" diff --git a/src/djangoaddicts/hostutils/views/gui.py b/src/djangoaddicts/hostutils/views/gui.py index 8e40716..68e5461 100644 --- a/src/djangoaddicts/hostutils/views/gui.py +++ b/src/djangoaddicts/hostutils/views/gui.py @@ -2,18 +2,21 @@ import psutil from dateutil.relativedelta import relativedelta +from django.conf import settings from django.shortcuts import render from django.views.generic import View +from handyhelpers.permissions import InAnyGroup # import forms from djangoaddicts.hostutils.forms import HostProcessFilterForm -class ShowHost(View): +class ShowHost(InAnyGroup, View): """Display dashboard like page showing an overview of host data""" template_name = "hostutils/bs5/detail/detail_host.html" title = "Host Dashboard" + permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])} def get(self, request, *args, **kwargs): """allow get method""" @@ -41,11 +44,12 @@ def get(self, request, *args, **kwargs): return render(request, self.template_name, context=context) -class ShowHostCpu(View): +class ShowHostCpu(InAnyGroup, View): """Display dashboard like page showing host cpu data""" template_name = "hostutils/bs5/detail/cpu.html" title = "CPU Dashboard" + permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])} def get(self, request, *args, **kwargs): """CPU Dashboard""" @@ -76,11 +80,12 @@ def get(self, request, *args, **kwargs): return render(request, self.template_name, context=context) -class ShowHostDisk(View): +class ShowHostDisk(InAnyGroup, View): """Display dashboard like page showing host disk data""" template_name = "hostutils/bs5/detail/disk.html" title = "Disk Dashboard" + permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])} def get(self, request, *args, **kwargs): """allow get method""" @@ -93,11 +98,12 @@ def get(self, request, *args, **kwargs): return render(request, self.template_name, context=context) -class ShowHostMemory(View): +class ShowHostMemory(InAnyGroup, View): """Display dashboard like page showing host memory data""" template_name = "hostutils/bs5/detail/memory.html" title = "Memory Dashboard" + permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])} def get(self, request, *args, **kwargs): """allow get method""" @@ -109,11 +115,12 @@ def get(self, request, *args, **kwargs): return render(request, self.template_name, context=context) -class ShowHostNetwork(View): +class ShowHostNetwork(InAnyGroup, View): """Display dashboard like page showing host network data""" template_name = "hostutils/bs5/detail/network.html" title = "Network Dashboard" + permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])} def get(self, request, *args, **kwargs): """allow get method""" @@ -127,11 +134,12 @@ def get(self, request, *args, **kwargs): return render(request, self.template_name, context=context) -class ShowHostProcesses(View): +class ShowHostProcesses(InAnyGroup, View): """Display dashboard like page showing host process data""" template_name = "hostutils/bs5/detail/processes.html" title = "Process Dashboard" + permission_dict = {"GET": getattr(settings, "DJA_HOSTUTILS_PERMISSIONS", [])} def get(self, request, *args, **kwargs): """allow get method""" diff --git a/src/djangoaddicts/hostutils/views/htmx.py b/src/djangoaddicts/hostutils/views/htmx.py index 2103e0c..98e171e 100644 --- a/src/djangoaddicts/hostutils/views/htmx.py +++ b/src/djangoaddicts/hostutils/views/htmx.py @@ -20,14 +20,15 @@ class GetHostCpuStats(BuildBootstrapModalView): def get(self, request, *args, **kwargs): context = {} - self.modal_subtitle = kwargs["cpu"] + self.modal_subtitle = kwargs.get("cpu", None) try: - cpu = kwargs["cpu"] + cpu = kwargs.get("cpu", None) context = { "time": psutil.cpu_times(percpu=True)[cpu], "time_percent": psutil.cpu_times_percent(percpu=True)[cpu], "frequency": psutil.cpu_freq(percpu=True)[cpu], } + print('TEST: ', kwargs) except IndexError: return HttpResponse("Invalid request", status=400) self.modal_body = loader.render_to_string("hostutils/bs5/htmx/get_cpu_stats.htm", context=context) diff --git a/tests/core/settings.py b/tests/core/settings.py index 3c39fd5..d558650 100644 --- a/tests/core/settings.py +++ b/tests/core/settings.py @@ -3,6 +3,7 @@ Location: /docs/django_settings.py """ + from pathlib import Path import os @@ -18,10 +19,18 @@ INSTALLED_APPS = [ "django.contrib.auth", "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", "handyhelpers", "djangoaddicts.hostutils", ] +MIDDLEWARE = [ + "django.contrib.sessions.middleware.SessionMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", +] + ROOT_URLCONF = "tests.core.urls" DATABASES = { @@ -54,4 +63,6 @@ USE_TZ = True +MESSAGE_ON_PERMISSION_DENY = True + print(BASE_DIR) diff --git a/tests/core/urls.py b/tests/core/urls.py index 28d933f..d333c44 100644 --- a/tests/core/urls.py +++ b/tests/core/urls.py @@ -1,8 +1,12 @@ +from django.http import HttpResponse from django.urls import path, include + urlpatterns = [ path( "hostutils/", include("djangoaddicts.hostutils.urls"), ), + path('', lambda request: HttpResponse('hello world', content_type='text/plain'), name="home"), + ] diff --git a/tests/unit/test_gui.py b/tests/unit/test_gui.py index ccad831..7effde6 100644 --- a/tests/unit/test_gui.py +++ b/tests/unit/test_gui.py @@ -1,4 +1,4 @@ -from django.test import TestCase +from django.test import TestCase, Client from django.shortcuts import reverse from unittest.mock import patch import psutil @@ -6,13 +6,16 @@ import itertools + class ShowHostCpuTests(TestCase): """test ShowHostCpu view""" def test_get(self): """verify page can be rendered""" - url = reverse("hostutils:host_cpu") - response = self.client.get(url) + referrer = reverse("home") + url = reverse("hostutils:host_cpu") + + response = self.client.get(url, HTTP_REFERER=referrer) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "hostutils/bs5/detail/cpu.html") @@ -73,6 +76,7 @@ def test_get(self): def test_get_with_invalid(self): """verify page is redered if psutil.NoSuchProcess is raised""" + url = reverse("hostutils:host_process") with patch("psutil.process_iter") as mocked_process_list: process_list = psutil.process_iter()