Skip to content

Commit d290ae1

Browse files
committed
feat(form): ACLStandardRule: Change validation to support multiple sources; Add validation to restrict to a single Source
1 parent 87515f4 commit d290ae1

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

netbox_acls/forms/models.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@
5151

5252
# Sets a standard error message for ACL rules with an action of remark, but no remark set.
5353
error_message_no_remark = "Action is set to remark, you MUST add a remark."
54-
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
55-
error_message_action_remark_source_prefix_set = "Action is set to remark, Source Prefix CANNOT be set."
5654
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
5755
error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
5856

57+
# Sets a standard error message for ACL rules with an action of remark, but no source is set.
58+
error_message_action_remark_source_set = "Action is set to remark, Source CANNOT be set."
59+
60+
# Sets a standard error message for ACL rules when more than one IP/Host sources are set.
61+
error_message_sources_more_than_one = "Only one IP/Host related Source can be specified."
5962

6063
class AccessListForm(NetBoxModelForm):
6164
"""
@@ -535,35 +538,41 @@ class Meta:
535538
"index": help_text_acl_rule_index,
536539
"action": help_text_acl_action,
537540
"remark": mark_safe(
538-
"<b>*Note:</b> CANNOT be set if source prefix OR action is set.",
541+
"<b>*Note:</b> CANNOT be set if source OR action is set.",
539542
),
540543
}
541544

542545
def clean(self):
543546
"""
544547
Validates form inputs before submitting:
545548
- Check if action set to remark, but no remark set.
546-
- Check if action set to remark, but source_prefix set.
549+
- Check if action set to remark, but source set.
547550
- Check remark set, but action not set to remark.
551+
- Check not more than one source is set.
548552
"""
549553
super().clean()
550554
cleaned_data = self.cleaned_data
551555
error_message = {}
552556

553557
action = cleaned_data.get("action")
554558
remark = cleaned_data.get("remark")
555-
source_prefix = cleaned_data.get("source_prefix")
559+
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
556560

557561
if action == "remark":
558562
# Check if action set to remark, but no remark set.
559563
if not remark:
560564
error_message["remark"] = [error_message_no_remark]
561-
# Check if action set to remark, but source_prefix set.
562-
if source_prefix:
563-
error_message["source_prefix"] = [error_message_action_remark_source_prefix_set]
565+
# Check if action set to remark, but source set.
566+
if any(cleaned_data.get(source) for source in sources):
567+
for source in sources:
568+
error_message[source] = [error_message_action_remark_source_set]
564569
# Check remark set, but action not set to remark.
565570
elif remark:
566571
error_message["remark"] = [error_message_remark_without_action_remark]
572+
# Check not more than one source is set.
573+
elif sum(bool(cleaned_data.get(source)) for source in sources) > 1:
574+
for source in sources:
575+
error_message[source] = [error_message_sources_more_than_one]
567576

568577
if error_message:
569578
raise ValidationError(error_message)

0 commit comments

Comments
 (0)