2
0
Эх сурвалжийг харах

Simplify VLANGroup scope assignment (WIP)

Jeremy Stretch 4 жил өмнө
parent
commit
a86178f19b

+ 18 - 27
netbox/ipam/forms.py

@@ -1144,12 +1144,13 @@ class VLANGroupForm(BootstrapMixin, CustomFieldModelForm):
             'sites': '$site'
         }
     )
-    site_group = DynamicModelChoiceField(
+    sitegroup = DynamicModelChoiceField(
         queryset=SiteGroup.objects.all(),
         required=False,
         initial_params={
             'sites': '$site'
-        }
+        },
+        label='Site group'
     )
     site = DynamicModelChoiceField(
         queryset=Site.objects.all(),
@@ -1159,7 +1160,7 @@ class VLANGroupForm(BootstrapMixin, CustomFieldModelForm):
         },
         query_params={
             'region_id': '$region',
-            'group_id': '$site_group',
+            'group_id': '$sitegroup',
         }
     )
     location = DynamicModelChoiceField(
@@ -1180,18 +1181,19 @@ class VLANGroupForm(BootstrapMixin, CustomFieldModelForm):
             'location_id': '$location',
         }
     )
-    cluster_group = DynamicModelChoiceField(
+    clustergroup = DynamicModelChoiceField(
         queryset=ClusterGroup.objects.all(),
         required=False,
         initial_params={
             'clusters': '$cluster'
-        }
+        },
+        label='Cluster group'
     )
     cluster = DynamicModelChoiceField(
         queryset=Cluster.objects.all(),
         required=False,
         query_params={
-            'group_id': '$cluster_group',
+            'group_id': '$clustergroup',
         }
     )
     slug = SlugField()
@@ -1199,29 +1201,19 @@ class VLANGroupForm(BootstrapMixin, CustomFieldModelForm):
     class Meta:
         model = VLANGroup
         fields = [
-            'name', 'slug', 'description', 'region', 'site_group', 'site', 'location', 'rack', 'cluster_group',
-            'cluster',
+            'name', 'slug', 'description', 'scope_type', 'region', 'sitegroup', 'site', 'location', 'rack',
+            'clustergroup', 'cluster',
         ]
+        widgets = {
+            'scope_type': StaticSelect2,
+        }
 
     def __init__(self, *args, **kwargs):
         instance = kwargs.get('instance')
         initial = kwargs.get('initial', {})
 
         if instance is not None and instance.scope:
-            if type(instance.scope) is Rack:
-                initial['rack'] = instance.scope
-            elif type(instance.scope) is Location:
-                initial['location'] = instance.scope
-            elif type(instance.scope) is Site:
-                initial['site'] = instance.scope
-            elif type(instance.scope) is SiteGroup:
-                initial['site_group'] = instance.scope
-            elif type(instance.scope) is Region:
-                initial['region'] = instance.scope
-            elif type(instance.scope) is Cluster:
-                initial['cluster'] = instance.scope
-            elif type(instance.scope) is ClusterGroup:
-                initial['cluster_group'] = instance.scope
+            initial[instance.scope_type.model] = instance.scope
 
             kwargs['initial'] = initial
 
@@ -1230,11 +1222,10 @@ class VLANGroupForm(BootstrapMixin, CustomFieldModelForm):
     def clean(self):
         super().clean()
 
-        # Assign scope object
-        self.instance.scope = self.cleaned_data['rack'] or self.cleaned_data['location'] or \
-            self.cleaned_data['site'] or self.cleaned_data['site_group'] or \
-            self.cleaned_data['region'] or self.cleaned_data['cluster'] or \
-            self.cleaned_data['cluster_group'] or None
+        # Assign scope based on scope_type
+        if self.cleaned_data['scope_type']:
+            scope_field = self.cleaned_data['scope_type'].model
+            self.instance.scope = self.cleaned_data.get(scope_field)
 
 
 class VLANGroupCSVForm(CustomFieldModelCSVForm):

+ 8 - 20
netbox/templates/ipam/vlangroup_edit.html

@@ -16,26 +16,14 @@
             <strong>Scope</strong>
         </div>
         <div class="panel-body">
-            {% with virtual_tab_active=form.initial.cluster %}
-                <ul class="nav nav-tabs" role="tablist">
-                    <li role="presentation"{% if not virtual_tab_active %} class="active"{% endif %}><a href="#physical" role="tab" data-toggle="tab">Physical</a></li>
-                    <li role="presentation"{% if virtual_tab_active %} class="active"{% endif %}><a href="#virtual" role="tab" data-toggle="tab">Virtual</a></li>
-                </ul>
-                <div class="tab-content">
-                    <div class="tab-pane{% if not virtual_tab_active %} active{% endif %}" id="physical">
-                        {% render_field form.region %}
-                        {% render_field form.site_group %}
-                        {% render_field form.site %}
-                        {% render_field form.location %}
-                        {% render_field form.rack %}
-                    </div>
-                    <div class="tab-pane{% if virtual_tab_active %} active{% endif %}" id="virtual">
-                        {% render_field form.cluster_group %}
-                        {% render_field form.cluster %}
-                    </div>
-                </div>
-                <span class="help-block">The VLAN group will be limited in scope to the most-specific object selected above.</span>
-            {% endwith %}
+            {% render_field form.scope_type %}
+            {% render_field form.region %}
+            {% render_field form.sitegroup %}
+            {% render_field form.site %}
+            {% render_field form.location %}
+            {% render_field form.rack %}
+            {% render_field form.clustergroup %}
+            {% render_field form.cluster %}
         </div>
     </div>
     {% if form.custom_fields %}