From d3f3c06a05cde41b23dab610851e8ba82792d6be Mon Sep 17 00:00:00 2001 From: zhangshuai <446077695@qq.com> Date: Mon, 16 Jan 2017 13:46:05 +0800 Subject: [PATCH] Plan edit - change resource and parameters User is unable to update the Protection Plan from the dashboard - updating name, status, resources and parameters. Change-Id: I5dfcc9507689a27d1a6bfdc9ca8f9f4711ba797a Closes-Bug: #1643338 --- karbor_dashboard/protectionplans/forms.py | 47 ++++++ karbor_dashboard/protectionplans/tables.py | 12 +- karbor_dashboard/protectionplans/urls.py | 2 + karbor_dashboard/protectionplans/views.py | 85 ++++++++++- .../js/protectionplans.update.js | 143 ++++++++++++++++++ .../templates/protectionplans/_update.html | 69 +++++++++ .../templates/protectionplans/update.html | 10 ++ 7 files changed, 366 insertions(+), 2 deletions(-) create mode 100644 karbor_dashboard/static/karbordashboard/js/protectionplans.update.js create mode 100644 karbor_dashboard/templates/protectionplans/_update.html create mode 100644 karbor_dashboard/templates/protectionplans/update.html diff --git a/karbor_dashboard/protectionplans/forms.py b/karbor_dashboard/protectionplans/forms.py index 3e622ea..a99c0fd 100644 --- a/karbor_dashboard/protectionplans/forms.py +++ b/karbor_dashboard/protectionplans/forms.py @@ -22,6 +22,9 @@ from horizon import messages import json from karbor_dashboard.api import karbor as karborclient +STATUS_CHOICE = [("suspended", "suspended"), + ("started", "started")] + class CreateProtectionPlanForm(horizon_forms.SelfHandlingForm): name = forms.CharField(label=_("Name")) @@ -78,6 +81,50 @@ class CreateProtectionPlanForm(horizon_forms.SelfHandlingForm): exceptions.handle(request, _('Unable to create protection plan.')) +class UpdateProtectionPlanForm(horizon_forms.SelfHandlingForm): + name = forms.CharField(label=_("Name"), max_length=255, required=False) + status = forms.ChoiceField(label=_('Status'), + choices=STATUS_CHOICE, + widget=forms.Select(attrs={ + 'class': 'switchable'})) + plan = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "plan"})) + provider = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "provider"})) + resources = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "resources"})) + parameters = forms.CharField( + widget=forms.HiddenInput(attrs={"class": "parameters"})) + + def handle(self, request, data): + plan_id = self.initial['plan_id'] + status = data["status"] + data_ = {"status": status} + + name = data["name"] + if name: + data_.update({"name": name}) + + resources = json.loads(data["resources"]) + if resources: + resources_ = [] + for resource in resources: + if resource not in resources_: + resources_.append(resource) + data_.update({"resources": resources_}) + + try: + new_plan = karborclient.plan_update(request, + plan_id, + data_) + messages.success(request, + _("Protection Plan updated successfully.")) + return new_plan + except Exception as e: + msg = _('Unable to update protection plan. ') + e.message + exceptions.handle(request, msg) + + class ScheduleProtectForm(horizon_forms.SelfHandlingForm): id = forms.CharField(label=_("ID"), widget=forms.HiddenInput) name = forms.CharField(label=_("Name"), widget=forms.HiddenInput) diff --git a/karbor_dashboard/protectionplans/tables.py b/karbor_dashboard/protectionplans/tables.py index b15f0b8..dd37be5 100644 --- a/karbor_dashboard/protectionplans/tables.py +++ b/karbor_dashboard/protectionplans/tables.py @@ -43,6 +43,16 @@ class ScheduleProtectLink(tables.LinkAction): return True +class EditPlanLink(tables.LinkAction): + name = "editplan" + verbose_name = _("Edit Plan") + url = "horizon:karbor:protectionplans:update" + classes = ("ajax-modal",) + + def allowed(self, request, protectionplan): + return True + + class ProtectNowLink(tables.Action): name = "protectnow" verbose_name = _("Protect Now") @@ -110,7 +120,7 @@ class ProtectionPlansTable(tables.DataTable): class Meta(object): name = 'protectionplans' verbose_name = _('Protection Plans') - row_actions = (ScheduleProtectLink, ProtectNowLink, + row_actions = (ScheduleProtectLink, EditPlanLink, ProtectNowLink, DeleteProtectionPlansAction) table_actions = (ProtectionPlanFilterAction, CreateProtectionPlanLink, DeleteProtectionPlansAction) diff --git a/karbor_dashboard/protectionplans/urls.py b/karbor_dashboard/protectionplans/urls.py index d9e9085..fa66949 100644 --- a/karbor_dashboard/protectionplans/urls.py +++ b/karbor_dashboard/protectionplans/urls.py @@ -23,4 +23,6 @@ urlpatterns = [ views.ScheduleProtectView.as_view(), name='scheduleprotect'), url(r'^(?P[^/]+)/detail/$', views.DetailView.as_view(), name='detail'), + url(r'^(?P[^/]+)/update/$', + views.UpdateView.as_view(), name='update'), ] diff --git a/karbor_dashboard/protectionplans/views.py b/karbor_dashboard/protectionplans/views.py index d0798eb..c64dcf7 100644 --- a/karbor_dashboard/protectionplans/views.py +++ b/karbor_dashboard/protectionplans/views.py @@ -12,6 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. +import json +import uuid + from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse_lazy from django.utils.translation import ugettext_lazy as _ @@ -26,7 +29,6 @@ from karbor_dashboard.api import karbor as karborclient from karbor_dashboard.protectionplans import forms from karbor_dashboard.protectionplans import tables from karborclient.v1 import protectables -import uuid class IndexView(horizon_tables.DataTableView): @@ -123,6 +125,87 @@ class CreateView(horizon_forms.ModalFormView): self.get_results([dependent], result.showid, results) +class UpdateView(horizon_forms.ModalFormView): + template_name = 'protectionplans/update.html' + modal_header = _("Update Protection Plan") + form_id = "update_protectionplan_form" + form_class = forms.UpdateProtectionPlanForm + submit_label = _("Update Protection Plan") + submit_url = "horizon:karbor:protectionplans:update" + success_url = reverse_lazy('horizon:karbor:protectionplans:index') + page_title = _("Update Protection Plan") + + def get_context_data(self, **kwargs): + context = super(UpdateView, self).get_context_data(**kwargs) + context["instances"] = self.get_protectable_objects() + args = (self.kwargs['plan_id'],) + context['submit_url'] = reverse(self.submit_url, args=args) + return context + + @memoized.memoized_method + def get_protectable_objects(self): + try: + instances = karborclient.protectable_list_instances( + self.request, "OS::Keystone::Project") + results = [] + self.get_results(instances, None, results) + return results + except Exception: + exceptions.handle( + self.request, + _('Unable to retrieve protection resources.'), + redirect=reverse("horizon:karbor:protectionplans:index")) + + def get_results(self, instances, showparentid, results): + for instance in instances: + if instance is not None: + resource = {} + resource["id"] = instance.id + resource["type"] = instance.type + resource["name"] = instance.name + resource["showid"] = str(uuid.uuid4()) + resource["showparentid"] = showparentid + result = protectables.Instances(self, resource) + results.append(result) + + for dependent_resource in instance.dependent_resources: + if dependent_resource is not None: + dependent = karborclient.protectable_get_instance( + self.request, + dependent_resource["type"], + dependent_resource["id"]) + self.get_results([dependent], result.showid, results) + + @memoized.memoized_method + def get_plan_object(self, *args, **kwargs): + plan_id = self.kwargs['plan_id'] + try: + return karborclient.plan_get(self.request, plan_id) + except Exception: + redirect = reverse("horizon:karbor:protectionplans:index") + msg = _('Unable to retrieve protection plan details.') + exceptions.handle(self.request, msg, redirect=redirect) + + @memoized.memoized_method + def get_provider_object(self, provider_id): + try: + return karborclient.provider_get(self.request, provider_id) + except Exception: + redirect = reverse("horizon:karbor:protectionplans:index") + msg = _('Unable to retrieve provider details.') + exceptions.handle(self.request, msg, redirect=redirect) + + def get_initial(self): + initial = super(UpdateView, self).get_initial() + plan = self.get_plan_object() + provider = self.get_provider_object(plan.provider_id) + initial.update({'plan_id': self.kwargs['plan_id'], + 'name': getattr(plan, 'name', ''), + 'plan': json.dumps(plan._info), + 'provider': json.dumps(provider._info)}) + return initial + + class ScheduleProtectView(horizon_forms.ModalFormView): template_name = 'protectionplans/scheduleprotect.html' modal_header = _("Schedule Protect") diff --git a/karbor_dashboard/static/karbordashboard/js/protectionplans.update.js b/karbor_dashboard/static/karbordashboard/js/protectionplans.update.js new file mode 100644 index 0000000..752bf93 --- /dev/null +++ b/karbor_dashboard/static/karbordashboard/js/protectionplans.update.js @@ -0,0 +1,143 @@ +/* Copyright (c) 2016 Huawei, 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. +*/ + +/* set the children check */ +function setChecked(node, isChecked) { + if(isChecked) { + node.row.find("input[type=checkbox]").prop("checked", true); + } + else { + node.row.find("input[type=checkbox]").removeAttr("checked"); + } + for(var i = 0; i + {% endcompress %} +{% endblock %} + + +
+
+ {% include "horizon/common/_form_fields.html" %} +
+
+ + + + + + + + + + {% for instance in instances %} + + + + + + {% endfor %} + +
+ Resource Name + + Resource Type + + Actions +
+ + + {{instance.name}} + + {{instance.type}} + + +
+
+
+
+ +{% endblock %} + +{% block modal-footer %} + Cancel + +{% endblock %} diff --git a/karbor_dashboard/templates/protectionplans/update.html b/karbor_dashboard/templates/protectionplans/update.html new file mode 100644 index 0000000..b7c8ea6 --- /dev/null +++ b/karbor_dashboard/templates/protectionplans/update.html @@ -0,0 +1,10 @@ +{% extends 'base.html' %} +{% load i18n %} + +{% block title %} + {% trans "Update Protection Plan" %} +{% endblock %} + +{% block main %} + {% include 'protectionplans/_update.html' %} +{% endblock %}