From f26e65d7b6aecbd0a18b6e2c31dfea7799089891 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Mon, 8 Sep 2014 12:14:14 -0700 Subject: [PATCH] filter nodes by flavor dropdown, primarily intended for EC2 deployment where nodes are named after flavors --- planetstack/core/admin.py | 2 + .../admin/core/slice/change_form.html | 54 ++++++++++++++----- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py index d472851..3ff973f 100644 --- a/planetstack/core/admin.py +++ b/planetstack/core/admin.py @@ -278,6 +278,8 @@ class SliverInline(PlStackTabularInline): if db_field.name == 'deploymentNetwork': kwargs['queryset'] = Deployment.select_by_acl(request.user) kwargs['widget'] = forms.Select(attrs={'onChange': "sliver_deployment_changed(this);"}) + elif db_field.name == 'flavor': + kwargs['widget'] = forms.Select(attrs={'onChange': "sliver_flavor_changed(this);"}) field = super(SliverInline, self).formfield_for_foreignkey(db_field, request, **kwargs) diff --git a/planetstack/templates/admin/core/slice/change_form.html b/planetstack/templates/admin/core/slice/change_form.html index 1084004..424a41b 100644 --- a/planetstack/templates/admin/core/slice/change_form.html +++ b/planetstack/templates/admin/core/slice/change_form.html @@ -20,20 +20,31 @@ site_login_bases = [ {% endfor %} ]; -function update_nodes(deployment_select, node_select_id) { +function update_nodes(deployment_select, flavor_select, node_select) { deployment_id = $(deployment_select).val(); - html = "\n"; + flavor_name = $(flavor_select).children(":selected").text() + html=""; for (i in deployment_nodes) { dn = deployment_nodes[i]; - if (dn[0] == deployment_id) { + if ((dn[0] == deployment_id) && (dn[2].lastIndexOf(flavor_name,0) === 0)) { html = html + '\n' } } + if (!html) { + // now try it without the flavor hostname prefix matching + for (i in deployment_nodes) { + dn = deployment_nodes[i]; + if (dn[0] == deployment_id) { + html = html + '\n' + } + } + } + html = "\n" + html; //console.log(html); - $("#"+node_select_id).empty().append(html); + node_select.empty().append(html); } -function update_flavors(deployment_select, flavor_select_id) { +function update_flavors(deployment_select, flavor_select) { deployment_id = $(deployment_select).val(); html = "\n"; for (i in deployment_flavors) { @@ -43,20 +54,37 @@ function update_flavors(deployment_select, flavor_select_id) { } } //console.log(html); - $("#"+flavor_select_id).empty().append(html); + flavor_select.empty().append(html); } -function sliver_deployment_changed(deployment_select) { +function sliver_deployment_changed(any_control) { + /* This function handles someone changing the deploymentNetwork control + in the add-sliver line. It updats the flavors and nodes dialogs + accordingly. + */ + /* the inscrutable jquery selector below says: find the closest parent "tr" to the current element - then find the child with class "field-node" + then find the child with class "field-deploymentNetwork" then find the child with that is a select - then return its id + then return it's id + then turn it into a jquery object + */ + deployment_select = $("#" + $($(any_control).closest('tr')[0]).find('.field-deploymentNetwork select')[0].id); + node_select = $("#" + $($(any_control).closest('tr')[0]).find('.field-node select')[0].id); + flavor_select = $("#" + $($(any_control).closest('tr')[0]).find('.field-flavor select')[0].id); + update_nodes(deployment_select, flavor_select, node_select); + update_flavors(deployment_select, flavor_select); +} + +function sliver_flavor_changed(any_control) { + /* this is like sliver_flavor changed, but does not update the flavors + control */ - nodes_select_id = $($(deployment_select).closest('tr')[0]).find('.field-node select')[0].id; - update_nodes(deployment_select, nodes_select_id); - flavors_select_id = $($(deployment_select).closest('tr')[0]).find('.field-flavor select')[0].id; - update_flavors(deployment_select, flavors_select_id); + deployment_select = $("#" + $($(any_control).closest('tr')[0]).find('.field-deploymentNetwork select')[0].id); + node_select = $("#" + $($(any_control).closest('tr')[0]).find('.field-node select')[0].id); + flavor_select = $("#" + $($(any_control).closest('tr')[0]).find('.field-flavor select')[0].id); + update_nodes(deployment_select, flavor_select, node_select); } function update_slice_prefix(site_select, slice_name_id) { -- 2.43.0