Skip to content

Commit 06bfee3

Browse files
committed
feat(form): ACLExtendedRule: Change validation to support multiple sources; Add validation to restrict to a single Source
1 parent 93f0cd4 commit 06bfee3

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

netbox_acls/forms/models.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -667,34 +667,33 @@ class Meta:
667667
"description",
668668
)
669669
help_texts = {
670-
"action": help_text_acl_action,
671-
"destination_ports": help_text_acl_rule_logic,
672670
"index": help_text_acl_rule_index,
673-
"protocol": help_text_acl_rule_logic,
671+
"action": help_text_acl_action,
674672
"remark": mark_safe(
675673
"<b>*Note:</b> CANNOT be set if action is not set to remark.",
676674
),
677675
"source_ports": help_text_acl_rule_logic,
676+
"destination_ports": help_text_acl_rule_logic,
677+
"protocol": help_text_acl_rule_logic,
678678
}
679679

680680
def clean(self):
681681
"""
682682
Validates form inputs before submitting:
683683
- Check if action set to remark, but no remark set.
684-
- Check if action set to remark, but source_prefix set.
685-
- Check if action set to remark, but source_ports set.
686-
- Check if action set to remark, but destination_prefix set.
687-
- Check if action set to remark, but destination_ports set.
688-
- Check if action set to remark, but protocol set.
684+
- Check if action set to remark, but source set.
685+
- Check if action set to remark, but protocol set
689686
- Check remark set, but action not set to remark.
687+
- Check not more than one source is set.
690688
"""
691689
super().clean()
692690
cleaned_data = self.cleaned_data
693691
error_message = {}
694692

695693
action = cleaned_data.get("action")
696694
remark = cleaned_data.get("remark")
697-
source_prefix = cleaned_data.get("source_prefix")
695+
696+
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
698697
source_ports = cleaned_data.get("source_ports")
699698
destination_prefix = cleaned_data.get("destination_prefix")
700699
destination_ports = cleaned_data.get("destination_ports")
@@ -703,8 +702,11 @@ def clean(self):
703702
if action == "remark":
704703
if not remark:
705704
error_message["remark"] = [error_message_no_remark]
706-
if source_prefix:
707-
error_message["source_prefix"] = [error_message_action_remark_source_prefix_set]
705+
706+
# Check if action set to remark, but source set.
707+
for source in sources:
708+
error_message[source] = [error_message_action_remark_source_set]
709+
708710
if source_ports:
709711
error_message["source_ports"] = ["Action is set to remark, Source Ports CANNOT be set."]
710712
if destination_prefix:
@@ -716,5 +718,10 @@ def clean(self):
716718
elif remark:
717719
error_message["remark"] = [error_message_remark_without_action_remark]
718720

721+
# Check not more than one source is set.
722+
elif sum(bool(cleaned_data.get(source)) for source in sources) > 1:
723+
for source in sources:
724+
error_message[source] = [error_message_sources_more_than_one]
725+
719726
if error_message:
720727
raise ValidationError(error_message)

0 commit comments

Comments
 (0)