Refactor status and admin state translation code

In some of the dashboard 'views.py' files, there is code that enables
the detail status and, when applicable, admin state labels to be
translated.  The current coding style of these sections is hard
to read.  Any and all sections of code that use this style should
be refactored to enhance the code readability.  This change
proposes to create a single utility method that handles this
repetitive code block that is used in many places.  Thus to perform
the translation label lookup, this method need only be called.  This
method will provide a single clean and consistent code block that can
be used for any of these situations in the future.

Change-Id: I6353abef58fc2a481d4379eb1a2f8289d7f1790b
Closes-Bug: #1490031
This commit is contained in:
Lucas Palm 2015-09-21 15:42:03 -05:00
parent 152f38221f
commit b56b962a8f
10 changed files with 58 additions and 77 deletions

View File

@ -84,8 +84,8 @@ class EditNetwork(policy.PolicyTargetMixin, tables.LinkAction):
DISPLAY_CHOICES = (
("UP", pgettext_lazy("Admin state of a Network", u"UP")),
("DOWN", pgettext_lazy("Admin state of a Network", u"DOWN")),
("up", pgettext_lazy("Admin state of a Network", u"UP")),
("down", pgettext_lazy("Admin state of a Network", u"DOWN")),
)

View File

@ -24,6 +24,7 @@ from horizon.utils import memoized
from openstack_dashboard import api
from openstack_dashboard.dashboards.project.networks import views as user_views
from openstack_dashboard.utils import filters
from openstack_dashboard.dashboards.admin.networks.agents \
import tables as agents_tables
@ -169,21 +170,12 @@ class DetailView(tables.MultiTableView):
context["network"] = network
context["url"] = self.get_redirect_url()
context["actions"] = table.render_row_actions(network)
status_label = [label for (value, label) in
networks_tables.project_tables.STATUS_DISPLAY_CHOICES
if value.lower() == (network.status or '').lower()]
if status_label:
network.status_label = status_label[0]
else:
network.status_label = network.status
admin_state_label = [state for (value, state) in
networks_tables.DISPLAY_CHOICES
if value.lower() ==
(network.admin_state or '').lower()]
if admin_state_label:
network.admin_state_label = admin_state_label[0]
else:
network.admin_state_label = network.admin_state
choices = networks_tables.project_tables.STATUS_DISPLAY_CHOICES
network.status_label = (
filters.get_display_label(choices, network.status))
choices = networks_tables.DISPLAY_CHOICES
network.admin_state_label = (
filters.get_display_label(choices, network.admin_state))
return context
@staticmethod

View File

@ -29,6 +29,7 @@ from horizon import tabs
from horizon.utils import memoized
from openstack_dashboard import api
from openstack_dashboard.utils import filters
from openstack_dashboard.dashboards.project.images.images \
import forms as project_forms
@ -126,13 +127,8 @@ class DetailView(tabs.TabView):
context["image"] = image
context["url"] = self.get_redirect_url()
context["actions"] = table.render_row_actions(image)
status_label = [label for (value, label) in
project_tables.ImagesTable.STATUS_DISPLAY_CHOICES
if value.lower() == (image.status or '').lower()]
if status_label:
image.status_label = status_label[0]
else:
image.status_label = image.status
choices = project_tables.ImagesTable.STATUS_DISPLAY_CHOICES
image.status_label = filters.get_display_label(choices, image.status)
return context
@staticmethod

View File

