Skip to content

Commit 1eb5a00

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

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
@@ -50,11 +50,14 @@
5050

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

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

5962
class AccessListForm(NetBoxModelForm):
6063
"""
@@ -518,35 +521,41 @@ class Meta:
518521
"index": help_text_acl_rule_index,
519522
"action": help_text_acl_action,
520523
"remark": mark_safe(
521-
"<b>*Note:</b> CANNOT be set if source prefix OR action is set.",
524+
"<b>*Note:</b> CANNOT be set if source OR action is set.",
522525
),
523526
}
524527

525528
def clean(self):
526529
"""
527530
Validates form inputs before submitting:
528531
- Check if action set to remark, but no remark set.
529-
- Check if action set to remark, but source_prefix set.
532+
- Check if action set to remark, but source set.
530533
- Check remark set, but action not set to remark.
534+
- Check not more than one source is set.
531535
"""
532536
super().clean()
533537
cleaned_data = self.cleaned_data
534538
error_message = {}
535539

536540
action = cleaned_data.get("action")
537541
remark = cleaned_data.get("remark")
538-
source_prefix = cleaned_data.get("source_prefix")
542+
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
539543

540544
if action == "remark":
541545
# Check if action set to remark, but no remark set.
542546
if not remark:
543547
error_message["remark"] = [error_message_no_remark]
544-
# Check if action set to remark, but source_prefix set.
545-
if source_prefix:
546-
error_message["source_prefix"] = [error_message_action_remark_source_prefix_set]
548+
# Check if action set to remark, but source set.
549+
if any(cleaned_data.get(source) for source in sources):
550+
for source in sources:
551+
error_message[source] = [error_message_action_remark_source_set]
547552
# Check remark set, but action not set to remark.
548553
elif remark:
549554
error_message["remark"] = [error_message_remark_without_action_remark]
555+
# Check not more than one source is set.
556+
elif sum(bool(cleaned_data.get(source)) for source in sources) > 1:
557+
for source in sources:
558+
error_message[source] = [error_message_sources_more_than_one]
550559

551560
if error_message:
552561
raise ValidationError(error_message)

0 commit comments

Comments
 (0)