diff --git a/README.md b/README.md index 79fa245..5742821 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,7 @@ the device front, the number of the first slot (0 or 1, as printed by the vendor), row- or column-wise numbering and the default form factor. With **Save as template for the device type** the layout is stored for the device type; the next device of the same type offers **Apply template** and needs -a single click. Templates are managed under **Plugins > NetBox Utilities > Disk templates**. +a single click. Templates are managed under **Disks > Disk templates** in the main menu. The layout can be changed later with **Change layout**; slots that still hold a disk cannot be removed. @@ -276,17 +276,28 @@ capacity, the usable RAID capacity, spares and failed disks. The warnings panel lists failed disks, expired warranties, warranties ending within `disk_warranty_warning_days` and disks older than `disk_age_warning_years`. -**Lists, search and export.** **Plugins > NetBox Utilities > Disks** lists all disks of all -devices with filters for device, tenant, RAID group, status, type, interface, -form factor, manufacturer, warranty (expired / expiring / valid), *older than -N years* and *with warnings*. The global tenant filter applies to this list. -Export works through NetBox' standard **Export** button. The NetBox global search +**Main menu.** The plugin adds its own **Disks** menu with the entries +**Disks**, **RAID groups** and **Disk templates**. + +**Disks grouped by device.** **Disks > Disks** shows all disks grouped by device. +Each device is a collapsible section whose header shows the device, role, site, +tenant, number of disks, raw capacity, failed disks and warnings, plus a link to +the device's **Disks** tab. The page offers a search (serial number, model, +manufacturer, device), **Only with warnings**, **Show removed** and **Expand +all** / **Collapse all**; the global tenant filter applies here as well. + +**Lists, search and export.** **Table view & export** on that page opens the flat +list of all disks with filters for device, tenant, RAID group, status, type, +interface, form factor, manufacturer, warranty (expired / expiring / valid), +*older than N years* and *with warnings*. The global tenant filter applies to +this list. Export, bulk edit and bulk delete work through NetBox' standard +buttons. The NetBox global search finds disks by serial number, model, manufacturer and firmware and shows the device and slot. The device list offers an optional **Disks** column (used/total, linked to the tab) under **Configure Table**; it runs two queries per row, so enable it only where needed. -**CSV import.** **Plugins > NetBox Utilities > Disks > Import** accepts CSV, JSON or YAML +**CSV import.** **Disks > Disks > Import** accepts CSV, JSON or YAML with these columns: `device` (device name), `slot`, `status`, `disk_type`, `interface`, `form_factor`, `manufacturer`, `model`, `capacity` (e.g. `4 TB`), `serial`, `firmware`, `manufacture_date`, `install_date`, `removal_date`, diff --git a/netbox_utilities/disk_views.py b/netbox_utilities/disk_views.py index 5d24c1b..ae259d1 100644 --- a/netbox_utilities/disk_views.py +++ b/netbox_utilities/disk_views.py @@ -1,8 +1,10 @@ +from itertools import groupby from urllib.parse import urlencode from dcim.models import Device from django.contrib import messages from django.core.exceptions import ValidationError +from django.core.paginator import Paginator from django.db import transaction from django.http import JsonResponse from django.shortcuts import get_object_or_404, redirect, render @@ -294,6 +296,64 @@ class DiskReplaceView(ContentTypePermissionRequiredMixin, View): # +class DiskByDeviceView(ContentTypePermissionRequiredMixin, View): + """All disks grouped by device in collapsible sections; the flat list stays available.""" + + template_name = "netbox_utilities/disk_by_device.html" + # Read by the global tenant filter middleware, which injects tenant_id for this filterset. + filterset = disk_filtersets.DiskFilterSet + devices_per_page = 50 + + def get_required_permission(self): + return "netbox_utilities.view_disk" + + def get(self, request): + queryset = Disk.objects.restrict(request.user, "view").select_related( + "device", "device__role", "device__tenant", "device__site", "raid_group" + ) + params = request.GET.copy() + params.pop("page", None) + show_removed = params.get("removed") == "1" + if not show_removed and not params.getlist("status"): + queryset = queryset.filter(status__in=disks.SLOT_OCCUPYING_STATUSES) + filterset = self.filterset(params, queryset) + queryset = filterset.qs.order_by("device__name", "device_id", "slot", "pk") + + groups = [] + for device, device_disks in groupby(queryset, key=lambda disk: disk.device): + device_disks = list(device_disks) + installed = [disk for disk in device_disks if disk.occupies_slot] + summary = disks.disk_summary(installed, 0) + groups.append( + { + "device": device, + "disks": device_disks, + "count": len(installed), + "raw_capacity": disks.format_capacity(summary["raw_capacity_gb"]), + "failed": summary["failed"], + "warning_count": sum(1 for disk in installed if disk.warnings), + } + ) + + page = Paginator(groups, self.devices_per_page).get_page(request.GET.get("page")) + query = params.urlencode() + return render( + request, + self.template_name, + { + "page": page, + "groups": page.object_list, + "device_count": len(groups), + "disk_count": sum(group["count"] for group in groups), + "q": params.get("q", ""), + "show_removed": show_removed, + "warnings_only": params.get("has_warnings") == "true", + "query": query, + "expand_all": len(page.object_list) == 1, + }, + ) + + @register_model_view(Disk, "list", path="", detail=False) class DiskListView(generic.ObjectListView): queryset = Disk.objects.select_related("device", "raid_group", "tenant") diff --git a/netbox_utilities/navigation.py b/netbox_utilities/navigation.py index 61e29ed..83e89f4 100644 --- a/netbox_utilities/navigation.py +++ b/netbox_utilities/navigation.py @@ -1,29 +1,6 @@ -from netbox.plugins import PluginMenuButton, PluginMenuItem +from netbox.plugins import PluginMenu, PluginMenuButton, PluginMenuItem menu_items = ( - PluginMenuItem( - link="plugins:netbox_utilities:disk_list", - link_text="Datenträger", - permissions=("netbox_utilities.view_disk",), - buttons=( - PluginMenuButton( - link="plugins:netbox_utilities:disk_bulk_import", - title="Importieren", - icon_class="mdi mdi-upload", - permissions=("netbox_utilities.add_disk",), - ), - ), - ), - PluginMenuItem( - link="plugins:netbox_utilities:raidgroup_list", - link_text="RAID-Gruppen", - permissions=("netbox_utilities.view_raidgroup",), - ), - PluginMenuItem( - link="plugins:netbox_utilities:diskbaytemplate_list", - link_text="Datenträger-Vorlagen", - permissions=("netbox_utilities.view_diskbaytemplate",), - ), PluginMenuItem( link="plugins:netbox_utilities:bulk_module_install", link_text="Module mehrfach einbauen", @@ -41,3 +18,39 @@ menu_items = ( staff_only=True, ), ) + +# Own top-level menu for disks; the disk entry opens the list grouped by device. +menu = PluginMenu( + label="Datenträger", + icon_class="mdi mdi-harddisk", + groups=( + ( + "Datenträger", + ( + PluginMenuItem( + link="plugins:netbox_utilities:disk_by_device", + link_text="Datenträger", + permissions=("netbox_utilities.view_disk",), + buttons=( + PluginMenuButton( + link="plugins:netbox_utilities:disk_bulk_import", + title="Importieren", + icon_class="mdi mdi-upload", + permissions=("netbox_utilities.add_disk",), + ), + ), + ), + PluginMenuItem( + link="plugins:netbox_utilities:raidgroup_list", + link_text="RAID-Gruppen", + permissions=("netbox_utilities.view_raidgroup",), + ), + PluginMenuItem( + link="plugins:netbox_utilities:diskbaytemplate_list", + link_text="Datenträger-Vorlagen", + permissions=("netbox_utilities.view_diskbaytemplate",), + ), + ), + ), + ), +) diff --git a/netbox_utilities/static/netbox_utilities/netbox_utilities.css b/netbox_utilities/static/netbox_utilities/netbox_utilities.css index 744f77b..59e8b8c 100644 --- a/netbox_utilities/static/netbox_utilities/netbox_utilities.css +++ b/netbox_utilities/static/netbox_utilities/netbox_utilities.css @@ -381,3 +381,22 @@ a.nbu-disk-slot[draggable="true"] { pointer-events: none; opacity: 0.6; } + +/* Datenträger grouped by device */ +.nbu-disk-group-header { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 0.25rem; + cursor: pointer; + user-select: none; +} + +.nbu-disk-group-chevron { + margin-right: 0.25rem; + transition: transform 0.15s ease-in-out; +} + +.nbu-disk-group-header[aria-expanded="true"] .nbu-disk-group-chevron { + transform: rotate(90deg); +} diff --git a/netbox_utilities/templates/netbox_utilities/disk_by_device.html b/netbox_utilities/templates/netbox_utilities/disk_by_device.html new file mode 100644 index 0000000..06f4cf2 --- /dev/null +++ b/netbox_utilities/templates/netbox_utilities/disk_by_device.html @@ -0,0 +1,129 @@ +{% extends 'base/layout.html' %} +{% load helpers %} + +{% block title %}Datenträger{% endblock %} + +{% block controls %} +
+ + Tabellenansicht & Export + + {% if perms.netbox_utilities.add_disk %} + + Importieren + + {% endif %} +
+{% endblock controls %} + +{% block content %} +
+
+ + + + + {% if query %}Zurücksetzen{% endif %} +
+
+ +
+ {{ disk_count }} Datenträger in {{ device_count }} Gerät{{ device_count|pluralize:"en" }} + {% if groups %} + + + {% endif %} +
+ + {% for group in groups %} + {% with device=group.device %} +
+
+ + {{ device }} + {% if device.role %}{{ device.role }}{% endif %} + {% if device.site %}{{ device.site }}{% endif %}{% if device.tenant %} · {{ device.tenant }}{% endif %} + {{ group.count }} Datenträger + {{ group.raw_capacity }} + {% if group.failed %}{{ group.failed }} defekt{% endif %} + {% if group.warning_count %} {{ group.warning_count }}{% endif %} + + + +
+
+
+ + + + + + + + + {% for disk in group.disks %} + + + + + + + + + + + + {% endfor %} + +
SlotStatusTypHersteller / ModellGrößeSeriennummerRAID-GruppeGarantie bisWarnungen
{{ disk.slot }}{% badge disk.get_status_display bg_color=disk.get_status_color %}{{ disk.get_disk_type_display|placeholder }}{% if disk.interface %} {{ disk.get_interface_display }}{% endif %}{{ disk.manufacturer }} {{ disk.model }}{{ disk.capacity_display }}{{ disk.serial|placeholder }}{{ disk.raid_group|linkify:"name"|placeholder }}{{ disk.warranty_end|isodate|placeholder }}{% for level, text in disk.warnings %}{{ text }}{% empty %}{{ ""|placeholder }}{% endfor %}
+
+
+
+ {% endwith %} + {% empty %} +
Keine Datenträger gefunden.
+ {% endfor %} + + {% if page.has_other_pages %} + + {% endif %} + + +{% endblock content %} diff --git a/netbox_utilities/urls.py b/netbox_utilities/urls.py index 5df5e35..db0c339 100644 --- a/netbox_utilities/urls.py +++ b/netbox_utilities/urls.py @@ -18,6 +18,7 @@ urlpatterns = [ path("settings/", views.UtilitiesSettingsView.as_view(), name="settings"), path("tenant/select/", views.SelectTenantView.as_view(), name="select_tenant"), # Datenträger + path("disks/by-device/", disk_views.DiskByDeviceView.as_view(), name="disk_by_device"), path("disks/", include(get_model_urls("netbox_utilities", "disk", detail=False))), path("disks//", include(get_model_urls("netbox_utilities", "disk"))), path("disks//replace/", disk_views.DiskReplaceView.as_view(), name="disk_replace"),