summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhangshuai <446077695@qq.com>2017-03-02 06:34:35 +0800
committerzhangshuai <446077695@qq.com>2017-03-20 03:24:11 +0800
commit5497479156c45f953af3cf8d96e9b378ea3d068f (patch)
treef7fd61d33e9e2edd71c480793997f7cb7ad74ff0
parentde064cb87c61c7a7ac57e55c5f21bc2d59d651a5 (diff)
Fix restore form page
Make restore target optional, user can use default target or specified restore target by choose the checkbox. Change-Id: I732dcb684c4b60568817f8fa9ff806955ebaa9a6
Notes
Notes (review): Code-Review+2: Yuval Brik <yuval@brik.org.il> Code-Review+2: chenying <ying.chen@huawei.com> Workflow+1: chenying <ying.chen@huawei.com> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Wed, 22 Mar 2017 07:34:34 +0000 Reviewed-on: https://review.openstack.org/440005 Project: openstack/karbor-dashboard Branch: refs/heads/master
-rw-r--r--karbor_dashboard/checkpoints/forms.py74
-rw-r--r--karbor_dashboard/static/karbordashboard/js/checkpoints.restore.js44
2 files changed, 100 insertions, 18 deletions
diff --git a/karbor_dashboard/checkpoints/forms.py b/karbor_dashboard/checkpoints/forms.py
index 8918602..5ab0b07 100644
--- a/karbor_dashboard/checkpoints/forms.py
+++ b/karbor_dashboard/checkpoints/forms.py
@@ -12,7 +12,6 @@
12# License for the specific language governing permissions and limitations 12# License for the specific language governing permissions and limitations
13# under the License. 13# under the License.
14 14
15from django.core import validators
16from django import forms 15from django import forms
17from django.utils.translation import ugettext_lazy as _ 16from django.utils.translation import ugettext_lazy as _
18from django.views.decorators.debug import sensitive_variables # noqa 17from django.views.decorators.debug import sensitive_variables # noqa
@@ -24,6 +23,8 @@ from horizon import messages
24import json 23import json
25from karbor_dashboard.api import karbor as karborclient 24from karbor_dashboard.api import karbor as karborclient
26 25
26EMPTY_VALUES = (None, '', u'', [], (), {})
27
27 28
28class RestoreCheckpointForm(horizon_forms.SelfHandlingForm): 29class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
29 provider_id = forms.CharField(label=_("Provider ID"), 30 provider_id = forms.CharField(label=_("Provider ID"),
@@ -32,16 +33,45 @@ class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
32 checkpoint_id = forms.CharField(label=_("Checkpoint ID"), 33 checkpoint_id = forms.CharField(label=_("Checkpoint ID"),
33 widget=forms.HiddenInput(), 34 widget=forms.HiddenInput(),
34 required=False) 35 required=False)
35 restore_target = forms.CharField( 36 use_current_session = forms.BooleanField(
37 label=_("Use current session credentials"),
38 widget=forms.CheckboxInput(attrs={
39 'class': 'disable_input',
40 'data-slug': 'use_current_session',
41 'data-disable-on-checked': 'true',
42 'checked': 'checked'
43 }),
44 initial=False,
45 required=False)
46 restore_target = forms.URLField(
36 label=_("Restore Target"), 47 label=_("Restore Target"),
37 required=False, 48 widget=forms.URLInput(attrs={
38 validators=[validators.URLValidator(), ]) 49 'class': 'disabled_input',
50 'data-disable-on': 'use_current_session',
51 'data-source-manual': _("Restore Target"),
52 'disabled': 'disabled',
53 'value': 'Target: Current project'
54 }),
55 required=False)
39 restore_target_username = forms.CharField( 56 restore_target_username = forms.CharField(
40 label=_("Restore Target Username"), 57 label=_("Restore Target Username"),
58 widget=forms.TextInput(attrs={
59 'class': 'disabled_input',
60 'data-disable-on': 'use_current_session',
61 'data-source-manual': _("Restore Target Username"),
62 'disabled': 'disabled',
63 'value': 'Target Username: current user'
64 }),
41 required=False) 65 required=False)
42 restore_target_password = forms.CharField( 66 restore_target_password = forms.CharField(
43 label=_("Restore Target Password"), 67 label=_("Restore Target Password"),
44 widget=forms.PasswordInput(), 68 widget=forms.PasswordInput(attrs={
69 'class': 'disabled_input',
70 'data-disable-on': 'use_current_session',
71 'data-source-manual': _("Restore Target Password"),
72 'disabled': 'disabled',
73 'hidden': 'hidden'
74 }),
45 required=False) 75 required=False)
46 provider = forms.CharField( 76 provider = forms.CharField(
47 widget=forms.HiddenInput(attrs={"class": "provider"})) 77 widget=forms.HiddenInput(attrs={"class": "provider"}))
@@ -59,21 +89,29 @@ class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
59 89
60 @sensitive_variables('restore_target_password') 90 @sensitive_variables('restore_target_password')
61 def handle(self, request, data): 91 def handle(self, request, data):
62 def empty_validate(data_): 92 def all_empty(data_list):
63 return data_ in validators.EMPTY_VALUES 93 return all(map(lambda x: x in EMPTY_VALUES, data_list))
94
95 def all_not_empty(data_list):
96 return all(map(lambda x: x not in EMPTY_VALUES, data_list))
97
98 def empty_to_none(data_):
99 return data_ if data_ not in EMPTY_VALUES else None
64 100
65 target = data["restore_target"] 101 target = empty_to_none(data["restore_target"])
66 target_username = data["restore_target_username"] 102 target_username = empty_to_none(data["restore_target_username"])
67 target_password = data["restore_target_password"] 103 target_password = empty_to_none(data["restore_target_password"])
104 use_current_session = empty_to_none(data["use_current_session"])
68 105
69 if not ((target and target_username and target_password) or 106 if not use_current_session:
70 all(map(empty_validate, 107 validate_data = [target, target_username, target_password]
71 [target, target_username, target_password]))): 108 if not (all_empty(validate_data) or all_not_empty(validate_data)):
72 messages.warning(request, 109 messages.warning(request,
73 _('Restore Target, Restore Target Username and ' 110 _('Restore Target, Restore Target Username '
74 'Restore Target Password must be assigned at ' 111 'and Restore Target Password must be '
75 'the same time or not assigned.')) 112 'assigned at the same time or not '
76 return False 113 'assigned.'))
114 return False
77 115
78 try: 116 try:
79 data_parameters = json.loads(data["parameters"]) 117 data_parameters = json.loads(data["parameters"])
diff --git a/karbor_dashboard/static/karbordashboard/js/checkpoints.restore.js b/karbor_dashboard/static/karbordashboard/js/checkpoints.restore.js
index 02daa48..a22ee33 100644
--- a/karbor_dashboard/static/karbordashboard/js/checkpoints.restore.js
+++ b/karbor_dashboard/static/karbordashboard/js/checkpoints.restore.js
@@ -96,5 +96,49 @@ horizon.checkpoints_restore = {
96 $(document).on('click', "#parametersdialog a.close", function() { 96 $(document).on('click', "#parametersdialog a.close", function() {
97 $.Karbor.closeDialog("#parametersdialog"); 97 $.Karbor.closeDialog("#parametersdialog");
98 }); 98 });
99
100 $(document).on('change', "input.disable_input", function (evt) {
101 var $fieldset = $(evt.target).closest('fieldset'),
102 $disable_inputs = $fieldset.find('input.disable_input');
103
104 $disable_inputs.each(function(index, disable_input){
105 var $disable_input = $(disable_input),
106 visible = $disable_input.parent().hasClass('themable-checkbox') ? $disable_input.siblings('label').is(':visible') : $disable_input.is(':visible'),
107 slug = $disable_input.data('slug'),
108 disabled = $disable_input.prop('checked'),
109 disable_on = $disable_input.data('disableOnChecked');
110
111 // If checkbox is hidden then do not apply any further logic
112 if (!visible) return;
113
114 function handle_disabled_field(index, input){
115 var $input = $(input);
116
117 if (disabled != disable_on) {
118 $input.val("");
119 $input.attr("disabled", false);
120 if ($input.attr('id') == "id_restore_target_password"){
121 $input.closest('.form-group').removeClass("hide");
122 }
123 } else {
124 if ($input.attr('id') == 'id_restore_target'){
125 $input.val("Target: local host");
126 }
127 if ($input.attr('id') == 'id_restore_target_username'){
128 $input.val("Target username: current project");
129 }
130 if ($input.attr('id') == 'id_restore_target_password'){
131 $input.closest('.form-group').addClass("hide");
132 }
133 $input.attr("disabled", true);
134 }
135 }
136
137 $fieldset.find('.disabled_input[data-disable-on*="' + slug + '"]').each(handle_disabled_field);
138 $fieldset.siblings().find('.disabled_input[data-disable-on*="' + slug + '"]').each(handle_disabled_field);
139 });
140 });
141
142 $("input[name='restore_target_password']").closest('.form-group').addClass("hide");
99 } 143 }
100}; 144};