Skip to content

Commit 560d937

Browse files
committed
fix(models): Ensure interface host matches access list host
Move validation to the `save` method to enforce that the assigned interface's host matches the access list's host before saving. This replaces validation in the `clean` method for better data integrity enforcement.
1 parent 624f796 commit 560d937

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

netbox_acls/models/access_lists.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,18 @@ def get_absolute_url(self):
150150
args=[self.pk],
151151
)
152152

153-
def clean(self):
154-
super().clean()
155-
156-
# Get the model type of the assigned interface.
157-
if self.assigned_object_type.model_class() == VMInterface:
158-
interface_host = self.assigned_object.virtual_machine
159-
elif self.assigned_object_type.model_class() == Interface:
160-
interface_host = self.assigned_object.device
161-
# Check if the assigned interface's host is the same as the host assigned to the access list.
162-
if interface_host != self.access_list.assigned_object:
153+
def save(self, *args, **kwargs):
154+
"""Saves the current instance to the database."""
155+
# Ensure the assigned interface's host matches the host assigned to the access list.
156+
if self.assigned_object.parent_object != self.access_list.assigned_object:
163157
raise ValidationError(
164158
{
165159
"assigned_object": "The assigned object must be the same as the device assigned to it."
166160
}
167161
)
168162

163+
super().save(*args, **kwargs)
164+
169165
def get_direction_color(self):
170166
return ACLAssignmentDirectionChoices.colors.get(self.direction)
171167

0 commit comments

Comments
 (0)