Browse Source

Use BooleanField for admin_state_up form

admin_state_up is a boolean value but the network and port edit forms
expected a string version of True/False. As a result, True/False string
was shown as the default value in these forms. The field is implemented
as ChoiceField, but there is no special reason not to use BooleanField
for a boolean field and admin_state(_up) fields are the only exceptions
in horizon. This commit replaces all admin_state(_up) fields to
use BooleanField.

As far as I checked, this pattern is used only in the networking
related panels and this patch clean them up.

Change-Id: I9286f6c29d67fec7a88b74661bc8eca945fd9061
Closes-Bug: #1672213
Akihiro Motoki 2 years ago
parent
commit
c8cd03189a

+ 7
- 9
openstack_dashboard/dashboards/admin/networks/forms.py View File

@@ -118,10 +118,9 @@ class CreateNetwork(forms.SelfHandlingForm):
118 118
             'class': 'switched',
119 119
             'data-switch-on': 'network_type',
120 120
         }))
121
-    admin_state = forms.ThemableChoiceField(
122
-        choices=[('True', _('UP')),
123
-                 ('False', _('DOWN'))],
124
-        label=_("Admin State"))
121
+    admin_state = forms.BooleanField(label=_("Enable Admin State"),
122
+                                     initial=True,
123
+                                     required=False)
125 124
     shared = forms.BooleanField(label=_("Shared"),
126 125
                                 initial=False, required=False)
