summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWangliangyu <wangliangyu@gohighsec.com>2019-01-17 11:28:18 +0800
committerAkihiro Motoki <amotoki@gmail.com>2019-02-06 16:55:32 +0900
commit2bbd0e250917b9c7d65c067cb04d55fceccef7ae (patch)
treee16a6afacd55ac6aa7658ed3c7e7b817581f2a90
parent37aec60a8ff8fa5f73dbcffdcdbf671bc2699792 (diff)
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] https://github.com/openstack/horizon/blob/3730ec164e5a5d3d27ec46634910a0d8156984b6/horizon/workflows/base.py#L157 Change-Id: I8fd14861c33e60cba90d4193ce8953a3b27bbe26 Closes-Bug: #1812142
Notes
Notes (review): Code-Review+2: Akihiro Motoki <amotoki@gmail.com> Workflow+1: Akihiro Motoki <amotoki@gmail.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Wed, 06 Feb 2019 09:44:50 +0000 Reviewed-on: https://review.openstack.org/631386 Project: openstack/horizon Branch: refs/heads/master
-rw-r--r--openstack_dashboard/dashboards/project/networks/subnets/workflows.py35
-rw-r--r--openstack_dashboard/dashboards/project/networks/workflows.py41
2 files changed, 47 insertions, 29 deletions
diff --git a/openstack_dashboard/dashboards/project/networks/subnets/workflows.py b/openstack_dashboard/dashboards/project/networks/subnets/workflows.py
index 85ca977..0f4366e 100644
--- a/openstack_dashboard/dashboards/project/networks/subnets/workflows.py
+++ b/openstack_dashboard/dashboards/project/networks/subnets/workflows.py
@@ -32,6 +32,19 @@ LOG = logging.getLogger(__name__)
32class CreateSubnetInfoAction(network_workflows.CreateSubnetInfoAction): 32class CreateSubnetInfoAction(network_workflows.CreateSubnetInfoAction):
33 with_subnet = forms.BooleanField(initial=True, required=False, 33 with_subnet = forms.BooleanField(initial=True, required=False,
34 widget=forms.HiddenInput()) 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 class Meta(object): 49 class Meta(object):
37 name = _("Subnet") 50 name = _("Subnet")
@@ -41,7 +54,7 @@ class CreateSubnetInfoAction(network_workflows.CreateSubnetInfoAction):
41 54
42 def clean(self): 55 def clean(self):
43 cleaned_data = workflows.Action.clean(self) 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 return cleaned_data 58 return cleaned_data
46 59
47 60
@@ -105,6 +118,23 @@ class UpdateSubnetInfoAction(CreateSubnetInfoAction):
105 ip_version = forms.ThemableChoiceField(choices=[(4, 'IPv4'), (6, 'IPv6')], 118 ip_version = forms.ThemableChoiceField(choices=[(4, 'IPv4'), (6, 'IPv6')],
106 widget=forms.HiddenInput(), 119 widget=forms.HiddenInput(),
107 label=_("IP Version")) 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 class Meta(object): 139 class Meta(object):
110 name = _("Subnet") 140 name = _("Subnet")
@@ -114,8 +144,7 @@ class UpdateSubnetInfoAction(CreateSubnetInfoAction):
114 144
115 def clean(self): 145 def clean(self):
116 cleaned_data = workflows.Action.clean(self) 146 cleaned_data = workflows.Action.clean(self)
117 self._check_subnet_data(cleaned_data, is_create=False, 147 self._check_subnet_data(cleaned_data)
118 with_network_form=False)
119 return cleaned_data 148 return cleaned_data
120 149
121 150
diff --git a/openstack_dashboard/dashboards/project/networks/workflows.py b/openstack_dashboard/dashboards/project/networks/workflows.py
index 81e81ab..dcefe98 100644
--- a/openstack_dashboard/dashboards/project/networks/workflows.py
+++ b/openstack_dashboard/dashboards/project/networks/workflows.py
@@ -139,11 +139,13 @@ class CreateSubnetInfoAction(workflows.Action):
139 required=False) 139 required=False)
140 140
141 cidr = forms.IPField(label=_("Network Address"), 141 cidr = forms.IPField(label=_("Network Address"),
142 required=False,
143 initial="", 142 initial="",
143 error_messages={
144 'required': _('Specify "Network Address" or '
145 'clear "Create Subnet" checkbox '
146 'in previous step.')},
144 widget=forms.TextInput(attrs={ 147 widget=forms.TextInput(attrs={
145 'class': 'switched', 148 'class': 'switched',
146 'data-required-when-shown': 'true',
147 'data-switch-on': 'source', 149 'data-switch-on': 'source',
148 'data-source-manual': _("Network Address"), 150 'data-source-manual': _("Network Address"),
149 }), 151 }),
@@ -202,13 +204,6 @@ class CreateSubnetInfoAction(workflows.Action):
202 def __init__(self, request, context, *args, **kwargs): 204 def __init__(self, request, context, *args, **kwargs):
203 super(CreateSubnetInfoAction, self).__init__(request, context, *args, 205 super(CreateSubnetInfoAction, self).__init__(request, context, *args,
204 **kwargs) 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 if not getattr(settings, 'OPENSTACK_NEUTRON_NETWORK', 207 if not getattr(settings, 'OPENSTACK_NEUTRON_NETWORK',
213 {}).get('enable_ipv6', True): 208 {}).get('enable_ipv6', True):
214 self.fields['ip_version'].widget = forms.HiddenInput() 209 self.fields['ip_version'].widget = forms.HiddenInput()
@@ -279,8 +274,7 @@ class CreateSubnetInfoAction(workflows.Action):
279 'allowed': range_str}) 274 'allowed': range_str})
280 raise forms.ValidationError(msg) 275 raise forms.ValidationError(msg)
281 276
282 def _check_subnet_data(self, cleaned_data, is_create=True, 277 def _check_subnet_data(self, cleaned_data):
283 with_network_form=True):
284 cidr = cleaned_data.get('cidr') 278 cidr = cleaned_data.get('cidr')
285 ip_version = int(cleaned_data.get('ip_version')) 279 ip_version = int(cleaned_data.get('ip_version'))
286 gateway_ip = cleaned_data.get('gateway_ip') 280 gateway_ip = cleaned_data.get('gateway_ip')
@@ -293,14 +287,9 @@ class CreateSubnetInfoAction(workflows.Action):
293 '"Enter Network Address manually" and specify ' 287 '"Enter Network Address manually" and specify '
294 '"Network Address".') 288 '"Network Address".')
295 raise forms.ValidationError(msg) 289 raise forms.ValidationError(msg)
296 if not cidr and address_source != 'subnetpool': 290 if address_source == 'subnetpool' and 'cidr' in self._errors:
297 if with_network_form: 291 del self._errors['cidr']
298 msg = _('Specify "Network Address" or ' 292 elif cidr:
299 'clear "Create Subnet" checkbox in previous step.')
300 else:
301 msg = _("Specify network address")
302 raise forms.ValidationError(msg)
303 if cidr:
304 subnet = netaddr.IPNetwork(cidr) 293 subnet = netaddr.IPNetwork(cidr)
305 if subnet.version != ip_version: 294 if subnet.version != ip_version:
306 msg = _('Network Address and IP version are inconsistent.') 295 msg = _('Network Address and IP version are inconsistent.')
@@ -316,18 +305,18 @@ class CreateSubnetInfoAction(workflows.Action):
316 if netaddr.IPAddress(gateway_ip).version is not ip_version: 305 if netaddr.IPAddress(gateway_ip).version is not ip_version:
317 msg = _('Gateway IP and IP version are inconsistent.') 306 msg = _('Gateway IP and IP version are inconsistent.')
318 raise forms.ValidationError(msg) 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 if no_gateway and 'gateway_ip' in self._errors: 308 if no_gateway and 'gateway_ip' in self._errors:
324 del self._errors['gateway_ip'] 309 del self._errors['gateway_ip']
325 310
311 def _remove_fields_errors(self):
312 self._errors = {}
313
326 def clean(self): 314 def clean(self):
327 cleaned_data = super(CreateSubnetInfoAction, self).clean() 315 with_subnet = self.initial.get('with_subnet')
328 with_subnet = cleaned_data.get('with_subnet')
329 if not with_subnet: 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 self._check_subnet_data(cleaned_data) 320 self._check_subnet_data(cleaned_data)
332 return cleaned_data 321 return cleaned_data
333 322