fix: show switch stack options only for devices with a switch role
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
102b161356
commit
cc2546b332
3 files changed
+23
-3
No files matched your search
@@ -1,6 +1,6 @@
|
||||
from netbox.plugins import PluginConfig, get_plugin_config
|
||||
|
||||
__version__ = "0.26.0"
|
||||
__version__ = "0.26.1"
|
||||
|
||||
|
||||
class NetBoxUtilitiesConfig(PluginConfig):
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
@@ -53,11 +55,29 @@ def build_stack_form_mixin():
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if not self._is_switch_form():
|
||||
for name in STACK_FORM_FIELDS:
|
||||
self.fields.pop(name, None)
|
||||
return
|
||||
if not self.is_bound and self.instance.pk:
|
||||
members = stack_members(self.instance)
|
||||
self.initial["utilities_switch_stack"] = SwitchStack.objects.filter(device=self.instance).exists()
|
||||
self.initial["utilities_stack_members"] = [member.pk for member in members]
|
||||
|
||||
def _is_switch_form(self):
|
||||
"""Stack options only belong to switches (role chosen in the form, else the saved role)."""
|
||||
from dcim.models import DeviceRole
|
||||
|
||||
from .switch_ports import is_switch
|
||||
|
||||
role = None
|
||||
role_id = (self.data.get("role") if self.is_bound else None) or self.initial.get("role")
|
||||
if role_id:
|
||||
role = DeviceRole.objects.filter(pk=getattr(role_id, "pk", role_id)).first()
|
||||
if role is None and self.instance.pk:
|
||||
role = getattr(self.instance, "role", None)
|
||||
return is_switch(SimpleNamespace(role=role))
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
members = list(self.cleaned_data.get("utilities_stack_members") or [])
|
||||
@@ -67,7 +87,7 @@ def build_stack_form_mixin():
|
||||
|
||||
def save(self, commit=True):
|
||||
device = super().save(commit=commit)
|
||||
if commit:
|
||||
if commit and "utilities_switch_stack" in self.fields:
|
||||
save_stack(device, self.cleaned_data.get("utilities_switch_stack"), self.cleaned_data.get("utilities_stack_members"))
|
||||
return device
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "netbox-utilities"
|
||||
version = "0.26.0"
|
||||
version = "0.26.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"
|
||||
|
||||
Reference in new issue
Block a user