Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding rbac option #21

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/version_history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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"
20 changes: 14 additions & 6 deletions src/djangoaddicts/hostutils/views/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand All @@ -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"""
Expand All @@ -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"""
Expand All @@ -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"""
Expand Down
5 changes: 3 additions & 2 deletions src/djangoaddicts/hostutils/views/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tests/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Location: /docs/django_settings.py
"""

from pathlib import Path

import os
Expand All @@ -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 = {
Expand Down Expand Up @@ -54,4 +63,6 @@

USE_TZ = True

MESSAGE_ON_PERMISSION_DENY = True

print(BASE_DIR)
4 changes: 4 additions & 0 deletions tests/core/urls.py
Original file line number Diff line number Diff line change
@@ -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"),

]
10 changes: 7 additions & 3 deletions tests/unit/test_gui.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from django.test import TestCase
from django.test import TestCase, Client
from django.shortcuts import reverse
from unittest.mock import patch
import psutil
import subprocess
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")

Expand Down Expand Up @@ -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()
Expand Down
Loading