Check title in mark image form
Title should be unique To prevent extra api call to glance hidden fiels was used Change-Id: Iea3da169121a7c2947600ed87e524d22e0df7282 Closes-Bug: 1259522
This commit is contained in:
parent
adee5d87fd
commit
8e1869db91
|
@ -13,17 +13,37 @@
|
|||
# under the License.
|
||||
|
||||
import logging
|
||||
import json
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.forms import ValidationError
|
||||
from horizon.forms import SelfHandlingForm
|
||||
from horizon import messages, exceptions
|
||||
from openstack_dashboard.api import glance
|
||||
import json
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def filter_murano_images(images, request=None):
|
||||
marked_images = []
|
||||
for image in images:
|
||||
metadata = image.properties.get('murano_image_info')
|
||||
if metadata:
|
||||
try:
|
||||
metadata = json.loads(metadata)
|
||||
except ValueError:
|
||||
msg = _('Invalid metadata for image: {0}'.format(image.id))
|
||||
log.warn(msg)
|
||||
if request:
|
||||
exceptions.handle(request, msg)
|
||||
else:
|
||||
image.title = metadata.get('title', 'No Title')
|
||||
image.type = metadata.get('type', 'No Type')
|
||||
|
||||
marked_images.append(image)
|
||||
return marked_images
|
||||
|
||||
|
||||
class MarkImageForm(SelfHandlingForm):
|
||||
_metadata = {
|
||||
'windows.2012': ' Windows Server 2012',
|
||||
|
@ -34,6 +54,7 @@ class MarkImageForm(SelfHandlingForm):
|
|||
image = forms.ChoiceField(label='Image')
|
||||
title = forms.CharField(max_length="255", label=_("Title"))
|
||||
type = forms.ChoiceField(label="Type", choices=_metadata.items())
|
||||
existing_titles = forms.CharField(widget=forms.HiddenInput)
|
||||
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
super(MarkImageForm, self).__init__(request, *args, **kwargs)
|
||||
|
@ -46,6 +67,8 @@ class MarkImageForm(SelfHandlingForm):
|
|||
exceptions.handle(request, _('Unable to retrieve list of images'))
|
||||
|
||||
self.fields['image'].choices = [(i.id, i.name) for i in images]
|
||||
self.fields['existing_titles'].initial = \
|
||||
[image.title for image in filter_murano_images(images)]
|
||||
|
||||
def handle(self, request, data):
|
||||
log.debug('Marking image with specified metadata: {0}'.format(data))
|
||||
|
@ -65,3 +88,13 @@ class MarkImageForm(SelfHandlingForm):
|
|||
except Exception:
|
||||
exceptions.handle(request, _('Unable to mark image'),
|
||||
redirect='horizon:murano:images:index')
|
||||
|
||||
def clean_title(self):
|
||||
cleaned_data = super(MarkImageForm, self).clean()
|
||||
title = cleaned_data.get('title')
|
||||
existing_titles = self.fields['existing_titles'].initial
|
||||
if title in existing_titles:
|
||||
raise ValidationError(_('Specified title already in use.'
|
||||
' Please choose another one.'))
|
||||
|
||||
return title
|
||||
|
|
|
@ -13,21 +13,15 @@
|
|||
# under the License.
|
||||
|
||||
import logging
|
||||
import json
|
||||
|
||||
from django.core.urlresolvers import reverse, reverse_lazy
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from openstack_dashboard.api import glance
|
||||
from horizon import exceptions
|
||||
from horizon import tables
|
||||
from horizon import messages
|
||||
from horizon.forms.views import ModalFormView
|
||||
from .tables import MarkedImagesTable
|
||||
|
||||
from .forms import MarkImageForm
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
from .forms import MarkImageForm, filter_murano_images
|
||||
|
||||
|
||||
class MarkedImagesView(tables.DataTableView):
|
||||
|
@ -43,23 +37,7 @@ class MarkedImagesView(tables.DataTableView):
|
|||
uri = reverse('horizon:murano:images:index')
|
||||
|
||||
exceptions.handle(self.request, msg, redirect=uri)
|
||||
|
||||
marked_images = []
|
||||
for image in images:
|
||||
metadata = image.properties.get('murano_image_info')
|
||||
if metadata:
|
||||
try:
|
||||
metadata = json.loads(metadata)
|
||||
except ValueError:
|
||||
msg = _('Invalid metadata for image: {0}'.format(image.id))
|
||||
LOG.warn(msg)
|
||||
messages.error(self.request, msg)
|
||||
else:
|
||||
image.title = metadata.get('title', 'No Title')
|
||||
image.type = metadata.get('type', 'No Type')
|
||||
|
||||
marked_images.append(image)
|
||||
return marked_images
|
||||
return filter_murano_images(images, request=self.request)
|
||||
|
||||
|
||||
class MarkImageView(ModalFormView):
|
||||
|
|
Loading…
Reference in New Issue