diff --git a/netbox_acls/forms/models.py b/netbox_acls/forms/models.py index d25bab2b..454c8782 100644 --- a/netbox_acls/forms/models.py +++ b/netbox_acls/forms/models.py @@ -1,15 +1,16 @@ """ Defines each django model's GUI form to add or edit objects for each django model. """ -from django.utils.translation import gettext_lazy as _ + from dcim.models import Device, Interface, Region, Site, SiteGroup, VirtualChassis from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from django.utils.safestring import mark_safe +from django.utils.translation import gettext_lazy as _ from ipam.models import Prefix from netbox.forms import NetBoxModelForm -from utilities.forms.rendering import FieldSet from utilities.forms.fields import CommentField, DynamicModelChoiceField +from utilities.forms.rendering import FieldSet, TabbedGroups from virtualization.models import ( Cluster, ClusterGroup, @@ -117,14 +118,41 @@ class AccessListForm(NetBoxModelForm): "cluster_group_id": "$cluster_group", }, ) - comments = CommentField() + fieldsets = ( - FieldSet('region', 'site_group', 'site', 'virtual_machine', 'virtual_chassis', 'device', name=_('Assignment')), - FieldSet('name', 'type', 'default_action', name=_('Access List')), - FieldSet('comments', 'tags', name=_('')), + FieldSet( + "name", + "type", + "default_action", + "tags", + name=_("Access List Details"), + ), + FieldSet( + TabbedGroups( + FieldSet( + "region", + "site_group", + "site", + "device", + name=_("Device"), + ), + FieldSet( + "virtual_chassis", + name=_("Virtual Chassis"), + ), + FieldSet( + "cluster_type", + "cluster_group", + "cluster", + "virtual_machine", + name=_("Virtual Machine"), + ), + ), + name=_("Host Assignment"), + ), ) - + class Meta: model = AccessList fields = ( @@ -145,7 +173,7 @@ class Meta: "default_action": "The default behavior of the ACL.", "name": "The name uniqueness per device is case insensitive.", "type": mark_safe( - "*Note: CANNOT be changed if ACL Rules are assoicated to this Access List.", + "*Note: CANNOT be changed if ACL Rules are associated to this Access List.", ), } @@ -200,14 +228,22 @@ def clean(self): # Check if more than one host type selected. if (device and virtual_chassis) or (device and virtual_machine) or (virtual_chassis and virtual_machine): raise ValidationError( - {"__all__": "Access Lists must be assigned to one host at a time. Either a device, virtual chassis or virtual machine."}, + { + "__all__": ( + "Access Lists must be assigned to one host at a time. Either a device, virtual chassis or " + "virtual machine." + ) + }, ) # Check if no hosts selected. if not device and not virtual_chassis and not virtual_machine: - raise ValidationError({"__all__": "Access Lists must be assigned to a device, virtual chassis or virtual machine."}) + raise ValidationError( + {"__all__": "Access Lists must be assigned to a device, virtual chassis or virtual machine."} + ) existing_acls = None + host_type = None if device: host_type = "device" existing_acls = AccessList.objects.filter(name=name, device=device).exists() @@ -233,7 +269,9 @@ def clean(self): def save(self, *args, **kwargs): # Set assigned object self.instance.assigned_object = ( - self.cleaned_data.get("device") or self.cleaned_data.get("virtual_chassis") or self.cleaned_data.get("virtual_machine") + self.cleaned_data.get("device") + or self.cleaned_data.get("virtual_chassis") + or self.cleaned_data.get("virtual_machine") ) return super().save(*args, **kwargs) @@ -292,10 +330,29 @@ class ACLInterfaceAssignmentForm(NetBoxModelForm): ), ) comments = CommentField() + fieldsets = ( - FieldSet('device', 'interface', 'virtual_machine', 'vminterface', name=_('Assignment')), - FieldSet('access_list', 'direction', name=_('Access List Details')), - FieldSet('comments', 'tags', name=_('')), + FieldSet( + "access_list", + "direction", + "tags", + name=_("Access List Details"), + ), + FieldSet( + TabbedGroups( + FieldSet( + "device", + "interface", + name=_("Device"), + ), + FieldSet( + "virtual_machine", + "vminterface", + name=_("Virtual Machine"), + ), + ), + name=_("Interface Assignment"), + ), ) def __init__(self, *args, **kwargs): @@ -453,9 +510,21 @@ class ACLStandardRuleForm(NetBoxModelForm): ) fieldsets = ( - FieldSet("access_list", "description", "tags", name=_('Access List Details')), - FieldSet("index", "action", "remark", "source_prefix", name=_('Rule Definition')) + FieldSet( + "access_list", + "description", + "tags", + name=_("Access List Details"), + ), + FieldSet( + "index", + "action", + "remark", + "source_prefix", + name=_("Rule Definition"), + ), ) + class Meta: model = ACLStandardRule fields = ( @@ -468,9 +537,6 @@ class Meta: "description", ) - - - help_texts = { "index": help_text_acl_rule_index, "action": help_text_acl_action, @@ -540,9 +606,25 @@ class ACLExtendedRuleForm(NetBoxModelForm): label="Destination Prefix", ) fieldsets = ( - FieldSet("access_list", "description", "tags", name=_('Access List Details')), - FieldSet("index", "action", "remark", "source_prefix", "source_ports", "destination_prefix", "destination_ports", "protocol", name=_('Rule Definition')) + FieldSet( + "access_list", + "description", + "tags", + name=_("Access List Details"), + ), + FieldSet( + "index", + "action", + "remark", + "source_prefix", + "source_ports", + "destination_prefix", + "destination_ports", + "protocol", + name=_("Rule Definition"), + ), ) + class Meta: model = ACLExtendedRule fields = ( diff --git a/netbox_acls/templates/netbox_acls/accesslist_edit.html b/netbox_acls/templates/netbox_acls/accesslist_edit.html deleted file mode 100644 index 66a056ba..00000000 --- a/netbox_acls/templates/netbox_acls/accesslist_edit.html +++ /dev/null @@ -1,97 +0,0 @@ -{% extends 'generic/object_edit.html' %} -{% load static %} -{% load form_helpers %} - -{% block form %} - {% render_errors form %} -
-
-

