From 8aac8d5f94a0a09abbc7f12abb52d203ac014d34 Mon Sep 17 00:00:00 2001 From: Ekaterina Fedorova Date: Tue, 22 Apr 2014 10:10:31 +0400 Subject: [PATCH] Add last step to manage redirect on form submission If checked - stay at app catalog, go to the environment details otherwise Implements blueprint add-return-to-environment-checkbox Change-Id: Ifc1d78f003ede6638e66eac0762e6552a3d6df3e --- muranodashboard/catalog/forms.py | 34 +++++++++++++++++++ muranodashboard/catalog/urls.py | 3 +- muranodashboard/catalog/views.py | 6 ++-- muranodashboard/dynamic_ui/services.py | 29 ++++++++++++---- muranodashboard/environments/views.py | 20 ++++++++--- .../muranodashboard/css/hide_app_name.css | 7 ++++ .../templates/services/_wizard_create.html | 1 + 7 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 muranodashboard/catalog/forms.py create mode 100644 muranodashboard/static/muranodashboard/css/hide_app_name.css diff --git a/muranodashboard/catalog/forms.py b/muranodashboard/catalog/forms.py new file mode 100644 index 000000000..6809c9e1d --- /dev/null +++ b/muranodashboard/catalog/forms.py @@ -0,0 +1,34 @@ +# Copyright (c) 2014 Mirantis, Inc. +# +# 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. + + +class WorkflowManagementForm(object): + name = 'workflowManagement' + field_specs = [{ + 'widgetMedia': + {'css': + {'all': ('muranodashboard/css/checkbox.css', + 'muranodashboard/css/hide_app_name.css') + } + }, + 'name': 'StayAtCatalog', + 'initial': False, + 'description': 'If checked, you will be returned to the ' + 'Application Catalog page. If not - to the ' + 'Environment page, where you can deploy' + ' the application.', + 'required': False, + 'type': 'boolean', + 'label': 'Add more applications to the environment'}] + validators = [] diff --git a/muranodashboard/catalog/urls.py b/muranodashboard/catalog/urls.py index 81f7ff240..5430c8dcf 100644 --- a/muranodashboard/catalog/urls.py +++ b/muranodashboard/catalog/urls.py @@ -31,7 +31,8 @@ urlpatterns = patterns( 'switch', name='switch_env'), url(r'^add/(?P[^/]+)/(?P[^/]+)$', - env_views.Wizard.as_view(services.get_app_forms), + env_views.Wizard.as_view(services.get_app_forms, + condition_dict=services.condition_getter), name='add'), url(r'^quick-add/(?P[^/]+)$', 'quick_deploy', diff --git a/muranodashboard/catalog/views.py b/muranodashboard/catalog/views.py index 8eb035488..2258900cd 100644 --- a/muranodashboard/catalog/views.py +++ b/muranodashboard/catalog/views.py @@ -99,8 +99,10 @@ def create_quick_environment(request): def quick_deploy(request, app_id): env = create_quick_environment(request) try: - return views.Wizard.as_view(services.get_app_forms)( - request, app_id=app_id, environment_id=env.id, do_redirect=True) + view = views.Wizard.as_view(services.get_app_forms, + condition_dict=services.condition_getter) + return view(request, app_id=app_id, environment_id=env.id, + do_redirect=True, drop_wm_form=True) except: api.environment_delete(request, env.id) raise diff --git a/muranodashboard/dynamic_ui/services.py b/muranodashboard/dynamic_ui/services.py index c0a8bceff..99e674d66 100644 --- a/muranodashboard/dynamic_ui/services.py +++ b/muranodashboard/dynamic_ui/services.py @@ -13,20 +13,19 @@ # under the License. -import os -import re import logging - +import os +import re import yaml import yaql - from muranodashboard.dynamic_ui import helpers from .helpers import decamelize from muranodashboard.environments.consts import CACHE_DIR from muranodashboard.dynamic_ui import version from muranodashboard.dynamic_ui import yaql_expression from muranodashboard.dynamic_ui import yaql_functions +from muranodashboard.catalog import forms as catalog_forms LOG = logging.getLogger(__name__) @@ -74,10 +73,12 @@ class Service(object): if forms: for data in forms: name, field_specs, validators = self.extract_form_data(data) - self._add_form(name, field_specs, validators) + self._init_forms(field_specs, name, validators) - # for pickling/unpickling - self._forms.append((name, field_specs, validators)) + # Add ManageWorkflowForm + workflow_form = catalog_forms.WorkflowManagementForm + self._init_forms(workflow_form.field_specs, workflow_form.name, + workflow_form.validators) def __getstate__(self): dct = dict((k, v) for (k, v) in self.__dict__.iteritems() @@ -110,6 +111,11 @@ class Service(object): self.forms.append(Form) + def _init_forms(self, field_specs, name, validators): + self._add_form(name, field_specs, validators) + # for pickling/unpickling + self._forms.append((name, field_specs, validators)) + @staticmethod def extract_form_data(form_data): form_name = form_data.keys()[0] @@ -173,6 +179,15 @@ def import_app(request, app_id): return app +def condition_getter(request, kwargs): + def _func(wizard): + return not wizard.get_wizard_flag('drop_wm_form') + + app = import_app(request, kwargs['app_id']) + last_form_key = str(len(app.forms) - 1) + return {last_form_key: _func} + + def get_app_forms(request, kwargs): app = import_app(request, kwargs.get('app_id')) return app.forms diff --git a/muranodashboard/environments/views.py b/muranodashboard/environments/views.py index e893f265d..7387f4145 100644 --- a/muranodashboard/environments/views.py +++ b/muranodashboard/environments/views.py @@ -23,6 +23,7 @@ from django.contrib.formtools.wizard.views import SessionWizardView from django.http import HttpResponseRedirect # noqa from django.http import HttpResponse # noqa from django.views import generic +from django.utils.decorators import classonlymethod from horizon import exceptions from horizon import tabs from horizon import tables @@ -37,7 +38,6 @@ from tabs import ServicesTabs, DeploymentTabs, EnvironmentDetailsTabs from . import api from muranoclient.common.exceptions import HTTPUnauthorized, \ CommunicationError, HTTPInternalServerError, HTTPForbidden, HTTPNotFound -from django.utils.decorators import classonlymethod from muranodashboard.dynamic_ui.services import get_service_name from muranodashboard.dynamic_ui.services import get_service_field_descriptions from muranodashboard.dynamic_ui import helpers @@ -75,6 +75,11 @@ class LazyWizard(SessionWizardView): _kwargs = copy.copy(initkwargs) _kwargs = cls.get_initkwargs(forms, *initargs, **_kwargs) + + cdict = _kwargs.get('condition_dict') + if cdict and hasattr(cdict, '__call__'): + _kwargs['condition_dict'] = cdict(request, kwargs) + self = cls(**_kwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get @@ -115,6 +120,12 @@ class Wizard(hz_views.ModalFormMixin, LazyWizard): consts.DASHBOARD_ATTRS_KEY, {}) storage['name'] = app_name + wm_form_data = service.cleaned_data.get('workflowManagement') + if wm_form_data: + do_redirect = not wm_form_data['StayAtCatalog'] + else: + do_redirect = self.get_wizard_flag('do_redirect') + try: srv = api.service_create(self.request, environment_id, attributes) except HTTPForbidden: @@ -133,7 +144,7 @@ class Wizard(hz_views.ModalFormMixin, LazyWizard): messages.success(self.request, message) - if self._get_wizard_flag('do_redirect'): + if do_redirect: return HttpResponseRedirect(url) else: srv_id = getattr(srv, '?')['id'] @@ -160,7 +171,7 @@ class Wizard(hz_views.ModalFormMixin, LazyWizard): param = self.kwargs.get(key) return param if param is not None else self.request.POST.get(key) - def _get_wizard_flag(self, key): + def get_wizard_flag(self, key): value = self._get_wizard_param(key) if isinstance(value, basestring): return value.lower() == 'true' @@ -177,7 +188,8 @@ class Wizard(hz_views.ModalFormMixin, LazyWizard): 'service_name': app.name, 'app_id': app_id, 'environment_id': self.kwargs.get('environment_id'), - 'do_redirect': self._get_wizard_flag('do_redirect'), + 'do_redirect': self.get_wizard_flag('do_redirect'), + 'drop_wm_form': self.get_wizard_flag('drop_wm_form'), 'prefix': self.prefix, }) return context diff --git a/muranodashboard/static/muranodashboard/css/hide_app_name.css b/muranodashboard/static/muranodashboard/css/hide_app_name.css new file mode 100644 index 000000000..41589d0fd --- /dev/null +++ b/muranodashboard/static/muranodashboard/css/hide_app_name.css @@ -0,0 +1,7 @@ +div.right h3{ + display: none !important; +} + +div.right p strong { + display: none !important; +} diff --git a/muranodashboard/templates/services/_wizard_create.html b/muranodashboard/templates/services/_wizard_create.html index 2128f8d89..d76c8a5f6 100644 --- a/muranodashboard/templates/services/_wizard_create.html +++ b/muranodashboard/templates/services/_wizard_create.html @@ -58,6 +58,7 @@ + {% if wizard.steps.next %} {% trans "Next" as next %} {% else %}