127 126
     external = forms.BooleanField(label=_("External Network"),
@@ -250,7 +249,7 @@ class CreateNetwork(forms.SelfHandlingForm):
250 249
         try:
251 250
             params = {'name': data['name'],
252 251
                       'tenant_id': data['tenant_id'],
253
-                      'admin_state_up': (data['admin_state'] == 'True'),
252
+                      'admin_state_up': data['admin_state'],
254 253
                       'shared': data['shared'],
255 254
                       'router:external': data['external']}
256 255
             if api.neutron.is_extension_supported(request, 'provider'):
@@ -312,9 +311,8 @@ class UpdateNetwork(forms.SelfHandlingForm):
312 311
     network_id = forms.CharField(label=_("ID"),
313 312
                                  widget=forms.TextInput(
314 313
                                      attrs={'readonly': 'readonly'}))
315
-    admin_state = forms.ThemableChoiceField(choices=[(True, _('UP')),
316
-                                                     (False, _('DOWN'))],
317
-                                            label=_("Admin State"))
314
+    admin_state = forms.BooleanField(label=_("Enable Admin State"),
315
+                                     required=False)
318 316
     shared = forms.BooleanField(label=_("Shared"), required=False)
319 317
     external = forms.BooleanField(label=_("External Network"), required=False)
320 318
     failure_url = 'horizon:admin:networks:index'
@@ -322,7 +320,7 @@ class UpdateNetwork(forms.SelfHandlingForm):
322 320
     def handle(self, request, data):
323 321
         try:
324 322
             params = {'name': data['name'],
325
-                      'admin_state_up': (data['admin_state'] == 'True'),
323
+                      'admin_state_up': data['admin_state'],
326 324
                       'shared': data['shared'],
327 325
                       'router:external': data['external']}
328 326
             network = api.neutron.network_update(request,

+ 1
- 2
openstack_dashboard/dashboards/admin/networks/ports/forms.py View File

@@ -102,7 +102,7 @@ class CreatePort(project_forms.CreatePort):
102 102
             params = {
103 103
                 'tenant_id': network.tenant_id,
104 104
                 'network_id': data['network_id'],
105
-                'admin_state_up': data['admin_state'] == 'True',
105
+                'admin_state_up': data['admin_state'],
106 106
                 'name': data['name'],
107 107
                 'device_id': data['device_id'],
108 108
                 'device_owner': data['device_owner'],
@@ -165,7 +165,6 @@ class UpdatePort(project_forms.UpdatePort):
165 165
         try:
166 166
             LOG.debug('params = %s', data)
167 167
             extension_kwargs = {}
168
-            data['admin_state'] = (data['admin_state'] == 'True')
169 168
             if 'binding__vnic_type' in data:
170 169
                 extension_kwargs['binding__vnic_type'] = \
171 170
                     data['binding__vnic_type']

+ 2
- 5
openstack_dashboard/dashboards/project/firewalls/forms.py View File

@@ -134,10 +134,8 @@ class UpdateFirewall(forms.SelfHandlingForm):
134 134
                                   label=_("Description"),
135 135
                                   required=False)
136 136
     firewall_policy_id = forms.ThemableChoiceField(label=_("Policy"))
137
-    admin_state_up = forms.ThemableChoiceField(choices=[(True, _('UP')),
138
-                                                        (False, _('DOWN'))],
139
-                                               label=_("Admin State"))
140
-
137
+    admin_state_up = forms.BooleanField(label=_("Enable Admin State"),
138
+                                        required=False)
141 139
     failure_url = 'horizon:project:firewalls:index'
142 140
 
143 141
     def __init__(self, request, *args, **kwargs):
@@ -165,7 +163,6 @@ class UpdateFirewall(forms.SelfHandlingForm):
165 163
     def handle(self, request, context):
166 164
         firewall_id = self.initial['firewall_id']
167 165
         name_or_id = context.get('name') or firewall_id
168
-        context['admin_state_up'] = (context['admin_state_up'] == 'True')
169 166
         try:
170 167
             firewall = api.fwaas.firewall_update(request, firewall_id,
171 168
                                                  **context)

+ 3
- 8
openstack_dashboard/dashboards/project/firewalls/workflows.py View File

@@ -343,9 +343,9 @@ class AddFirewallAction(workflows.Action):
343 343
                                   label=_("Description"),
344 344
                                   required=False)
345 345
     firewall_policy_id = forms.ThemableChoiceField(label=_("Policy"))
346
-    admin_state_up = forms.ThemableChoiceField(choices=[(True, _('UP')),
347
-                                                        (False, _('DOWN'))],
348
-                                               label=_("Admin State"))
346
+    admin_state_up = forms.BooleanField(label=_("Enable Admin State"),
347
+                                        initial=True,
348
+                                        required=False)
349 349
 
350 350
     def __init__(self, request, *args, **kwargs):
351 351
         super(AddFirewallAction, self).__init__(request, *args, **kwargs)
@@ -380,11 +380,6 @@ class AddFirewallStep(workflows.Step):
380 380
     contributes = ("name", "firewall_policy_id", "description",
381 381
                    "admin_state_up")
382 382
 
383
-    def contribute(self, data, context):
384
-        context = super(AddFirewallStep, self).contribute(data, context)
385
-        context['admin_state_up'] = (context['admin_state_up'] == 'True')
386
-        return context
387
-
388 383
 
389 384
 class AddFirewall(workflows.Workflow):
390 385
     slug = "addfirewall"

+ 3
- 6
openstack_dashboard/dashboards/project/networks/forms.py View File

@@ -38,11 +38,8 @@ class UpdateNetwork(forms.SelfHandlingForm):
38 38
     network_id = forms.CharField(label=_("ID"),
39 39
                                  widget=forms.TextInput(
40 40
                                      attrs={'readonly': 'readonly'}))
41
-    admin_state = forms.ThemableChoiceField(
42
-        choices=[('True', _('UP')),
43
-                 ('False', _('DOWN'))],
44
-        required=False,
45
-        label=_("Admin State"))
41
+    admin_state = forms.BooleanField(label=_("Enable Admin State"),
42
+                                     required=False)
46 43
     shared = forms.BooleanField(label=_("Shared"), required=False)
47 44
     failure_url = 'horizon:project:networks:index'
48 45
 
@@ -54,7 +51,7 @@ class UpdateNetwork(forms.SelfHandlingForm):
54 51
 
55 52
     def handle(self, request, data):
56 53
         try:
57
-            params = {'admin_state_up': (data['admin_state'] == 'True'),
54
+            params = {'admin_state_up': data['admin_state'],
58 55
                       'name': data['name']}
59 56
             # Make sure we are not sending shared data when the user
60 57
             # doesnt'have admin rights because even if the user doesn't

+ 6
- 9
openstack_dashboard/dashboards/project/networks/ports/forms.py View File

@@ -44,9 +44,9 @@ class CreatePort(forms.SelfHandlingForm):
44 44
     name = forms.CharField(max_length=255,
45 45
                            label=_("Name"),
46 46
                            required=False)
47
-    admin_state = forms.ChoiceField(choices=[('True', _('UP')),
48
-                                             ('False', _('DOWN'))],
49
-                                    label=_("Admin State"))
47
+    admin_state = forms.BooleanField(label=_("Enable Admin State"),
48
+                                     initial=True,
49
+                                     required=False)
50 50
     device_id = forms.CharField(max_length=100, label=_("Device ID"),
51 51
                                 help_text=_("Device ID attached to the port"),
52 52
                                 required=False)
@@ -132,7 +132,7 @@ class CreatePort(forms.SelfHandlingForm):
132 132
         try:
133 133
             params = {
134 134
                 'network_id': data['network_id'],
135
-                'admin_state_up': data['admin_state'] == 'True',
135
+                'admin_state_up': data['admin_state'],
136 136
                 'name': data['name'],
137 137
                 'device_id': data['device_id'],
138 138
                 'device_owner': data['device_owner']
@@ -179,10 +179,8 @@ class UpdatePort(forms.SelfHandlingForm):
179 179
     name = forms.CharField(max_length=255,
180 180
                            label=_("Name"),
181 181
                            required=False)
182
-    admin_state = forms.ThemableChoiceField(
183
-        choices=[('True', _('UP')),
184
-                 ('False', _('DOWN'))],
185
-        label=_("Admin State"))
182
+    admin_state = forms.BooleanField(label=_("Enable Admin State"),
183
+                                     required=False)
186 184
     failure_url = 'horizon:project:networks:detail'
187 185
 
188 186
     def __init__(self, request, *args, **kwargs):
@@ -232,7 +230,6 @@ class UpdatePort(forms.SelfHandlingForm):
232 230
             exceptions.handle(self.request, msg)
233 231
 
234 232
     def handle(self, request, data):
235
-        data['admin_state'] = (data['admin_state'] == 'True')
236 233
         try:
237 234
             LOG.debug('params = %s', data)
238 235
             extension_kwargs = {}

+ 5
- 7
openstack_dashboard/dashboards/project/networks/workflows.py View File

@@ -37,13 +37,11 @@ class CreateNetworkInfoAction(workflows.Action):
37 37
     net_name = forms.CharField(max_length=255,
38 38
                                label=_("Network Name"),
39 39
                                required=False)
40
-    admin_state = forms.ThemableChoiceField(
41
-        choices=[(True, _('UP')),
42
-                 (False, _('DOWN'))],
43
-        label=_("Admin State"),
40
+    admin_state = forms.BooleanField(
41
+        label=_("Enable Admin State"),
42
+        initial=True,
44 43
         required=False,
45
-        help_text=_("The state to start"
46
-                    " the network in."))
44
+        help_text=_("The state to start the network in."))
47 45
     shared = forms.BooleanField(label=_("Shared"), initial=False,
48 46
                                 required=False)
49 47
     with_subnet = forms.BooleanField(label=_("Create Subnet"),
@@ -463,7 +461,7 @@ class CreateNetwork(workflows.Workflow):
463 461
     def _create_network(self, request, data):
464 462
         try:
465 463
             params = {'name': data['net_name'],
466
-                      'admin_state_up': (data['admin_state'] == 'True'),
464
+                      'admin_state_up': data['admin_state'],
467 465
                       'shared': data['shared']}
468 466
             network = api.neutron.network_create(request, **params)
469 467
             self.context['net_id'] = network.id

+ 6
- 8
openstack_dashboard/dashboards/project/routers/forms.py View File

@@ -34,10 +34,9 @@ LOG = logging.getLogger(__name__)
34 34
 class CreateForm(forms.SelfHandlingForm):
35 35
     name = forms.CharField(max_length=255, label=_("Router Name"),
36 36
                            required=False)
37
-    admin_state_up = forms.ThemableChoiceField(label=_("Admin State"),
38
-                                               choices=[(True, _('UP')),
39
-                                                        (False, _('DOWN'))],
40
-                                               required=False)
37
+    admin_state_up = forms.BooleanField(label=_("Enable Admin State"),
38
+                                        initial=True,
39
+                                        required=False)
41 40
     external_network = forms.ThemableChoiceField(label=_("External Network"),
42 41
                                                  required=False)
43 42
     mode = forms.ChoiceField(label=_("Router Type"))
@@ -116,9 +115,8 @@ class CreateForm(forms.SelfHandlingForm):
116 115
 
117 116
 class UpdateForm(forms.SelfHandlingForm):
118 117
     name = forms.CharField(label=_("Name"), required=False)
119
-    admin_state = forms.ThemableChoiceField(choices=[(True, _('UP')),
120
-                                                     (False, _('DOWN'))],
121
-                                            label=_("Admin State"))
118
+    admin_state = forms.BooleanField(label=_("Enable Admin State"),
119
+                                     required=False)
122 120
     router_id = forms.CharField(label=_("ID"),
123 121
                                 widget=forms.TextInput(
124 122
                                     attrs={'readonly': 'readonly'}))
@@ -155,7 +153,7 @@ class UpdateForm(forms.SelfHandlingForm):
155 153
 
156 154
     def handle(self, request, data):
157 155
         try:
158
-            params = {'admin_state_up': (data['admin_state'] == 'True'),
156
+            params = {'admin_state_up': data['admin_state'],
159 157
                       'name': data['name']}
160 158
             if self.dvr_allowed:
161 159
                 params['distributed'] = (data['mode'] == 'distributed')

+ 5
- 5
openstack_dashboard/dashboards/project/routers/tests.py View File

@@ -264,7 +264,7 @@ class RouterActionTests(RouterMixin, test.TestCase):
264 264
         api.neutron.network_list(IsA(http.HttpRequest))\
265 265
             .AndReturn(self.networks.list())
266 266
         params = {'name': router.name,
267
-                  'admin_state_up': str(router.admin_state_up)}
267
+                  'admin_state_up': router.admin_state_up}
268 268
         api.neutron.router_create(IsA(http.HttpRequest), **params)\
269 269
             .AndReturn(router)
270 270
 
@@ -291,7 +291,7 @@ class RouterActionTests(RouterMixin, test.TestCase):
291 291
         api.neutron.network_list(IsA(http.HttpRequest))\
292 292
             .AndReturn(self.networks.list())
293 293
         params = {'name': router.name,
294
-                  'admin_state_up': str(router.admin_state_up)}
294
+                  'admin_state_up': router.admin_state_up}
295 295
         api.neutron.router_create(IsA(http.HttpRequest), **params)\
296 296
             .AndReturn(router)
297 297
 
@@ -322,7 +322,7 @@ class RouterActionTests(RouterMixin, test.TestCase):
322 322
         param = {'name': router.name,
323 323
                  'distributed': True,
324 324
                  'ha': True,
325
-                 'admin_state_up': str(router.admin_state_up)}
325
+                 'admin_state_up': router.admin_state_up}
326 326
         api.neutron.router_create(IsA(http.HttpRequest), **param)\
327 327
             .AndReturn(router)
328 328
 
@@ -352,7 +352,7 @@ class RouterActionTests(RouterMixin, test.TestCase):
352 352
         api.neutron.network_list(IsA(http.HttpRequest))\
353 353
             .MultipleTimes().AndReturn(self.networks.list())
354 354
         params = {'name': router.name,
355
-                  'admin_state_up': str(router.admin_state_up)}
355
+                  'admin_state_up': router.admin_state_up}
356 356
         api.neutron.router_create(IsA(http.HttpRequest), **params)\
357 357
             .AndRaise(self.exceptions.neutron)
358 358
         self.mox.ReplayAll()
@@ -380,7 +380,7 @@ class RouterActionTests(RouterMixin, test.TestCase):
380 380
         api.neutron.network_list(IsA(http.HttpRequest))\
381 381
             .MultipleTimes().AndReturn(self.networks.list())
382 382
         params = {'name': router.name,
383
-                  'admin_state_up': str(router.admin_state_up)}
383
+                  'admin_state_up': router.admin_state_up}
384 384
         api.neutron.router_create(IsA(http.HttpRequest), **params)\
