|
50 | 50 |
|
51 | 51 | # Sets a standard error message for ACL rules with an action of remark, but no remark set.
|
52 | 52 | 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." |
55 | 53 | # Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
|
56 | 54 | error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
|
57 | 55 |
|
| 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." |
58 | 61 |
|
59 | 62 | class AccessListForm(NetBoxModelForm):
|
60 | 63 | """
|
@@ -518,35 +521,41 @@ class Meta:
|
518 | 521 | "index": help_text_acl_rule_index,
|
519 | 522 | "action": help_text_acl_action,
|
520 | 523 | "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.", |
522 | 525 | ),
|
523 | 526 | }
|
524 | 527 |
|
525 | 528 | def clean(self):
|
526 | 529 | """
|
527 | 530 | Validates form inputs before submitting:
|
528 | 531 | - 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. |
530 | 533 | - Check remark set, but action not set to remark.
|
| 534 | + - Check not more than one source is set. |
531 | 535 | """
|
532 | 536 | super().clean()
|
533 | 537 | cleaned_data = self.cleaned_data
|
534 | 538 | error_message = {}
|
535 | 539 |
|
536 | 540 | action = cleaned_data.get("action")
|
537 | 541 | 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"] |
539 | 543 |
|
540 | 544 | if action == "remark":
|
541 | 545 | # Check if action set to remark, but no remark set.
|
542 | 546 | if not remark:
|
543 | 547 | 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] |
547 | 552 | # Check remark set, but action not set to remark.
|
548 | 553 | elif remark:
|
549 | 554 | 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] |
550 | 559 |
|
551 | 560 | if error_message:
|
552 | 561 | raise ValidationError(error_message)
|
|
0 commit comments