|
51 | 51 |
|
52 | 52 | # Sets a standard error message for ACL rules with an action of remark, but no remark set.
|
53 | 53 | 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." |
56 | 54 | # Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
|
57 | 55 | error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
|
58 | 56 |
|
| 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." |
59 | 62 |
|
60 | 63 | class AccessListForm(NetBoxModelForm):
|
61 | 64 | """
|
@@ -535,35 +538,41 @@ class Meta:
|
535 | 538 | "index": help_text_acl_rule_index,
|
536 | 539 | "action": help_text_acl_action,
|
537 | 540 | "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.", |
539 | 542 | ),
|
540 | 543 | }
|
541 | 544 |
|
542 | 545 | def clean(self):
|
543 | 546 | """
|
544 | 547 | Validates form inputs before submitting:
|
545 | 548 | - 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. |
547 | 550 | - Check remark set, but action not set to remark.
|
| 551 | + - Check not more than one source is set. |
548 | 552 | """
|
549 | 553 | super().clean()
|
550 | 554 | cleaned_data = self.cleaned_data
|
551 | 555 | error_message = {}
|
552 | 556 |
|
553 | 557 | action = cleaned_data.get("action")
|
554 | 558 | 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"] |
556 | 560 |
|
557 | 561 | if action == "remark":
|
558 | 562 | # Check if action set to remark, but no remark set.
|
559 | 563 | if not remark:
|
560 | 564 | 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] |
564 | 569 | # Check remark set, but action not set to remark.
|
565 | 570 | elif remark:
|
566 | 571 | 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] |
567 | 576 |
|
568 | 577 | if error_message:
|
569 | 578 | raise ValidationError(error_message)
|
|
0 commit comments