diff --git a/netbox_acls/choices.py b/netbox_acls/choices.py index 8247d45..4b1c5b2 100644 --- a/netbox_acls/choices.py +++ b/netbox_acls/choices.py @@ -51,9 +51,12 @@ class ACLAssignmentDirectionChoices(ChoiceSet): Defines the direction of the application of the ACL on an associated interface. """ + DIRECTION_INGRESS = "ingress" + DIRECTION_EGRESS = "egress" + CHOICES = [ - ("ingress", "Ingress", "blue"), - ("egress", "Egress", "purple"), + (DIRECTION_INGRESS, "Ingress", "blue"), + (DIRECTION_EGRESS, "Egress", "purple"), ] @@ -62,9 +65,12 @@ class ACLTypeChoices(ChoiceSet): Defines the choices availble for the Access Lists plugin specific to ACL type. """ + TYPE_STANDARD = "standard" + TYPE_EXTENDED = "extended" + CHOICES = [ - ("extended", "Extended", "purple"), - ("standard", "Standard", "blue"), + (TYPE_EXTENDED, "Extended", "purple"), + (TYPE_STANDARD, "Standard", "blue"), ] @@ -73,8 +79,12 @@ class ACLProtocolChoices(ChoiceSet): Defines the choices availble for the Access Lists plugin specific to ACL Rule protocol. """ + PROTOCOL_ICMP = "icmp" + PROTOCOL_TCP = "tcp" + PROTOCOL_UDP = "udp" + CHOICES = [ - ("icmp", "ICMP", "purple"), - ("tcp", "TCP", "blue"), - ("udp", "UDP", "orange"), + (PROTOCOL_ICMP, "ICMP", "purple"), + (PROTOCOL_TCP, "TCP", "blue"), + (PROTOCOL_UDP, "UDP", "orange"), ] diff --git a/netbox_acls/forms/models.py b/netbox_acls/forms/models.py index a8800e5..0c9b2e5 100644 --- a/netbox_acls/forms/models.py +++ b/netbox_acls/forms/models.py @@ -16,6 +16,7 @@ from utilities.forms import ( ) from virtualization.models import VirtualMachine, VMInterface +from ..choices import ACLTypeChoices from ..models import ( AccessList, ACLExtendedRule, @@ -201,8 +202,12 @@ class AccessListForm(NetBoxModelForm): "name": [error_same_acl_name], } # Check if Access List has no existing rules before change the Access List's type. - if (acl_type == "extended" and self.instance.aclstandardrules.exists()) or ( - acl_type == "standard" and self.instance.aclextendedrules.exists() + if ( + acl_type == ACLTypeChoices.TYPE_EXTENDED + and self.instance.aclstandardrules.exists() + ) or ( + acl_type == ACLTypeChoices.TYPE_STANDARD + and self.instance.aclextendedrules.exists() ): error_message["type"] = [ "This ACL has ACL rules associated, CANNOT change ACL type.", @@ -425,7 +430,7 @@ class ACLStandardRuleForm(NetBoxModelForm): access_list = DynamicModelChoiceField( queryset=AccessList.objects.all(), query_params={ - "type": "standard", + "type": ACLTypeChoices.TYPE_STANDARD, }, help_text=mark_safe( "*Note: This field will only display Standard ACLs.", @@ -507,7 +512,7 @@ class ACLExtendedRuleForm(NetBoxModelForm): access_list = DynamicModelChoiceField( queryset=AccessList.objects.all(), query_params={ - "type": "extended", + "type": ACLTypeChoices.TYPE_EXTENDED, }, help_text=mark_safe( "*Note: This field will only display Extended ACLs.", diff --git a/netbox_acls/models/access_list_rules.py b/netbox_acls/models/access_list_rules.py index 2dff662..8b21dde 100644 --- a/netbox_acls/models/access_list_rules.py +++ b/netbox_acls/models/access_list_rules.py @@ -8,7 +8,7 @@ from django.db import models from django.urls import reverse from netbox.models import NetBoxModel -from ..choices import ACLProtocolChoices, ACLRuleActionChoices +from ..choices import ACLProtocolChoices, ACLRuleActionChoices, ACLTypeChoices from .access_lists import AccessList __all__ = ( @@ -84,7 +84,7 @@ class ACLStandardRule(ACLRule): on_delete=models.CASCADE, to=AccessList, verbose_name="Standard Access List", - limit_choices_to={"type": "standard"}, + limit_choices_to={"type": ACLTypeChoices.TYPE_STANDARD}, related_name="aclstandardrules", ) diff --git a/netbox_acls/views.py b/netbox_acls/views.py index df1eae8..6ed5be9 100644 --- a/netbox_acls/views.py +++ b/netbox_acls/views.py @@ -6,7 +6,7 @@ Specifically, all the various interactions with a client. from django.db.models import Count from netbox.views import generic -from . import filtersets, forms, models, tables +from . import choices, filtersets, forms, models, tables __all__ = ( "AccessListView", @@ -48,9 +48,10 @@ class AccessListView(generic.ObjectView): """ Depending on the Access List type, the list view will return the required ACL Rule using the previous defined tables in tables.py. """ - if instance.type == "extended": + + if instance.type == choices.ACLTypeChoices.TYPE_EXTENDED: table = tables.ACLExtendedRuleTable(instance.aclextendedrules.all()) - elif instance.type == "standard": + elif instance.type == choices.ACLTypeChoices.TYPE_STANDARD: table = tables.ACLStandardRuleTable(instance.aclstandardrules.all()) table.configure(request)