diff --git a/README.md b/README.md index ba3f035..ffe7bd0 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ sudo systemctl restart netbox netbox-rq | `switch_ports_enabled` | `True` | Front panel and **Export switch port usage** button for switches | | `switch_device_roles` | `["Switch"]` | Device roles (case-insensitive) that get the front panel and export button | | `software_enabled` | `True` | Client software overview and **Software** device tab (requires netbox-slm) | -| `software_device_roles` | `["Clients"]` | Device roles (case-insensitive) that show the **Software** tab and are pre-selected in the overview | +| `software_device_roles` | `["Client", "Windows Client"]` | Device roles (name or slug, case-insensitive) shown in the client software overview and given the **Software** tab | | `wireless_trace_enabled` | `True` | Cable trace continues across wireless links (radio bridges) to the cabled port of the peer radio | ## Usage @@ -526,8 +526,9 @@ Software Übersicht* (in the menu of netbox-inventory, otherwise in the Clicking a product shows how many clients run each version and the list of clients; clicking a version filters that list. The current selection can be exported as CSV. Without an own selection the global tenant filter applies. -The device role filter is pre-set to `software_device_roles`; clearing it -includes all devices. +Only devices with a role from `software_device_roles` (name or slug, e.g. +`Windows Client` / `windows-client`) are included; servers and other devices +never appear. The role checkboxes narrow the selection to single client roles. Software lists of the **NetBox Windows Agent** (mode `netbox`) and the **NetBox Synchronizer** desktop app (agent scan) arrive as an inventory report diff --git a/netbox_utilities/__init__.py b/netbox_utilities/__init__.py index b37fb30..bd861bf 100644 --- a/netbox_utilities/__init__.py +++ b/netbox_utilities/__init__.py @@ -1,6 +1,6 @@ from netbox.plugins import PluginConfig, get_plugin_config -__version__ = "0.29.0" +__version__ = "0.29.1" class NetBoxUtilitiesConfig(PluginConfig): @@ -33,7 +33,7 @@ class NetBoxUtilitiesConfig(PluginConfig): "switch_device_roles": ["Switch"], "wireless_trace_enabled": True, "software_enabled": True, - "software_device_roles": ["Clients"], + "software_device_roles": ["Client", "Windows Client"], } middleware = [ "netbox_utilities.middleware.GlobalTenantFilterMiddleware", diff --git a/netbox_utilities/software.py b/netbox_utilities/software.py index 77bcbff..40ac348 100644 --- a/netbox_utilities/software.py +++ b/netbox_utilities/software.py @@ -4,7 +4,7 @@ import re from dataclasses import dataclass, field from datetime import date -DEFAULT_SOFTWARE_DEVICE_ROLES = ("Clients",) +DEFAULT_SOFTWARE_DEVICE_ROLES = ("Client", "Windows Client") # Number of products in the "top products" chart. TOP_PRODUCTS = 10 @@ -20,10 +20,21 @@ def configured_roles(roles=None): return {str(role).casefold() for role in (roles if roles is not None else DEFAULT_SOFTWARE_DEVICE_ROLES)} +def is_client_role(role, roles=None): + """A role counts as client role if its name or slug is configured ("Windows Client" or "windows-client").""" + if role is None: + return False + configured = configured_roles(roles) + slugs = {name.replace(" ", "-") for name in configured} + return ( + str(getattr(role, "name", "")).casefold() in configured + or str(getattr(role, "slug", "")).casefold() in configured | slugs + ) + + def supports_software(device, roles=None): """Devices with a configured client role get the "Software" tab.""" - role = getattr(device, "role", None) - return bool(role and str(getattr(role, "name", "")).casefold() in configured_roles(roles)) + return is_client_role(getattr(device, "role", None), roles) def version_key(name): diff --git a/netbox_utilities/software_views.py b/netbox_utilities/software_views.py index e3424f7..0412fce 100644 --- a/netbox_utilities/software_views.py +++ b/netbox_utilities/software_views.py @@ -39,8 +39,11 @@ def installation_model(): def client_role_ids(): - roles = software.configured_roles() - return [role.pk for role in DeviceRole.objects.all() if role.name.casefold() in roles] + return [role.pk for role in DeviceRole.objects.all() if software.is_client_role(role)] + + +def client_roles(): + return DeviceRole.objects.filter(pk__in=client_role_ids()).order_by("name") class SoftwareOverviewFilterForm(forms.Form): @@ -53,9 +56,19 @@ class SoftwareOverviewFilterForm(forms.Form): label="Mandant", query_params={"group_id": "$tenant_group_id"}, ) - role_id = DynamicModelMultipleChoiceField(queryset=DeviceRole.objects.all(), required=False, label="Gerätrolle") + # Only client roles: servers and other devices never appear in the overview. + role_id = forms.ModelMultipleChoiceField( + queryset=DeviceRole.objects.none(), + required=False, + label="Gerätrolle", + widget=forms.CheckboxSelectMultiple(attrs={"class": "form-check-input"}), + ) q = forms.CharField(required=False, label="Software suchen") + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields["role_id"].queryset = client_roles() + def group_tenant_ids(groups): group_ids = set() @@ -74,10 +87,7 @@ class SoftwareOverviewView(ContentTypePermissionRequiredMixin, View): def get(self, request): params = request.GET.copy() - submitted = params.pop("submitted", None) is not None - if not submitted and "role_id" not in params: - # First visit: clients only. An emptied role field afterwards means all roles. - params.setlist("role_id", [str(pk) for pk in client_role_ids()]) + params.pop("submitted", None) form = SoftwareOverviewFilterForm(params) form.is_valid() data = getattr(form, "cleaned_data", {}) @@ -86,8 +96,8 @@ class SoftwareOverviewView(ContentTypePermissionRequiredMixin, View): tenant_ids, scope_label = self.selected_tenant_ids(request, data) if tenant_ids is not None: devices = devices.filter(tenant_id__in=tenant_ids) - if roles := data.get("role_id"): - devices = devices.filter(role__in=roles) + # Always clients only; no role ticked means all client roles. + devices = devices.filter(role__in=data.get("role_id") or client_role_ids()) installations = ( installation_model() diff --git a/netbox_utilities/templates/netbox_utilities/software_overview.html b/netbox_utilities/templates/netbox_utilities/software_overview.html index ca33a39..3e4faa8 100644 --- a/netbox_utilities/templates/netbox_utilities/software_overview.html +++ b/netbox_utilities/templates/netbox_utilities/software_overview.html @@ -33,8 +33,15 @@ {{ form.tenant_id }}
- - {{ form.role_id }} + Gerätrolle + {% for choice in form.role_id %} +
+ {{ choice.tag }} + +
+ {% empty %} +
Keine Client-Rolle gefunden (software_device_roles)
+ {% endfor %}
diff --git a/netbox_utilities/tests/test_software.py b/netbox_utilities/tests/test_software.py index 62a0644..1d2f70c 100644 --- a/netbox_utilities/tests/test_software.py +++ b/netbox_utilities/tests/test_software.py @@ -73,3 +73,13 @@ class SoftwareSummaryTest(SimpleTestCase): self.assertTrue(software.supports_software(client, roles=["Clients"])) self.assertFalse(software.supports_software(server, roles=["Clients"])) self.assertFalse(software.supports_software(SimpleNamespace(role=None), roles=["Clients"])) + + def test_default_client_roles_match_name_or_slug_but_not_servers(self): + def role(name, slug): + return SimpleNamespace(name=name, slug=slug) + + roles = software.DEFAULT_SOFTWARE_DEVICE_ROLES + self.assertTrue(software.is_client_role(role("Client", "client"), roles)) + self.assertTrue(software.is_client_role(role("Windows Client", "windows-client"), roles)) + self.assertTrue(software.is_client_role(role("Windows-Clients", "windows-client"), roles)) + self.assertFalse(software.is_client_role(role("Server", "server"), roles)) diff --git a/pyproject.toml b/pyproject.toml index dad93c0..65e8a14 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "netbox-utilities" -version = "0.29.0" +version = "0.29.1" description = "Navigation, tenant utilities, connection VLANs, partial-width racks, disks, CPU & RAM, switch port export and bulk operations for NetBox 4.6/4.7" readme = "README.md" requires-python = ">=3.12"