mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-20 02:06:42 -06:00
Merge pull request #18445 from netbox-community/18436-fix-unassign-mac-with-primary
Fixes: #18436 - Prevent unassigning mac address when primary on an interface
This commit is contained in:
commit
e02ae72f0c
@ -1810,6 +1810,11 @@ class MACAddressForm(NetBoxModelForm):
|
|||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
if instance and instance.assigned_object and instance.assigned_object.primary_mac_address:
|
||||||
|
if instance.assigned_object.primary_mac_address.pk == instance.pk:
|
||||||
|
self.fields['interface'].disabled = True
|
||||||
|
self.fields['vminterface'].disabled = True
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ from django.urls import reverse
|
|||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from core.models import ObjectType
|
||||||
from dcim.choices import *
|
from dcim.choices import *
|
||||||
from dcim.constants import *
|
from dcim.constants import *
|
||||||
from dcim.fields import MACAddressField
|
from dcim.fields import MACAddressField
|
||||||
@ -1522,3 +1523,27 @@ class MACAddress(PrimaryModel):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.mac_address)
|
return str(self.mac_address)
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Denote the original assigned object (if any) for validation in clean()
|
||||||
|
self._original_assigned_object_id = self.__dict__.get('assigned_object_id')
|
||||||
|
self._original_assigned_object_type_id = self.__dict__.get('assigned_object_type_id')
|
||||||
|
|
||||||
|
def clean(self, *args, **kwargs):
|
||||||
|
super().clean()
|
||||||
|
if self._original_assigned_object_id and self._original_assigned_object_type_id:
|
||||||
|
assigned_object = self.assigned_object
|
||||||
|
ct = ObjectType.objects.get_for_id(self._original_assigned_object_type_id)
|
||||||
|
original_assigned_object = ct.get_object_for_this_type(pk=self._original_assigned_object_id)
|
||||||
|
|
||||||
|
if original_assigned_object.primary_mac_address:
|
||||||
|
if not assigned_object:
|
||||||
|
raise ValidationError(
|
||||||
|
_("Cannot unassign MAC Address while it is designated as the primary MAC for an object")
|
||||||
|
)
|
||||||
|
elif original_assigned_object != assigned_object:
|
||||||
|
raise ValidationError(
|
||||||
|
_("Cannot reassign MAC Address while it is designated as the primary MAC for an object")
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user