Browse Source

Ensure only one prompt for cidr and gateway_ip

This commit makes 3 changes:
 - There should be only one prompt for field self. But required check
   in method clean checks the cleaned data, instead of the original value.
   So this commit set the fields required and remove the prompt when
   unnecessary.
 - According to below code [1], we know that initial equals context,
   and hidden with_subnet under subnet tab will conflict with
   the one under networks tab.
   So, this commit get with_subnet from initial, instead of cleaned_data.
 - Even with_subnet is False, the field checks are also valid and prevent
   the submitting. So, this commit remove erros for there fields.

[1] 3730ec164e/horizon/workflows/base.py (L157)

Change-Id: I8fd14861c33e60cba90d4193ce8953a3b27bbe26
Closes-Bug: #1812142
Wangliangyu 3 months ago
parent
commit
2bbd0e2509

+ 32
- 3
openstack_dashboard/dashboards/project/networks/subnets/workflows.py View File

@@ -32,6 +32,19 @@ LOG = logging.getLogger(__name__)
32 32
 class CreateSubnetInfoAction(network_workflows.CreateSubnetInfoAction):
33 33
     with_subnet = forms.BooleanField(initial=True, required=False,
34 34
                                      widget=forms.HiddenInput())
35
+    cidr = forms.IPField(label=_("Network Address"),
36
+                         initial="",
37
+                         error_messages={
38
+                             'required': _("Specify network address")},
39
+                         widget=forms.TextInput(attrs={
40
+                             'class': 'switched',
41
+                             'data-switch-on': 'source',
42
+                             'data-source-manual': _("Network Address"),
43
+                         }),
44
+                         help_text=_("Network address in CIDR format "
45
+                                     "(e.g. 192.168.0.0/24, 2001:DB8::/48)"),
46
+                         version=forms.IPv4 | forms.IPv6,
47
+                         mask=True)
35 48
 
36 49
     class Meta(object):
37 50
         name = _("Subnet")
@@ -41,7 +54,7 @@ class CreateSubnetInfoAction(network_workflows.CreateSubnetInfoAction):
41 54
 
42 55
     def clean(self):
43 56
         cleaned_data = workflows.Action.clean(self)
44
-        self._check_subnet_data(cleaned_data, with_network_form=False)
57
+        self._check_subnet_data(cleaned_data)
45 58
         return cleaned_data
46 59
 
47 60
 
@@ -105,6 +118,23 @@ class UpdateSubnetInfoAction(CreateSubnetInfoAction):
105 118
     ip_version = forms.ThemableChoiceField(choices=[(4, 'IPv4'), (6, 'IPv6')],
106 119
                                            widget=forms.HiddenInput(),
107 120
                                            label=_("IP Version"))
121
+    gateway_ip = forms.IPField(
122
+        label=_("Gateway IP"),
123
+        widget=forms.TextInput(attrs={
124
+            'class': 'switched',
125
+            'data-switch-on': 'gateway_ip',
126
+            'data-source-manual': _("Gateway IP")
127
+        }),
128
+        initial="",
129
+        error_messages={
130
+            'required': _('Specify IP address of gateway or '
131
+                          'check "Disable Gateway" checkbox.')
132
+        },
133
+        help_text=_("IP address of Gateway (e.g. 192.168.0.254) "
134
+                    "If you do not want to use a gateway, "
135
+                    "check 'Disable Gateway' below."),
136
+        version=forms.IPv4 | forms.IPv6,
137
+        mask=False)
108 138
 
109 139
     class Meta(object):
110 140
         name = _("Subnet")
@@ -114,8 +144,7 @@ class UpdateSubnetInfoAction(CreateSubnetInfoAction):
114 144
 
115 145
     def clean(self):
116 146
         cleaned_data = workflows.Action.clean(self)
117
-        self._check_subnet_data(cleaned_data, is_create=False,
118
-                                with_network_form=False)
147
+        self._check_subnet_data(cleaned_data)
119 148
         return cleaned_data
120 149
 
121 150
 

+ 15
- 26
openstack_dashboard/dashboards/project/networks/workflows.py View File

@@ -139,11 +139,13 @@ class CreateSubnetInfoAction(workflows.Action):
139 139
                                   required=False)
