Skip to content

Commit 1179ccd

Browse files
committed
feat(form): ACLExtendedRule: Change validation to support multiple sources; Add validation to restrict to a single Source
1 parent 00bc0ac commit 1179ccd

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
@@ -684,34 +684,33 @@ class Meta:
684684
)
685685

686686
help_texts = {
687-
"action": help_text_acl_action,
688-
"destination_ports": help_text_acl_rule_logic,
689687
"index": help_text_acl_rule_index,
690-
"protocol": help_text_acl_rule_logic,
688+
"action": help_text_acl_action,
691689
"remark": mark_safe(
692690
"<b>*Note:</b> CANNOT be set if action is not set to remark.",
693691
),
694692
"source_ports": help_text_acl_rule_logic,
693+
"destination_ports": help_text_acl_rule_logic,
694+
"protocol": help_text_acl_rule_logic,
695695
}
696696

697697
def clean(self):
698698
"""
699699
Validates form inputs before submitting:
700700
- Check if action set to remark, but no remark set.
701-
- Check if action set to remark, but source_prefix set.
702-
- Check if action set to remark, but source_ports set.
703-
- Check if action set to remark, but destination_prefix set.
704-
- Check if action set to remark, but destination_ports set.
705-
- Check if action set to remark, but protocol set.
701+
- Check if action set to remark, but source set.
702+
- Check if action set to remark, but protocol set
706703
- Check remark set, but action not set to remark.
704+
- Check not more than one source is set.
707705
"""
708706
super().clean()
709707
cleaned_data = self.cleaned_data
710708
error_message = {}
711709

712710
action = cleaned_data.get("action")
713711
remark = cleaned_data.get("remark")
714-
source_prefix = cleaned_data.get("source_prefix")
712+
713+
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
715714
source_ports = cleaned_data.get("source_ports")
716715
destination_prefix = cleaned_data.get("destination_prefix")
717716
destination_ports = cleaned_data.get("destination_ports")
@@ -720,8 +719,11 @@ def clean(self):
720719
if action == "remark":
721720
if not remark:
722721
error_message["remark"] = [error_message_no_remark]
723-
if source_prefix:
724-
error_message["source_prefix"] = [error_message_action_remark_source_prefix_set]
722+
723+
# Check if action set to remark, but source set.
724+
for source in sources:
725+
error_message[source] = [error_message_action_remark_source_set]
726+
725727
if source_ports:
726728
error_message["source_ports"] = ["Action is set to remark, Source Ports CANNOT be set."]
727729
if destination_prefix:
@@ -733,5 +735,10 @@ def clean(self):
733735
elif remark:
734736
error_message["remark"] = [error_message_remark_without_action_remark]
735737

738+
# Check not more than one source is set.
739+
elif sum(bool(cleaned_data.get(source)) for source in sources) > 1:
740+
for source in sources:
741+
error_message[source] = [error_message_sources_more_than_one]
742+
736743
if error_message:
737744
raise ValidationError(error_message)

0 commit comments

Comments
 (0)