tuskar-ui-extras/tuskar_boxes/overview/forms.py
Radomir Dopieralski 17b976a089 Reset role's nodes to 0 if it has no flavor
Also, use names instead of ids for refering to flavors

Change-Id: I523772cbb90919cdc09bbce1d8da8bef60f7f74f
2015-01-23 11:23:03 +01:00

69 lines
2.4 KiB
Python

# -*- coding: utf8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import django.forms
from django.utils.translation import ugettext_lazy as _
import horizon.exceptions
from tuskar_ui.infrastructure.overview import forms
LOG = logging.getLogger(__name__)
class EditPlan(forms.EditPlan):
def __init__(self, *args, **kwargs):
super(EditPlan, self).__init__(*args, **kwargs)
self.fields.update(self._role_flavor_fields(self.plan))
def _role_flavor_fields(self, plan):
fields = {}
for role in plan.role_list:
field = django.forms.CharField(
label=_("Flavor for {0}").format(role.name),
initial=role.flavor(plan).name if role.flavor(plan) else '',
required=False,
widget=django.forms.HiddenInput(attrs={
'class': "boxes-flavor",
}),
)
field.role = role
fields['%s-flavor' % role.id] = field
return fields
def handle(self, request, data):
result = super(EditPlan, self).handle(request, data)
parameters = dict(
(field.role.flavor_parameter_name, data[name])
for (name, field) in self.fields.items()
if name.endswith('-flavor')
)
try:
self.plan = self.plan.patch(request, self.plan.uuid, parameters)
except Exception as e:
horizon.exceptions.handle(request, _("Unable to update the plan."))
LOG.exception(e)
return False
return result
def clean(self):
cleaned_data = super(EditPlan, self).clean()
# If a role has no flavor, it should have no nodes.
for key, value in cleaned_data.items():
if key.endswith('-flavor'):
if not value:
cleaned_data[key.replace('-flavor', '-count')] = 0
return cleaned_data