Skip to content

Commit 349b040

Browse files
committed
refactor form models
1 parent bde16dd commit 349b040

File tree

1 file changed

+64
-79
lines changed

1 file changed

+64
-79
lines changed

netbox_acls/forms/models.py

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -391,54 +391,35 @@ def clean(self):
391391
interface_types = self._get_interface_types()
392392

393393
# Initialize an error message variable
394-
error_message = self._validate_interface_types(interface_types)
394+
self._validate_interface_types(interface_types)
395395

396-
if not error_message:
397-
assigned_object_type, assigned_object = interface_types[0]
398-
host_type = (
399-
"device" if assigned_object_type == "interface" else "virtual_machine"
400-
)
396+
# Get the assigned interface & interface type
397+
assigned_object_type, assigned_object = interface_types[0]
401398

402-
# Get the parent host (device or virtual machine) of the assigned interface
403-
if assigned_object_type == "interface":
404-
host = Interface.objects.get(pk=assigned_object.pk).device
405-
assigned_object_id = Interface.objects.get(pk=assigned_object.pk).pk
406-
else:
407-
host = VMInterface.objects.get(pk=assigned_object.pk).virtual_machine
408-
assigned_object_id = VMInterface.objects.get(pk=assigned_object.pk).pk
409-
410-
# Get the ContentType id for the assigned object
411-
assigned_object_type_id = ContentType.objects.get_for_model(
412-
assigned_object
413-
).pk
414-
415-
if not error_message:
416-
# Check if the parent host is assigned to the Access List
417-
error_message |= self._check_if_interface_parent_is_assigned_to_access_list(
418-
cleaned_data.get("access_list"), assigned_object_type, host_type, host
419-
)
399+
# Get the parent host (device or virtual machine) of the assigned interface
400+
if assigned_object_type == "interface":
401+
assigned_object_id = Interface.objects.get(pk=assigned_object.pk).pk
402+
else:
403+
assigned_object_id = VMInterface.objects.get(pk=assigned_object.pk).pk
420404

421-
if not error_message:
422-
# Check for duplicate entries in the Access List
423-
error_message |= self._check_for_duplicate_entry(
424-
cleaned_data.get("access_list"),
425-
assigned_object_id,
426-
assigned_object_type_id,
427-
cleaned_data.get("direction"),
428-
)
405+
# Get the ContentType id for the assigned object
406+
assigned_object_type_id = ContentType.objects.get_for_model(assigned_object).pk
429407

430-
if not error_message:
431-
# Check if the interface already has an ACL applied in the specified direction
432-
error_message |= self._check_if_interface_already_has_acl_in_direction(
433-
assigned_object_id,
434-
assigned_object_type_id,
435-
cleaned_data.get("direction"),
436-
)
408+
# Check if the parent host is assigned to the Access List
409+
self._check_if_interface_parent_is_assigned_to_access_list(
410+
cleaned_data.get("access_list"), assigned_object_type, assigned_object
411+
)
437412

438-
if error_message:
439-
raise forms.ValidationError(error_message)
440-
else:
441-
return cleaned_data
413+
# Check for duplicate entries in the Access List
414+
self._check_if_interface_already_has_acl_in_direction(
415+
cleaned_data.get("access_list"),
416+
assigned_object_id,
417+
assigned_object_type,
418+
assigned_object_type_id,
419+
cleaned_data.get("direction"),
420+
)
421+
422+
return cleaned_data
442423

443424
def _get_interface_types(self):
444425
"""
@@ -458,69 +439,73 @@ def _validate_interface_types(self, interface_types):
458439
"""
459440
# Check if more than 1 hosts selected.
460441
if len(interface_types) > 1:
461-
return "Assignment can only be to one interface at a time (either a interface or vm_interface)."
442+
raise forms.ValidationError(
443+
"Assignment can only be to one interface at a time (either a interface or vminterface)."
444+
)
462445
# Check if no hosts selected.
463446
elif not interface_types:
464-
return "No interface or vm_interface selected."
465-
else:
466-
return {}
447+
raise forms.ValidationError("No interface or vminterface selected.")
467448

468449
def _check_if_interface_parent_is_assigned_to_access_list(
469-
self, access_list, assigned_object_type, host_type, host
450+
self, access_list, assigned_object_type, assigned_object
470451
):
471452
"""
472453
Check that an interface's parent device/virtual_machine is assigned to the Access List.
473454
"""
474-
475455
access_list_host = AccessList.objects.get(pk=access_list.pk).assigned_object
456+
host_type = (
457+
"device" if assigned_object_type == "interface" else "virtual_machine"
458+
)
459+
if assigned_object_type == "interface":
460+
host = Interface.objects.get(pk=assigned_object.pk).device
461+
else:
462+
host = VMInterface.objects.get(pk=assigned_object.pk).virtual_machine
476463

477464
if access_list_host != host:
478465
ERROR_ACL_NOT_ASSIGNED_TO_HOST = "Access List not present on selected host."
479-
return {
480-
"access_list": [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
481-
assigned_object_type: [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
482-
host_type: [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
483-
}
484-
else:
485-
return {}
466+
raise forms.ValidationError(
467+
{
468+
"access_list": [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
469+
assigned_object_type: [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
470+
host_type: [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
471+
}
472+
)
486473

487-
def _check_for_duplicate_entry(
488-
self, access_list, assigned_object_id, assigned_object_type_id, direction
474+
def _check_if_interface_already_has_acl_in_direction(
475+
self,
476+
access_list,
477+
assigned_object_id,
478+
assigned_object_type,
479+
assigned_object_type_id,
480+
direction,
489481
):
490482
"""
491-
Check for duplicate entry. (Because of GFK)
483+
Check that the interface does not have an existing ACL applied in the direction already.
492484
"""
493485

486+
# Check for duplicate entry. (Because of GFK)
494487
if ACLInterfaceAssignment.objects.filter(
495488
access_list=access_list,
496489
assigned_object_id=assigned_object_id,
497490
assigned_object_type=assigned_object_type_id,
498491
direction=direction,
499492
).exists():
500-
return {"access_list": ["Duplicate entry."]}
501-
else:
502-
return {}
503-
504-
def _check_if_interface_already_has_acl_in_direction(
505-
self, assigned_object_id, assigned_object_type_id, direction
506-
):
507-
"""
508-
Check that the interface does not have an existing ACL applied in the direction already.
509-
"""
510-
if not ACLInterfaceAssignment.objects.filter(
493+
raise forms.ValidationError({"access_list": ["Duplicate entry."]})
494+
# Check that the interface does not have an existing ACL applied in the direction already.
495+
elif ACLInterfaceAssignment.objects.filter(
511496
assigned_object_id=assigned_object_id,
512497
assigned_object_type=assigned_object_type_id,
513498
direction=direction,
514499
).exists():
515-
return {}
516-
517-
error_interface_already_assigned = (
518-
"Interfaces can only have 1 Access List assigned in each direction."
519-
)
520-
return {
521-
"direction": [error_interface_already_assigned],
522-
assigned_object_type: [error_interface_already_assigned],
523-
}
500+
error_interface_already_assigned = (
501+
"Interfaces can only have 1 Access List assigned in each direction."
502+
)
503+
raise forms.ValidationError(
504+
{
505+
"direction": [error_interface_already_assigned],
506+
assigned_object_type: [error_interface_already_assigned],
507+
}
508+
)
524509

525510
def save(self, *args, **kwargs):
526511
"""

0 commit comments

Comments
 (0)