Access List Details

-
- {% render_field form.name %} - {% render_field form.type %} - {% render_field form.default_action %} - {% render_field form.tags %} -
-
-
-

Host Assignment

-
-
-
- -
-
-
-
- {% render_field form.region %} - {% render_field form.site_group %} - {% render_field form.site %} - {% render_field form.device %} -
-
- {% render_field form.virtual_chassis %} -
-
- {% render_field form.cluster_type %} - {% render_field form.cluster_group %} - {% render_field form.cluster %} - {% render_field form.virtual_machine %} -
-
-
-
-
-

Comments

-
- {% render_field form.comments %} -
- {% if form.custom_fields %} -
-
-

Custom Fields

-
- {% render_custom_fields form %} -
- {% endif %} -{% endblock %} diff --git a/netbox_acls/templates/netbox_acls/aclinterfaceassignment_edit.html b/netbox_acls/templates/netbox_acls/aclinterfaceassignment_edit.html deleted file mode 100644 index f0a379a2..00000000 --- a/netbox_acls/templates/netbox_acls/aclinterfaceassignment_edit.html +++ /dev/null @@ -1,76 +0,0 @@ -{% extends 'generic/object_edit.html' %} -{% load static %} -{% load form_helpers %} - -{% block form %} - {% render_errors form %} -
-
-

Access List Details

-
- {% render_field form.access_list %} - {% render_field form.direction %} - {% render_field form.tags %} -
-
-
-

Interface Assignment

-
-
-
- -
-
-
-
- {% render_field form.device %} - {% render_field form.interface %} -
-
- {% render_field form.virtual_machine %} - {% render_field form.vminterface %} -
-
-
-
-
-

Comments

-
- {% render_field form.comments %} -
- {% if form.custom_fields %} -
-
-

Custom Fields

-
- {% render_custom_fields form %} -
- {% endif %} -{% endblock %} diff --git a/netbox_acls/views.py b/netbox_acls/views.py index 9f1fa81d..24b1e414 100644 --- a/netbox_acls/views.py +++ b/netbox_acls/views.py @@ -51,7 +51,7 @@ class AccessListView(generic.ObjectView): def get_extra_context(self, request, instance): """ Depending on the Access List type, the list view will return - the required ACL Rule using the previous defined tables in tables.py. + the required ACL Rule using the previously defined tables in tables.py. """ if instance.type == choices.ACLTypeChoices.TYPE_EXTENDED: @@ -92,7 +92,6 @@ class AccessListEditView(generic.ObjectEditView): queryset = models.AccessList.objects.prefetch_related("tags") form = forms.AccessListForm - template_name = "netbox_acls/accesslist_edit.html" @register_model_view(models.AccessList, "delete") @@ -220,7 +219,6 @@ class ACLInterfaceAssignmentEditView(generic.ObjectEditView): "tags", ) form = forms.ACLInterfaceAssignmentForm - template_name = "netbox_acls/aclinterfaceassignment_edit.html" def get_extra_addanother_params(self, request): """