Skip to content

Commit 0d82734

Browse files
committed
Added a new attribute - display_choices
to Column class, for substituting the display value of the statuses provided by Nova to some more meaningful ones in the instance table. Fixes bug 997374 Change-Id: I18560868435b4cbc42670e3fc9c0bc83ebf9fda4
1 parent b0df3b7 commit 0d82734

File tree

5 files changed

+94
-14
lines changed

5 files changed

+94
-14
lines changed

horizon/dashboards/nova/instances_and_volumes/instances/tables.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from horizon import api
2424
from horizon import tables
2525
from horizon.templatetags import sizeformat
26+
from horizon.utils.filters import replace_underscores
2627

2728
from .tabs import InstanceDetailTabs, LogTab, VNCTab
2829

@@ -216,10 +217,6 @@ def get_power_state(instance):
216217
return POWER_STATES.get(getattr(instance, "OS-EXT-STS:power_state", 0), '')
217218

218219

219-
def replace_underscores(string):
220-
return string.replace("_", " ")
221-
222-
223220
class InstancesTable(tables.DataTable):
224221
TASK_STATUS_CHOICES = (
225222
(None, True),
@@ -231,6 +228,9 @@ class InstancesTable(tables.DataTable):
231228
("paused", True),
232229
("error", False),
233230
)
231+
TASK_DISPLAY_CHOICES = (
232+
("image_snapshot", "Snapshotting"),
233+
)
234234
name = tables.Column("name", link="horizon:nova:instances_and_volumes:" \
235235
"instances:detail",
236236
verbose_name=_("Instance Name"))
@@ -245,7 +245,8 @@ class InstancesTable(tables.DataTable):
245245
verbose_name=_("Task"),
246246
filters=(title, replace_underscores),
247247
status=True,
248-
status_choices=TASK_STATUS_CHOICES)
248+
status_choices=TASK_STATUS_CHOICES,
249+
display_choices=TASK_DISPLAY_CHOICES)
249250
state = tables.Column(get_power_state,
250251
filters=(title, replace_underscores),
251252
verbose_name=_("Power State"))

horizon/dashboards/nova/instances_and_volumes/instances/tests.py

+42
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from django import http
2222
from django.core.urlresolvers import reverse
2323
from mox import IsA, IgnoreArg
24+
from copy import deepcopy
2425

2526
from horizon import api
2627
from horizon import test
@@ -308,6 +309,47 @@ def test_instance_vnc_exception(self):
308309
res = self.client.get(url)
309310
self.assertRedirectsNoFollow(res, INDEX_URL)
310311

312+
def test_create_instance_snapshot(self):
313+
server = self.servers.first()
314+
snapshot_server = deepcopy(server)
315+
setattr(snapshot_server, 'OS-EXT-STS:task_state',
316+
"IMAGE_SNAPSHOT")
317+
self.mox.StubOutWithMock(api, 'server_get')
318+
self.mox.StubOutWithMock(api, 'snapshot_create')
319+
self.mox.StubOutWithMock(api, 'snapshot_list_detailed')
320+
self.mox.StubOutWithMock(api, 'image_list_detailed')
321+
self.mox.StubOutWithMock(api, 'volume_snapshot_list')
322+
self.mox.StubOutWithMock(api, 'server_list')
323+
self.mox.StubOutWithMock(api, 'flavor_list')
324+
self.mox.StubOutWithMock(api, 'server_delete')
325+
self.mox.StubOutWithMock(api, 'volume_list')
326+
api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server)
327+
api.snapshot_create(IsA(http.HttpRequest),
328+
server.id,
329+
"snapshot1")
330+
api.server_get(IsA(http.HttpRequest), server.id).AndReturn(server)
331+
api.snapshot_list_detailed(IsA(http.HttpRequest)).AndReturn([])
332+
api.image_list_detailed(IsA(http.HttpRequest)).AndReturn([])
333+
api.volume_snapshot_list(IsA(http.HttpRequest)).AndReturn([])
334+
335+
api.volume_list(IsA(http.HttpRequest)).AndReturn(self.volumes.list())
336+
api.server_list(IsA(http.HttpRequest)).AndReturn([snapshot_server])
337+
api.flavor_list(IgnoreArg()).AndReturn(self.flavors.list())
338+
self.mox.ReplayAll()
339+
340+
formData = {'instance_id': server.id,
341+
'method': 'CreateSnapshot',
342+
'tenant_id': server.tenant_id,
343+
'name': 'snapshot1'}
344+
url = reverse('horizon:nova:images_and_snapshots:snapshots:create',
345+
args=[server.id])
346+
redir_url = reverse('horizon:nova:images_and_snapshots:index')
347+
res = self.client.post(url, formData)
348+
self.assertRedirects(res, redir_url)
349+
res = self.client.get(INDEX_URL)
350+
self.assertContains(res, "<td class=\"status_unknown\">"
351+
"Snapshotting</td>", 1)
352+
311353
def test_instance_update_get(self):
312354
server = self.servers.first()
313355

