Skip to content

Commit e9aa918

Browse files
committed
optimize form models
1 parent 5f5f24a commit e9aa918

File tree

2 files changed

+47
-60
lines changed

2 files changed

+47
-60
lines changed

netbox_acls/forms/constants.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
HELP_TEXT_ACL_RULE_LOGIC = mark_safe(
88
"<b>*Note:</b> CANNOT be set if action is set to remark.",
99
)
10+
1011
# Sets a standard help_text value to be used by the various classes for acl action
1112
HELP_TEXT_ACL_ACTION = "Action the rule will take (remark, deny, or allow)."
13+
1214
# Sets a standard help_text value to be used by the various classes for acl index
1315
HELP_TEXT_ACL_RULE_INDEX = (
1416
"Determines the order of the rule in the ACL processing. AKA Sequence Number."
1517
)
1618

1719
# Sets a standard error message for ACL rules with an action of remark, but no remark set.
1820
ERROR_MESSAGE_NO_REMARK = "Action is set to remark, you MUST add a remark."
19-
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
20-
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET = (
21-
"Action is set to remark, Source Prefix CANNOT be set."
22-
)
21+
2322
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
2423
ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK = (
2524
"CANNOT set remark unless action is set to remark."

netbox_acls/forms/models.py

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from ..choices import ACLTypeChoices
2121
from .constants import (
22-
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
2322
ERROR_MESSAGE_NO_REMARK,
2423
ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK,
2524
HELP_TEXT_ACL_ACTION,
@@ -185,14 +184,14 @@ def clean(self):
185184
host_types = self._get_host_types()
186185

187186
# Check if no hosts selected.
188-
self._validate_host_types(host_types)
187+
self._clean_check_host_types(host_types)
189188

190189
host_type, host = host_types[0]
191190

192191
# Check if duplicate entry.
193-
self._validate_duplicate_entry(name, host_type, host, error_message)
192+
self._clean_check_duplicate_entry(name, host_type, host, error_message)
194193
# Check if Access List has no existing rules before change the Access List's type.
195-
self._validate_acl_type_change(acl_type, error_message)
194+
self._clean_check_acl_type_change(acl_type, error_message)
196195

197196
if error_message:
198197
raise forms.ValidationError(error_message)
@@ -213,9 +212,9 @@ def _get_host_types(self):
213212
]
214213
return [x for x in host_types if x[1]]
215214

216-
def _validate_host_types(self, host_types):
215+
def _clean_check_host_types(self, host_types):
217216
"""
218-
Check number of host types selected.
217+
Used by parent class's clean method. Check number of host types selected.
219218
"""
220219
if len(host_types) > 1:
221220
raise forms.ValidationError(
@@ -227,9 +226,9 @@ def _validate_host_types(self, host_types):
227226
"Access Lists must be assigned to a device, virtual chassis or virtual machine.",
228227
)
229228

230-
def _validate_duplicate_entry(self, name, host_type, host, error_message):
229+
def _clean_check_duplicate_entry(self, name, host_type, host, error_message):
231230
"""
232-
Check if duplicate entry. (Because of GFK.)
231+
Used by parent class's clean method. Check if duplicate entry. (Because of GFK.)
233232
"""
234233
existing_acls = AccessList.objects.filter(
235234
name=name, **{host_type: host}
@@ -246,9 +245,9 @@ def _validate_duplicate_entry(self, name, host_type, host, error_message):
246245
"name": [error_same_acl_name],
247246
}
248247

249-
def _validate_acl_type_change(self, acl_type, error_message):
248+
def _clean_check_acl_type_change(self, acl_type, error_message):
250249
"""
251-
Check if Access List has no existing rules before change the Access List's type.
250+
Used by parent class's clean method. Check if Access List has no existing rules before change the Access List's type.
252251
"""
253252
if self.instance.pk:
254253
error_message["type"] = [
@@ -386,10 +385,10 @@ def clean(self):
386385
cleaned_data = super().clean()
387386

388387
# Get the interface types assigned to the Access List
389-
interface_types = self._get_interface_types()
388+
interface_types = self._clean_get_interface_types()
390389

391390
# Initialize an error message variable
392-
self._validate_interface_types(interface_types)
391+
self._clean_check_interface_types(interface_types)
393392

394393
# Get the assigned interface & interface type
395394
assigned_object_type, assigned_object = interface_types[0]
@@ -404,12 +403,12 @@ def clean(self):
404403
assigned_object_type_id = ContentType.objects.get_for_model(assigned_object).pk
405404

406405
# Check if the parent host is assigned to the Access List
407-
self._validate_if_interface_parent_is_assigned_to_access_list(
406+
self._clean_check_if_interface_parent_is_assigned_to_access_list(
408407
cleaned_data.get("access_list"), assigned_object_type, assigned_object
409408
)
410409

411410
# Check for duplicate entries in the Access List
412-
self._validate_if_interface_already_has_acl_in_direction(
411+
self._clean_check_if_interface_already_has_acl_in_direction(
413412
cleaned_data.get("access_list"),
414413
assigned_object_id,
415414
assigned_object_type,
@@ -419,9 +418,9 @@ def clean(self):
419418

420419
return cleaned_data
421420

422-
def _get_interface_types(self):
421+
def _clean_get_interface_types(self):
423422
"""
424-
Get interface type/model assigned to the Access List.
423+
Used by parent class's clean method. Get interface type/model assigned to the Access List.
425424
"""
426425
interface = self.cleaned_data.get("interface")
427426
vminterface = self.cleaned_data.get("vminterface")
@@ -431,9 +430,9 @@ def _get_interface_types(self):
431430
]
432431
return [x for x in interface_types if x[1]]
433432

434-
def _validate_interface_types(self, interface_types):
433+
def _clean_check_interface_types(self, interface_types):
435434
"""
436-
Check if number of interface type selected is 1.
435+
Used by parent class's clean method. Check if number of interface type selected is 1.
437436
"""
438437
# Check if more than 1 hosts selected.
439438
if len(interface_types) > 1:
@@ -444,11 +443,11 @@ def _validate_interface_types(self, interface_types):
444443
elif not interface_types:
445444
raise forms.ValidationError("No interface or vminterface selected.")
446445

447-
def _validate_if_interface_parent_is_assigned_to_access_list(
446+
def _clean_check_if_interface_parent_is_assigned_to_access_list(
448447
self, access_list, assigned_object_type, assigned_object
449448
):
450449
"""
451-
Check that an interface's parent device/virtual_machine is assigned to the Access List.
450+
Used by parent class's clean method. Check that an interface's parent device/virtual_machine is assigned to the Access List.
452451
"""
453452
access_list_host = AccessList.objects.get(pk=access_list.pk).assigned_object
454453
host_type = (
@@ -469,7 +468,7 @@ def _validate_if_interface_parent_is_assigned_to_access_list(
469468
}
470469
)
471470

472-
def _validate_if_interface_already_has_acl_in_direction(
471+
def _clean_check_if_interface_already_has_acl_in_direction(
473472
self,
474473
access_list,
475474
assigned_object_id,
@@ -489,8 +488,9 @@ def _validate_if_interface_already_has_acl_in_direction(
489488
direction=direction,
490489
).exists():
491490
raise forms.ValidationError({"access_list": ["Duplicate entry."]})
491+
492492
# Check that the interface does not have an existing ACL applied in the direction already.
493-
elif ACLInterfaceAssignment.objects.filter(
493+
if ACLInterfaceAssignment.objects.filter(
494494
assigned_object_id=assigned_object_id,
495495
assigned_object_type=assigned_object_type_id,
496496
direction=direction,
@@ -571,7 +571,7 @@ def clean(self):
571571
# No need to check for unique_together since there is no usage of GFK
572572

573573
if cleaned_data.get("action") == "remark":
574-
self._validate_acl_rules(cleaned_data, error_message, "extended")
574+
self._clean_check_acl_rules(cleaned_data, error_message, "extended")
575575
# Check remark set, but action not set to remark.
576576
elif cleaned_data.get("remark"):
577577
error_message["remark"] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK]
@@ -580,45 +580,33 @@ def clean(self):
580580
raise forms.ValidationError(error_message)
581581
return cleaned_data
582582

583-
def _validate_acl_rules(self, cleaned_data, error_message, rule_type):
583+
def _clean_check_acl_rules(self, cleaned_data, error_message, rule_type):
584584
"""
585-
Validates form inputs before submitting:
586-
- Check if action set to remark, but no remark set.
587-
- Check if action set to remark, but source_prefix set.
588-
- Check if action set to remark, but source_ports set.
589-
- Check if action set to remark, but destination_prefix set.
590-
- Check if action set to remark, but destination_ports set.
591-
- Check if action set to remark, but destination_ports set.
592-
- Check if action set to remark, but protocol set.
585+
Used by parent class's clean method. Checks form inputs before submitting:
586+
- Check if action set to remark, but no remark set.
587+
- Check if action set to remark, but other rule attributes set.
593588
"""
594589
# Check if action set to remark, but no remark set.
595590
if not cleaned_data.get("remark"):
596591
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
597-
# Check if action set to remark, but source_prefix set.
598-
if cleaned_data.get("source_prefix"):
599-
error_message["source_prefix"] = [
600-
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET
601-
]
592+
593+
# list all the fields of a rule besides the remark
602594
if rule_type == "extended":
603-
# Check if action set to remark, but source_ports set.
604-
if cleaned_data.get("source_ports"):
605-
error_message["source_ports"] = [
606-
"Action is set to remark, Source Ports CANNOT be set."
607-
]
608-
# Check if action set to remark, but destination_prefix set.
609-
if cleaned_data.get("destination_prefix"):
610-
error_message["destination_prefix"] = [
611-
"Action is set to remark, Destination Prefix CANNOT be set.",
612-
]
613-
# Check if action set to remark, but destination_ports set.
614-
if cleaned_data.get("destination_ports"):
615-
error_message["destination_ports"] = [
616-
"Action is set to remark, Destination Ports CANNOT be set."
617-
]
618-
# Check if action set to remark, but protocol set.
619-
if cleaned_data.get("protocol"):
620-
error_message["protocol"] = [
621-
"Action is set to remark, Protocol CANNOT be set."
595+
rule_attributes = [
596+
"source_prefix",
597+
"source_ports",
598+
"destination_prefix",
599+
"destination_ports",
600+
"protocol",
601+
]
602+
else:
603+
rule_attributes = ["source_prefix"]
604+
605+
# Check if action set to remark, but other fields set.
606+
for attribute in rule_attributes:
607+
if cleaned_data.get(attribute):
608+
error_message[attribute] = [
609+
f'Action is set to remark, {attribute.replace("_", " ").title()} CANNOT be set.'
622610
]
623611

624612

0 commit comments

Comments
 (0)