Skip to content

Commit e0c3a9a

Browse files
committed
fixed acl type choice usage
1 parent ee851d3 commit e0c3a9a

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

netbox_acls/choices.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ class ACLAssignmentDirectionChoices(ChoiceSet):
5151
Defines the direction of the application of the ACL on an associated interface.
5252
"""
5353

54+
DIRECTION_INGRESS = "ingress"
55+
DIRECTION_EGRESS = "egress"
56+
5457
CHOICES = [
55-
("ingress", "Ingress", "blue"),
56-
("egress", "Egress", "purple"),
58+
(DIRECTION_INGRESS, "Ingress", "blue"),
59+
(DIRECTION_EGRESS, "Egress", "purple"),
5760
]
5861

5962

@@ -62,9 +65,12 @@ class ACLTypeChoices(ChoiceSet):
6265
Defines the choices availble for the Access Lists plugin specific to ACL type.
6366
"""
6467

68+
TYPE_STANDARD = "standard"
69+
TYPE_EXTENDED = "extended"
70+
6571
CHOICES = [
66-
("extended", "Extended", "purple"),
67-
("standard", "Standard", "blue"),
72+
(TYPE_EXTENDED, "Extended", "purple"),
73+
(TYPE_STANDARD, "Standard", "blue"),
6874
]
6975

7076

@@ -73,8 +79,12 @@ class ACLProtocolChoices(ChoiceSet):
7379
Defines the choices availble for the Access Lists plugin specific to ACL Rule protocol.
7480
"""
7581

82+
PROTOCOL_ICMP = "icmp"
83+
PROTOCOL_TCP = "tcp"
84+
PROTOCOL_UDP = "udp"
85+
7686
CHOICES = [
77-
("icmp", "ICMP", "purple"),
78-
("tcp", "TCP", "blue"),
79-
("udp", "UDP", "orange"),
87+
(PROTOCOL_ICMP, "ICMP", "purple"),
88+
(PROTOCOL_TCP, "TCP", "blue"),
89+
(PROTOCOL_UDP, "UDP", "orange"),
8090
]

netbox_acls/forms/models.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from virtualization.models import VirtualMachine, VMInterface
1818

19+
from ..choices import ACLTypeChoices
1920
from ..models import (
2021
AccessList,
2122
ACLExtendedRule,
@@ -201,8 +202,12 @@ def clean(self):
201202
"name": [error_same_acl_name],
202203
}
203204
# Check if Access List has no existing rules before change the Access List's type.
204-
if (acl_type == "extended" and self.instance.aclstandardrules.exists()) or (
205-
acl_type == "standard" and self.instance.aclextendedrules.exists()
205+
if (
206+
acl_type == ACLTypeChoices.TYPE_EXTENDED
207+
and self.instance.aclstandardrules.exists()
208+
) or (
209+
acl_type == ACLTypeChoices.TYPE_STANDARD
210+
and self.instance.aclextendedrules.exists()
206211
):
207212
error_message["type"] = [
208213
"This ACL has ACL rules associated, CANNOT change ACL type.",
@@ -425,7 +430,7 @@ class ACLStandardRuleForm(NetBoxModelForm):
425430
access_list = DynamicModelChoiceField(
426431
queryset=AccessList.objects.all(),
427432
query_params={
428-
"type": "standard",
433+
"type": ACLTypeChoices.TYPE_STANDARD,
429434
},
430435
help_text=mark_safe(
431436
"<b>*Note:</b> This field will only display Standard ACLs.",
@@ -507,7 +512,7 @@ class ACLExtendedRuleForm(NetBoxModelForm):
507512
access_list = DynamicModelChoiceField(
508513
queryset=AccessList.objects.all(),
509514
query_params={
510-
"type": "extended",
515+
"type": ACLTypeChoices.TYPE_EXTENDED,
511516
},
512517
help_text=mark_safe(
513518
"<b>*Note:</b> This field will only display Extended ACLs.",

netbox_acls/models/access_list_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.urls import reverse
99
from netbox.models import NetBoxModel
1010

11-
from ..choices import ACLProtocolChoices, ACLRuleActionChoices
11+
from ..choices import ACLProtocolChoices, ACLRuleActionChoices, ACLTypeChoices
1212
from .access_lists import AccessList
1313

1414
__all__ = (
@@ -84,7 +84,7 @@ class ACLStandardRule(ACLRule):
8484
on_delete=models.CASCADE,
8585
to=AccessList,
8686
verbose_name="Standard Access List",
87-
limit_choices_to={"type": "standard"},
87+
limit_choices_to={"type": ACLTypeChoices.TYPE_STANDARD},
8888
related_name="aclstandardrules",
8989
)
9090

netbox_acls/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.db.models import Count
77
from netbox.views import generic
88

9-
from . import filtersets, forms, models, tables
9+
from . import choices, filtersets, forms, models, tables
1010

1111
__all__ = (
1212
"AccessListView",
@@ -48,9 +48,10 @@ def get_extra_context(self, request, instance):
4848
"""
4949
Depending on the Access List type, the list view will return the required ACL Rule using the previous defined tables in tables.py.
5050
"""
51-
if instance.type == "extended":
51+
52+
if instance.type == choices.ACLTypeChoices.TYPE_EXTENDED:
5253
table = tables.ACLExtendedRuleTable(instance.aclextendedrules.all())
53-
elif instance.type == "standard":
54+
elif instance.type == choices.ACLTypeChoices.TYPE_STANDARD:
5455
table = tables.ACLStandardRuleTable(instance.aclstandardrules.all())
5556
table.configure(request)
5657

0 commit comments

Comments
 (0)