Skip to content

Commit cce94e4

Browse files
committed
refactor
1 parent ced12c5 commit cce94e4

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

netbox_acls/forms/models.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ class Meta:
374374
),
375375
}
376376

377-
378377
def clean(self):
379378
"""
380379
Validates form inputs before submitting:
@@ -396,7 +395,9 @@ def clean(self):
396395

397396
if not error_message:
398397
assigned_object_type, assigned_object = interface_types[0]
399-
host_type = ("device" if assigned_object_type == "interface" else "virtual_machine")
398+
host_type = (
399+
"device" if assigned_object_type == "interface" else "virtual_machine"
400+
)
400401

401402
# Get the parent host (device or virtual machine) of the assigned interface
402403
if assigned_object_type == "interface":
@@ -407,26 +408,38 @@ def clean(self):
407408
assigned_object_id = VMInterface.objects.get(pk=assigned_object.pk).pk
408409

409410
# Get the ContentType id for the assigned object
410-
assigned_object_type_id = ContentType.objects.get_for_model(assigned_object).pk
411+
assigned_object_type_id = ContentType.objects.get_for_model(
412+
assigned_object
413+
).pk
411414

412415
if not error_message:
413416
# Check if the parent host is assigned to the Access List
414-
error_message |= self._check_if_interface_parent_is_assigned_to_access_list(cleaned_data.get("access_list"), assigned_object_type, host_type, host)
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+
)
415420

416421
if not error_message:
417422
# Check for duplicate entries in the Access List
418-
error_message |= self._check_for_duplicate_entry(cleaned_data.get("access_list"), assigned_object_id, assigned_object_type_id, cleaned_data.get("direction"),)
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+
)
419429

420430
if not error_message:
421431
# Check if the interface already has an ACL applied in the specified direction
422-
error_message |= self._check_if_interface_already_has_acl_in_direction(assigned_object_id, assigned_object_type_id, cleaned_data.get("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+
)
423437

424438
if error_message:
425439
raise forms.ValidationError(error_message)
426440
else:
427441
return cleaned_data
428442

429-
430443
def _get_interface_types(self):
431444
"""
432445
Get interface type/model assigned to the Access List.
@@ -452,26 +465,28 @@ def _validate_interface_types(self, interface_types):
452465
else:
453466
return {}
454467

455-
def _check_if_interface_parent_is_assigned_to_access_list(self, access_list, assigned_object_type, host_type, host):
468+
def _check_if_interface_parent_is_assigned_to_access_list(
469+
self, access_list, assigned_object_type, host_type, host
470+
):
456471
"""
457472
Check that an interface's parent device/virtual_machine is assigned to the Access List.
458473
"""
459474

460475
access_list_host = AccessList.objects.get(pk=access_list.pk).assigned_object
461476

462477
if access_list_host != host:
463-
ERROR_ACL_NOT_ASSIGNED_TO_HOST = (
464-
"Access List not present on selected host."
465-
)
478+
ERROR_ACL_NOT_ASSIGNED_TO_HOST = "Access List not present on selected host."
466479
return {
467480
"access_list": [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
468481
assigned_object_type: [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
469482
host_type: [ERROR_ACL_NOT_ASSIGNED_TO_HOST],
470-
}
483+
}
471484
else:
472485
return {}
473486

474-
def _check_for_duplicate_entry(self, access_list, assigned_object_id, assigned_object_type_id, direction):
487+
def _check_for_duplicate_entry(
488+
self, access_list, assigned_object_id, assigned_object_type_id, direction
489+
):
475490
"""
476491
Check for duplicate entry. (Because of GFK)
477492
"""
@@ -486,25 +501,26 @@ def _check_for_duplicate_entry(self, access_list, assigned_object_id, assigned_o
486501
else:
487502
return {}
488503

489-
def _check_if_interface_already_has_acl_in_direction(self, assigned_object_id, assigned_object_type_id, direction):
504+
def _check_if_interface_already_has_acl_in_direction(
505+
self, assigned_object_id, assigned_object_type_id, direction
506+
):
490507
"""
491508
Check that the interface does not have an existing ACL applied in the direction already.
492509
"""
493-
if ACLInterfaceAssignment.objects.filter(
510+
if not ACLInterfaceAssignment.objects.filter(
494511
assigned_object_id=assigned_object_id,
495512
assigned_object_type=assigned_object_type_id,
496513
direction=direction,
497514
).exists():
515+
return {}
516+
else:
498517
error_interface_already_assigned = (
499518
"Interfaces can only have 1 Access List assigned in each direction."
500519
)
501520
return {
502521
"direction": [error_interface_already_assigned],
503522
assigned_object_type: [error_interface_already_assigned],
504523
}
505-
else:
506-
return {}
507-
508524

509525
def save(self, *args, **kwargs):
510526
"""

0 commit comments

Comments
 (0)