From 953369c870ebc41e79525ffc4898aa6d7b10113a Mon Sep 17 00:00:00 2001 From: Pavlo Shchelokovskyy Date: Wed, 2 Sep 2020 19:10:11 +0300 Subject: [PATCH] Move image_field_data method in-tree this was removed in Horizon as 'not needed' in 26eebd4abbbabc1475bae61c51d1cfcf14dba770 however it is used to form a hint for glance.image constraint in heat-dashboard. Just copy-paste the method as it existed in horizon with minimal adaptations. Change-Id: I23fd3f45f0ce5b67691991336e727c377ca36281 --- heat_dashboard/content/stacks/forms.py | 33 +++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/heat_dashboard/content/stacks/forms.py b/heat_dashboard/content/stacks/forms.py index b23561e..d0f7760 100644 --- a/heat_dashboard/content/stacks/forms.py +++ b/heat_dashboard/content/stacks/forms.py @@ -15,6 +15,7 @@ import logging import django from django.conf import settings +from django.template.defaultfilters import filesizeformat from django.utils import html from django.utils.translation import ugettext_lazy as _ from django.views.decorators.debug import sensitive_variables @@ -35,6 +36,36 @@ from openstack_dashboard.dashboards.project.instances \ LOG = logging.getLogger(__name__) +def image_field_data(request, include_empty_option=False): + """Returns a list of tuples of all images. + + Generates a sorted list of images available. And returns a list of + (id, name) tuples. + + :param request: django http request object + :param include_empty_option: flag to include a empty tuple in the front of + the list + + :return: list of (id, name) tuples + + """ + try: + images = image_utils.get_available_images( + request, request.user.project_id) + except Exception: + exceptions.handle(request, _('Unable to retrieve images')) + images.sort(key=lambda c: c.name) + images_list = [('', _('Select Image'))] + for image in images: + image_label = u"{} ({})".format(image.name, filesizeformat(image.size)) + images_list.append((image.id, image_label)) + + if not images: + return [("", _("No images available")), ] + + return images_list + + def create_upload_form_attributes(prefix, input_type, name): """Creates attribute dicts for the switchable upload form @@ -416,7 +447,7 @@ class CreateStackForm(forms.SelfHandlingForm): if custom_type == 'nova.keypair': return instance_utils.keypair_field_data(self.request, True) if custom_type == 'glance.image': - return image_utils.image_field_data(self.request, True) + return image_field_data(self.request, True) if custom_type == 'nova.flavor': return instance_utils.flavor_field_data(self.request, True) return []