horizon/dashboards/syspanel/instances/tables.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
TerminateInstance, EditInstance, ConsoleLink, LogLink, SnapshotLink,
2727
TogglePause, ToggleSuspend, RebootInstance, get_size, UpdateRow,
2828
get_ips, get_power_state)
29-
29+
from horizon.utils.filters import replace_underscores
3030

3131
LOG = logging.getLogger(__name__)
3232

@@ -50,6 +50,9 @@ class SyspanelInstancesTable(tables.DataTable):
5050
("active", True),
5151
("error", False),
5252
)
53+
TASK_DISPLAY_CHOICES = (
54+
("image_snapshot", "Snapshotting"),
55+
)
5356
tenant = tables.Column("tenant_name", verbose_name=_("Tenant"))
5457
# NOTE(gabriel): Commenting out the user column because all we have
5558
# is an ID, and correlating that at production scale using our current
@@ -67,17 +70,18 @@ class SyspanelInstancesTable(tables.DataTable):
6770
verbose_name=_("Size"),
6871
classes=('nowrap-col',))
6972
status = tables.Column("status",
70-
filters=(title,),
73+
filters=(title, replace_underscores),
7174
verbose_name=_("Status"),
7275
status=True,
7376
status_choices=STATUS_CHOICES)
7477
task = tables.Column("OS-EXT-STS:task_state",
7578
verbose_name=_("Task"),
76-
filters=(title,),
79+
filters=(title, replace_underscores),
7780
status=True,
78-
status_choices=TASK_STATUS_CHOICES)
81+
status_choices=TASK_STATUS_CHOICES,
82+
display_choices=TASK_DISPLAY_CHOICES)
7983
state = tables.Column(get_power_state,
80-
filters=(title,),
84+
filters=(title, replace_underscores),
8185
verbose_name=_("Power State"))
8286

8387
class Meta:

horizon/tables/base.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class Column(html.HTMLElement):
110110
('off', False),
111111
)
112112
113+
.. attribute:: display_choices
114+
115+
A tuple of tuples representing the possible values to substitute
116+
the data when displayed in the column cell.
117+
113118
.. attribute:: empty_value
114119
115120
A string or callable to be used for cells which have no data.
@@ -157,8 +162,8 @@ class Column(html.HTMLElement):
157162

158163
def __init__(self, transform, verbose_name=None, sortable=False,
159164
link=None, hidden=False, attrs=None, status=False,
160-
status_choices=None, empty_value=None, filters=None,
161-
classes=None):
165+
status_choices=None, display_choices=None,
166+
empty_value=None, filters=None, classes=None):
162167
self.classes = classes or getattr(self, "classes", [])
163168
super(Column, self).__init__()
164169
self.attrs.update(attrs or {})
@@ -183,6 +188,7 @@ def __init__(self, transform, verbose_name=None, sortable=False,
183188
self.filters = filters or []
184189
if status_choices:
185190
self.status_choices = status_choices
191+
self.display_choices = display_choices
186192

187193
self.creation_counter = Column.creation_counter
188194
Column.creation_counter += 1
@@ -227,8 +233,16 @@ def get_data(self, datum):
227233
msg = termcolors.colorize(msg, **PALETTE['ERROR'])
228234
LOG.warning(msg)
229235
data = None
230-
for filter_func in self.filters:
231-
data = filter_func(data)
236+
display_value = None
237+
if self.display_choices:
238+
display_value = [display for (value, display) in
239+
self.display_choices
240+
if value.lower() == (data or '').lower()]
241+
if display_value:
242+
data = display_value[0]
243+
else:
244+
for filter_func in self.filters:
245+
data = filter_func(data)
232246
self.table._data_cache[self][datum_id] = data
233247
return self.table._data_cache[self][datum_id]
234248

horizon/utils/filters.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2+
3+
# Copyright 2012 Nebula, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
18+
def replace_underscores(string):
19+
return string.replace("_", " ")

0 commit comments

Comments
 (0)