@ -38,6 +38,7 @@ from horizon.utils import memoized
from horizon import workflows
from openstack_dashboard import api
from openstack_dashboard.utils import filters
from openstack_dashboard.dashboards.project.instances \
import console as project_console
@ -327,13 +328,9 @@ class DetailView(tabs.TabView):
# Need to raise here just in case.
raise exceptions.Http302(redirect)
status_label = [label for (value, label) in
project_tables.STATUS_DISPLAY_CHOICES
if value.lower() == (instance.status or '').lower()]
if status_label:
instance.status_label = status_label[0]
else:
instance.status_label = instance.status
choices = project_tables.STATUS_DISPLAY_CHOICES
instance.status_label = (
filters.get_display_label(choices, instance.status))
try:
instance.volumes = api.nova.instance_volumes_list(self.request,

View File

@ -147,14 +147,14 @@ def get_subnets(network):
DISPLAY_CHOICES = (
("UP", pgettext_lazy("Admin state of a Network", u"UP")),
("DOWN", pgettext_lazy("Admin state of a Network", u"DOWN")),
("up", pgettext_lazy("Admin state of a Network", u"UP")),
("down", pgettext_lazy("Admin state of a Network", u"DOWN")),
)
STATUS_DISPLAY_CHOICES = (
("ACTIVE", pgettext_lazy("Current status of a Network", u"Active")),
("BUILD", pgettext_lazy("Current status of a Network", u"Build")),
("DOWN", pgettext_lazy("Current status of a Network", u"Down")),
("ERROR", pgettext_lazy("Current status of a Network", u"Error")),
("active", pgettext_lazy("Current status of a Network", u"Active")),
("build", pgettext_lazy("Current status of a Network", u"Build")),
("down", pgettext_lazy("Current status of a Network", u"Down")),
("error", pgettext_lazy("Current status of a Network", u"Error")),
)

View File

@ -26,6 +26,7 @@ from horizon.utils import memoized
from horizon import workflows
from openstack_dashboard import api
from openstack_dashboard.utils import filters
from openstack_dashboard.dashboards.project.networks \
import forms as project_forms
@ -150,21 +151,12 @@ class DetailView(tables.MultiTableView):
table = project_tables.NetworksTable(self.request)
context["url"] = self.get_redirect_url()
context["actions"] = table.render_row_actions(network)
status_label = [label for (value, label) in
project_tables.STATUS_DISPLAY_CHOICES
if value.lower() == (network.status or '').lower()]
if status_label:
network.status_label = status_label[0]
else:
network.status_label = network.status
admin_state_label = [state for (value, state) in
project_tables.DISPLAY_CHOICES
if value.lower() ==
(network.admin_state or '').lower()]
if admin_state_label:
network.admin_state_label = admin_state_label[0]
else:
network.admin_state_label = network.admin_state
choices = project_tables.STATUS_DISPLAY_CHOICES
network.status_label = (
filters.get_display_label(choices, network.status))
choices = project_tables.DISPLAY_CHOICES
network.admin_state_label = (
filters.get_display_label(choices, network.admin_state))
return context
@staticmethod

View File

@ -203,8 +203,8 @@ class RoutersTable(tables.DataTable):
("error", pgettext_lazy("current status of router", u"Error")),
)
ADMIN_STATE_DISPLAY_CHOICES = (
("UP", pgettext_lazy("Admin state of a Router", u"UP")),
("DOWN", pgettext_lazy("Admin state of a Router", u"DOWN")),
("up", pgettext_lazy("Admin state of a Router", u"UP")),
("down", pgettext_lazy("Admin state of a Router", u"DOWN")),
)
name = tables.Column("name",

View File

@ -30,7 +30,10 @@ from horizon import messages
from horizon import tables
from horizon import tabs
from horizon.utils import memoized
from openstack_dashboard import api
from openstack_dashboard.utils import filters
from openstack_dashboard.dashboards.project.routers\
import forms as project_forms
from openstack_dashboard.dashboards.project.routers import tables as rtables
@ -152,22 +155,11 @@ class DetailView(tabs.TabbedTableView):
self.request, "dvr", "get")
context['ha_supported'] = api.neutron.get_feature_permission(
self.request, "l3-ha", "get")
status_label = [label for (value, label) in
table.STATUS_DISPLAY_CHOICES
if value.lower() == (router.status or '').lower()]
if status_label:
router.status_label = status_label[0]
else:
router.status_label = router.status
admin_state_label = [state for (value, state) in
table.ADMIN_STATE_DISPLAY_CHOICES
if value.lower() ==
(router.admin_state or '').lower()]
if admin_state_label:
router.admin_state_label = admin_state_label[0]
else:
router.admin_state_label = router.admin_state
choices = table.STATUS_DISPLAY_CHOICES
router.status_label = filters.get_display_label(choices, router.status)
choices = table.ADMIN_STATE_DISPLAY_CHOICES
router.admin_state_label = (
filters.get_display_label(choices, router.admin_state))
return context
def get_tabs(self, request, *args, **kwargs):

View File

@ -34,6 +34,7 @@ from openstack_dashboard import api
from openstack_dashboard.api import cinder
from openstack_dashboard import exceptions as dashboard_exception
from openstack_dashboard.usage import quotas
from openstack_dashboard.utils import filters
from openstack_dashboard.dashboards.project.volumes \
.volumes import forms as project_forms
@ -56,13 +57,8 @@ class DetailView(tabs.TabView):
context["volume"] = volume
context["url"] = self.get_redirect_url()
context["actions"] = table.render_row_actions(volume)
status_label = [label for (value, label) in
project_tables.VolumesTableBase.STATUS_DISPLAY_CHOICES
if value.lower() == (volume.status or '').lower()]
if status_label:
volume.status_label = status_label[0]
else:
volume.status_label = volume.status
choices = project_tables.VolumesTableBase.STATUS_DISPLAY_CHOICES
volume.status_label = filters.get_display_label(choices, volume.status)
return context
@memoized.memoized_method

View File

@ -27,3 +27,19 @@ def get_int_or_uuid(value):
return value
except (ValueError, AttributeError):
return int(value)
def get_display_label(choices, status):
"""This method is used in places where a resource's status or
admin state labels need to assigned before they are sent to the
view template.
"""
for (value, label) in choices:
if value == (status or '').lower():
display_label = label
break
else:
display_label = status
return display_label