Skip to content

Commit d7f4288

Browse files
committed
merge neste ifs & create new function
1 parent b8d7267 commit d7f4288

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

netbox_acls/forms/models.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,20 @@ def clean(self):
239239
"name": [error_same_acl_name],
240240
}
241241
# Check if Access List has no existing rules before change the Access List's type.
242-
if self.instance.pk:
243-
if (
242+
if (
243+
self.instance.pk
244+
and (
244245
acl_type == ACLTypeChoices.TYPE_EXTENDED
245246
and self.instance.aclstandardrules.exists()
246-
) or (
247+
)
248+
or (
247249
acl_type == ACLTypeChoices.TYPE_STANDARD
248250
and self.instance.aclextendedrules.exists()
249-
):
250-
error_message["type"] = [
251-
"This ACL has ACL rules associated, CANNOT change ACL type.",
252-
]
251+
)
252+
):
253+
error_message["type"] = [
254+
"This ACL has ACL rules associated, CANNOT change ACL type.",
255+
]
253256

254257
if error_message:
255258
raise forms.ValidationError(error_message)
@@ -642,38 +645,41 @@ def clean(self):
642645
# No need to check for unique_together since there is no usage of GFK
643646

644647
if cleaned_data.get("action") == "remark":
645-
# Check if action set to remark, but no remark set.
646-
if not cleaned_data.get("remark"):
647-
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
648-
# Check if action set to remark, but source_prefix set.
649-
if cleaned_data.get("source_prefix"):
650-
error_message["source_prefix"] = [
651-
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
652-
]
653-
# Check if action set to remark, but source_ports set.
654-
if cleaned_data.get("source_ports"):
655-
error_message["source_ports"] = [
656-
"Action is set to remark, Source Ports CANNOT be set.",
657-
]
658-
# Check if action set to remark, but destination_prefix set.
659-
if cleaned_data.get("destination_prefix"):
660-
error_message["destination_prefix"] = [
661-
"Action is set to remark, Destination Prefix CANNOT be set.",
662-
]
663-
# Check if action set to remark, but destination_ports set.
664-
if cleaned_data.get("destination_ports"):
665-
error_message["destination_ports"] = [
666-
"Action is set to remark, Destination Ports CANNOT be set.",
667-
]
668-
# Check if action set to remark, but protocol set.
669-
if cleaned_data.get("protocol"):
670-
error_message["protocol"] = [
671-
"Action is set to remark, Protocol CANNOT be set.",
672-
]
673-
# Check if action not set to remark, but remark set.
648+
self._extracted_from_clean_20(cleaned_data, error_message)
674649
elif cleaned_data.get("remark"):
675650
error_message["remark"] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK]
676651

677652
if error_message:
678653
raise forms.ValidationError(error_message)
679654
return cleaned_data
655+
656+
# TODO: Consolidate this function with the one in ACLStandardRuleForm
657+
def _extracted_from_clean_20(self, cleaned_data, error_message):
658+
# Check if action set to remark, but no remark set.
659+
if not cleaned_data.get("remark"):
660+
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
661+
# Check if action set to remark, but source_prefix set.
662+
if cleaned_data.get("source_prefix"):
663+
error_message["source_prefix"] = [
664+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
665+
]
666+
# Check if action set to remark, but source_ports set.
667+
if cleaned_data.get("source_ports"):
668+
error_message["source_ports"] = [
669+
"Action is set to remark, Source Ports CANNOT be set.",
670+
]
671+
# Check if action set to remark, but destination_prefix set.
672+
if cleaned_data.get("destination_prefix"):
673+
error_message["destination_prefix"] = [
674+
"Action is set to remark, Destination Prefix CANNOT be set.",
675+
]
676+
# Check if action set to remark, but destination_ports set.
677+
if cleaned_data.get("destination_ports"):
678+
error_message["destination_ports"] = [
679+
"Action is set to remark, Destination Ports CANNOT be set.",
680+
]
681+
# Check if action set to remark, but protocol set.
682+
if cleaned_data.get("protocol"):
683+
error_message["protocol"] = [
684+
"Action is set to remark, Protocol CANNOT be set.",
685+
]

0 commit comments

Comments
 (0)