From a8848d9a2ffc9842072d2ea64a59d004651d2b78 Mon Sep 17 00:00:00 2001 From: Andy Botting Date: Wed, 14 Aug 2019 15:08:12 +1000 Subject: [PATCH] Make sensible default choices for fields This commit makes some sensible default choices for fields when adding and application to an environment. These are applied to the image and keypairs fields. In both cases, if only one option is available, then make that the initial value selected. In the images case, we take it a step further and if there is only one image, make that the only choice, dropping the 'Select Image' option completely. Change-Id: I24462374a50c1b8d83970f10278362f3dc5daf8b --- muranodashboard/dynamic_ui/fields.py | 8 +++++++- .../tests/unit/dynamic_ui/test_fields.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/muranodashboard/dynamic_ui/fields.py b/muranodashboard/dynamic_ui/fields.py index 73d7318ef..430bb70c9 100644 --- a/muranodashboard/dynamic_ui/fields.py +++ b/muranodashboard/dynamic_ui/fields.py @@ -440,6 +440,9 @@ class KeyPairChoiceField(DynamicChoiceField): for keypair in sorted(keypairs, key=lambda e: e.name): self.choices.append((keypair.name, keypair.name)) + if len(keypairs) == 1: + self.initial = keypairs[0].name + class SecurityGroupChoiceField(DynamicChoiceField): """This widget allows to select a security group for VMs""" @@ -501,8 +504,11 @@ class ImageChoiceField(ChoiceField): for id_, title in sorted(six.iteritems(image_map), key=lambda e: e[1].title): image_choices.append((id_, title)) + if image_choices: - image_choices.insert(0, ("", _("Select Image"))) + # If only one choice, select it as only option + if len(image_choices) != 1: + image_choices.insert(0, ("", _("Select Image"))) else: image_choices.insert(0, ("", _("No images available"))) diff --git a/muranodashboard/tests/unit/dynamic_ui/test_fields.py b/muranodashboard/tests/unit/dynamic_ui/test_fields.py index ccf765275..3d7543753 100644 --- a/muranodashboard/tests/unit/dynamic_ui/test_fields.py +++ b/muranodashboard/tests/unit/dynamic_ui/test_fields.py @@ -624,7 +624,10 @@ class TestImageChoiceField(unittest.TestCase): status='active'), # Test whether second continue statement works. mock.Mock(id='bar_image_id', murano_property={ - 'title': 'foo_image_title', 'type': 'jpg'}, + 'title': 'bar_image_title', 'type': 'png'}, + status='active'), + mock.Mock(id='baz_image_id', murano_property={ + 'title': 'baz_image_title', 'type': 'jpg'}, status='active') ] image_choice_field = fields.ImageChoiceField() @@ -634,9 +637,17 @@ class TestImageChoiceField(unittest.TestCase): self.assertEqual(("", _("Select Image")), image_choice_field.choices[0]) - self.assertEqual("foo_image_id", image_choice_field.choices[1][0]) + self.assertEqual("bar_image_id", image_choice_field.choices[1][0]) self.assertIsInstance(image_choice_field.choices[1][1], fields.Choice) + # Test that 'Select Image' is dropped if only one result + image_choice_field.image_type = 'jpg' + image_choice_field.choices = [] + image_choice_field.update(self.request) + + self.assertEqual('baz_image_id', image_choice_field.choices[0][0]) + self.assertIsInstance(image_choice_field.choices[0][1], fields.Choice) + # Test whether first continue statement works. mock_get_murano_images.return_value = [ mock.Mock(murano_property={