Sort image source choices by name for volume

When creating a volume and selecting to use image as a
source we get a dropdown of the images that we can select
but this list is not sorted based on image name.

This changes so that it's sorted by the image name
ascending to match the instance launch dialog that
already lists images in ascending order by name.

Change-Id: I17212e460e307e08b2b6c2a8a68a14ffde8cc04b
(cherry picked from commit c18f9d90ae)
This commit is contained in:
Tobias Urdin 2024-08-20 11:09:50 +02:00
parent dcaf0cc51f
commit 88c087e107

View File

@ -252,12 +252,15 @@ class CreateForm(forms.SelfHandlingForm):
request.user.tenant_id)
if images:
source_type_choices.append(("image_source", _("Image")))
choices = [('', _("Choose an image"))]
choices = []
for image in images:
image.bytes = image.size
image.size = functions.bytes_to_gigabytes(image.bytes)
choices.append((image.id, image))
self.fields['image_source'].choices = choices
sorted_choices = sorted(
choices, key=lambda x: (x[1].name if x[1].name else ''))
sorted_choices.insert(0, ('', _("Choose an image")))
self.fields['image_source'].choices = sorted_choices
else:
del self.fields['image_source']