Simplify VLANGroup scope assignment (WIP)

This commit is contained in:
Jeremy Stretch 2021-04-02 09:36:14 -04:00
parent b77c228853
commit a86178f19b
2 changed files with 26 additions and 47 deletions

View File

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

View File

@ -16,26 +16,14 @@
<strong>Scope</strong> <strong>Scope</strong>
</div> </div>
<div class="panel-body"> <div class="panel-body">
{% with virtual_tab_active=form.initial.cluster %} {% render_field form.scope_type %}
<ul class="nav nav-tabs" role="tablist"> {% render_field form.region %}
<li role="presentation"{% if not virtual_tab_active %} class="active"{% endif %}><a href="#physical" role="tab" data-toggle="tab">Physical</a></li> {% render_field form.sitegroup %}
<li role="presentation"{% if virtual_tab_active %} class="active"{% endif %}><a href="#virtual" role="tab" data-toggle="tab">Virtual</a></li> {% render_field form.site %}
</ul> {% render_field form.location %}
<div class="tab-content"> {% render_field form.rack %}
<div class="tab-pane{% if not virtual_tab_active %} active{% endif %}" id="physical"> {% render_field form.clustergroup %}
{% render_field form.region %} {% render_field form.cluster %}
{% 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 %}
</div> </div>
</div> </div>
{% if form.custom_fields %} {% if form.custom_fields %}