385 385
             .AndRaise(self.exceptions.neutron)
386 386
         self.mox.ReplayAll()

+ 4
- 10
openstack_dashboard/dashboards/project/vpn/forms.py View File

@@ -34,15 +34,12 @@ class UpdateVPNService(forms.SelfHandlingForm):
34 34
         widget=forms.TextInput(attrs={'readonly': 'readonly'}))
35 35
     description = forms.CharField(
36 36
         required=False, max_length=80, label=_("Description"))
37
-    admin_state_up = forms.ChoiceField(choices=[(True, _('UP')),
38
-                                                (False, _('DOWN'))],
39
-                                       label=_("Admin State"),
40
-                                       required=False)
37
+    admin_state_up = forms.BooleanField(label=_("Enable Admin State"),
38
+                                        required=False)
41 39
 
42 40
     failure_url = 'horizon:project:vpn:index'
43 41
 
44 42
     def handle(self, request, context):
45
-        context['admin_state_up'] = (context['admin_state_up'] == 'True')
46 43
         try:
47 44
             data = {'vpnservice': {'name': context['name'],
48 45
                                    'description': context['description'],
@@ -279,10 +276,8 @@ class UpdateIPSecSiteConnection(forms.SelfHandlingForm):
279 276
         required=False,
280 277
         choices=[('bi-directional', _('bi-directional')),
281 278
                  ('response-only', _('response-only'))])
282
-    admin_state_up = forms.ChoiceField(choices=[(True, _('UP')),
283
-                                                (False, _('DOWN'))],
284
-                                       label=_("Admin State"),
285
-                                       required=False)
279
+    admin_state_up = forms.BooleanField(label=_("Enable Admin State"),
280
+                                        required=False)
286 281
 
287 282
     failure_url = 'horizon:project:vpn:index'
288 283
 
@@ -297,7 +292,6 @@ class UpdateIPSecSiteConnection(forms.SelfHandlingForm):
297 292
         return cleaned_data
298 293
 
299 294
     def handle(self, request, context):
300
-        context['admin_state_up'] = (context['admin_state_up'] == 'True')
301 295
         try:
302 296
             data = {'ipsec_site_connection':
303 297
                     {'name': context['name'],

+ 2
- 2
openstack_dashboard/dashboards/project/vpn/templates/vpn/_add_vpn_service_help.html View File

@@ -3,5 +3,5 @@
3 3
 <p>{% trans "Create VPN service for current project." %}</p>
4 4
 <p>{% trans "The VPN service is attached to a router and references to a single subnet to push to a remote site." %}</p>
5 5
 <p>{% trans "Specify a name, description, router, and subnet for the VPN Service." %}</p>
6
-<p>{% trans "Admin State is UP (True) by default." %}</p>
7
-<p>{% trans "The router, subnet and admin state fields are required. All others are optional." %} </p>
6
+<p>{% trans "Admin State is enabled by default." %}</p>
7
+<p>{% trans "The router, subnet and admin state fields require to be enabled. All others are optional." %} </p>

+ 11
- 11
openstack_dashboard/dashboards/project/vpn/workflows.py View File

@@ -28,11 +28,11 @@ class AddVPNServiceAction(workflows.Action):
28 28
         max_length=80, label=_("Description"))
29 29
     router_id = forms.ChoiceField(label=_("Router"))
30 30
     subnet_id = forms.ChoiceField(label=_("Subnet"))
31
-    admin_state_up = forms.ChoiceField(
32
-        choices=[(True, _('UP')), (False, _('DOWN'))],
33
-        label=_("Admin State"),
34
-        help_text=_("The state of VPN service to start in. "
35
-                    "If DOWN (False) VPN service does not forward packets."),
31
+    admin_state_up = forms.BooleanField(
32
+        label=_("Enable Admin State"),
33
+        help_text=_("The state of VPN service to start in. If disabled "
34
+                    "(not checked), VPN service does not forward packets."),
35
+        initial=True,
36 36
         required=False)
37 37
 
38 38
     def __init__(self, request, *args, **kwargs):
@@ -426,13 +426,13 @@ class AddIPSecSiteConnectionOptionalAction(workflows.Action):
426 426
         required=False,
427 427
         help_text=_("Valid integer greater than the DPD interval"))
428 428
     initiator = forms.ChoiceField(label=_("Initiator state"), required=False)
429
-    admin_state_up = forms.ChoiceField(
430
-        choices=[(True, _('UP')), (False, _('DOWN'))],
431
-        label=_("Admin State"),
432
-        required=False,
429
+    admin_state_up = forms.BooleanField(
430
+        label=_("Enable Admin State"),
433 431
         help_text=_("The state of IPSec site connection to start in. "
434
-                    "If DOWN (False), IPSec site connection "
435
-                    "does not forward packets."))
432
+                    "If disabled (not checked), IPSec site connection "
433
+                    "does not forward packets."),
434
+        initial=True,
435
+        required=False)
436 436
 
437 437
     def __init__(self, request, *args, **kwargs):
438 438
         super(AddIPSecSiteConnectionOptionalAction, self).__init__(

Loading…
Cancel
Save