Browse Source

Fix restore form page

Make restore target optional, user can use default target or
specified restore target by choose the checkbox.

Change-Id: I732dcb684c4b60568817f8fa9ff806955ebaa9a6
zhangshuai 2 years ago
parent
commit
5497479156

+ 56
- 18
karbor_dashboard/checkpoints/forms.py View File

@@ -12,7 +12,6 @@
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14 14
 
15
-from django.core import validators
16 15
 from django import forms
17 16
 from django.utils.translation import ugettext_lazy as _
18 17
 from django.views.decorators.debug import sensitive_variables  # noqa
@@ -24,6 +23,8 @@ from horizon import messages
24 23
 import json
25 24
 from karbor_dashboard.api import karbor as karborclient
26 25
 
26
+EMPTY_VALUES = (None, '', u'', [], (), {})
27
+
27 28
 
28 29
 class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
29 30
     provider_id = forms.CharField(label=_("Provider ID"),
@@ -32,16 +33,45 @@ class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
32 33
     checkpoint_id = forms.CharField(label=_("Checkpoint ID"),
33 34
                                     widget=forms.HiddenInput(),
34 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 47
         label=_("Restore Target"),
37
-        required=False,
38
-        validators=[validators.URLValidator(), ])
48
+        widget=forms.URLInput(attrs={
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 56
     restore_target_username = forms.CharField(
40 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 65
         required=False)
42 66
     restore_target_password = forms.CharField(
43 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 75
         required=False)
46 76
     provider = forms.CharField(
47 77
         widget=forms.HiddenInput(attrs={"class": "provider"}))
@@ -59,21 +89,29 @@ class RestoreCheckpointForm(horizon_forms.SelfHandlingForm):
59 89
 
60 90
     @sensitive_variables('restore_target_password')
61 91
     def handle(self, request, data):
62
-        def empty_validate(data_):
63
-            return data_ in validators.EMPTY_VALUES
92
+        def all_empty(data_list):
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"]
66
-        target_username = data["restore_target_username"]
67
-        target_password = data["restore_target_password"]
101
+        target = empty_to_none(data["restore_target"])
102
+        target_username = empty_to_none(data["restore_target_username"])
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
70
-                all(map(empty_validate,
71
-                        [target, target_username, target_password]))):
72
-            messages.warning(request,
73
-                             _('Restore Target, Restore Target Username and '
74
-                               'Restore Target Password must be assigned at '
75
-                               'the same time or not assigned.'))
76
-            return False
106
+        if not use_current_session:
107
+            validate_data = [target, target_username, target_password]
108
+            if not (all_empty(validate_data) or all_not_empty(validate_data)):
109
+                messages.warning(request,
110
+                                 _('Restore Target, Restore Target Username '
111
+                                   'and Restore Target Password must be '
112
+                                   'assigned at the same time or not '
113
+                                   'assigned.'))
114
+                return False
77 115
 
78 116
         try:
79 117
             data_parameters = json.loads(data["parameters"])

+ 44
- 0
karbor_dashboard/static/karbordashboard/js/checkpoints.restore.js View File

@@ -96,5 +96,49 @@ horizon.checkpoints_restore = {
96 96
     $(document).on('click', "#parametersdialog a.close", function() {
97 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
 };

Loading…
Cancel
Save