140 140
 
141 141
     cidr = forms.IPField(label=_("Network Address"),
142
-                         required=False,
143 142
                          initial="",
143
+                         error_messages={
144
+                             'required': _('Specify "Network Address" or '
145
+                                           'clear "Create Subnet" checkbox '
146
+                                           'in previous step.')},
144 147
                          widget=forms.TextInput(attrs={
145 148
                              'class': 'switched',
146
-                             'data-required-when-shown': 'true',
147 149
                              'data-switch-on': 'source',
148 150
                              'data-source-manual': _("Network Address"),
149 151
                          }),
@@ -202,13 +204,6 @@ class CreateSubnetInfoAction(workflows.Action):
202 204
     def __init__(self, request, context, *args, **kwargs):
203 205
         super(CreateSubnetInfoAction, self).__init__(request, context, *args,
204 206
                                                      **kwargs)
205
-        if 'with_subnet' in context:
206
-            self.fields['with_subnet'] = forms.BooleanField(
207
-                initial=context['with_subnet'],
208
-                required=False,
209
-                widget=forms.HiddenInput()
210
-            )
211
-
212 207
         if not getattr(settings, 'OPENSTACK_NEUTRON_NETWORK',
213 208
                        {}).get('enable_ipv6', True):
214 209
             self.fields['ip_version'].widget = forms.HiddenInput()
@@ -279,8 +274,7 @@ class CreateSubnetInfoAction(workflows.Action):
279 274
                         'allowed': range_str})
280 275
                 raise forms.ValidationError(msg)
281 276
 
282
-    def _check_subnet_data(self, cleaned_data, is_create=True,
283
-                           with_network_form=True):
277
+    def _check_subnet_data(self, cleaned_data):
284 278
         cidr = cleaned_data.get('cidr')
285 279
         ip_version = int(cleaned_data.get('ip_version'))
286 280
         gateway_ip = cleaned_data.get('gateway_ip')
@@ -293,14 +287,9 @@ class CreateSubnetInfoAction(workflows.Action):
293 287
                     '"Enter Network Address manually" and specify '
294 288
                     '"Network Address".')
295 289
             raise forms.ValidationError(msg)
296
-        if not cidr and address_source != 'subnetpool':
297
-            if with_network_form:
298
-                msg = _('Specify "Network Address" or '
299
-                        'clear "Create Subnet" checkbox in previous step.')
300
-            else:
301
-                msg = _("Specify network address")
302
-            raise forms.ValidationError(msg)
303
-        if cidr:
290
+        if address_source == 'subnetpool' and 'cidr' in self._errors:
291
+            del self._errors['cidr']
292
+        elif cidr:
304 293
             subnet = netaddr.IPNetwork(cidr)
305 294
             if subnet.version != ip_version:
306 295
                 msg = _('Network Address and IP version are inconsistent.')
@@ -316,18 +305,18 @@ class CreateSubnetInfoAction(workflows.Action):
316 305
             if netaddr.IPAddress(gateway_ip).version is not ip_version:
317 306
                 msg = _('Gateway IP and IP version are inconsistent.')
318 307
                 raise forms.ValidationError(msg)
319
-        if not is_create and not no_gateway and not gateway_ip:
320
-            msg = _('Specify IP address of gateway or '
321
-                    'check "Disable Gateway" checkbox.')
322
-            raise forms.ValidationError(msg)
323 308
         if no_gateway and 'gateway_ip' in self._errors:
324 309
             del self._errors['gateway_ip']
325 310
 
311
+    def _remove_fields_errors(self):
312
+        self._errors = {}
313
+
326 314
     def clean(self):
327
-        cleaned_data = super(CreateSubnetInfoAction, self).clean()
328
-        with_subnet = cleaned_data.get('with_subnet')
315
+        with_subnet = self.initial.get('with_subnet')
329 316
         if not with_subnet:
330
-            return cleaned_data
317
+            self._remove_fields_errors()
318
+            return None
319
+        cleaned_data = super(CreateSubnetInfoAction, self).clean()
331 320
         self._check_subnet_data(cleaned_data)
332 321
         return cleaned_data
333 322
 

Loading…
Cancel
Save