From 44cd16753c81d84e4813dee223fd61ce92184125 Mon Sep 17 00:00:00 2001 From: Georgi Demirchev Date: Mon, 21 Oct 2013 15:28:40 +0300 Subject: [PATCH] Sortable instances New filter that appends a timestamp to the uptime localized string, managed with tablesorter new parser on client side. Change-Id: I999b04d3da93bc00003fed954382a0ac99b506a3 Closes-Bug: #1222286 --- horizon/static/horizon/js/horizon.tables.js | 16 ++++++++++++++++ horizon/utils/filters.py | 12 ++++++++++++ .../dashboards/admin/instances/tables.py | 4 +++- .../dashboards/project/instances/tables.py | 4 +++- openstack_dashboard/usage/tables.py | 4 +++- 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/horizon/static/horizon/js/horizon.tables.js b/horizon/static/horizon/js/horizon.tables.js index c36899e775..616f51078e 100644 --- a/horizon/static/horizon/js/horizon.tables.js +++ b/horizon/static/horizon/js/horizon.tables.js @@ -196,6 +196,20 @@ $.tablesorter.addParser({ type: 'numeric' }); +$.tablesorter.addParser({ + // set a unique id + id: 'timesinceSorter', + is: function(s) { + // Not an auto-detected parser + return false; + }, + // compare int values + format: function(s, table, cell, cellIndex) { + return $(cell).find('span').data('seconds'); + }, + type: 'numeric' +}); + horizon.datatables.disable_buttons = function() { $("table .table_actions").on("click", ".btn.disabled", function(event){ event.preventDefault(); @@ -260,6 +274,8 @@ $(parent).find("table.datatable").each(function () { header_options[i] = {sorter: 'sizeSorter'}; } else if ($th.data('type') == 'ip'){ header_options[i] = {sorter: 'ipAddress'}; + } else if ($th.data('type') == 'timesince'){ + header_options[i] = {sorter: 'timesinceSorter'}; } }); $table.tablesorter({ diff --git a/horizon/utils/filters.py b/horizon/utils/filters.py index e73b18886b..dff17f6331 100644 --- a/horizon/utils/filters.py +++ b/horizon/utils/filters.py @@ -17,6 +17,9 @@ import iso8601 from django.template.defaultfilters import register # noqa +from django.template.defaultfilters import timesince # noqa +from django.utils.safestring import mark_safe # noqa +from django.utils import timezone @register.filter @@ -36,3 +39,12 @@ def parse_isotime(timestr): raise ValueError(e.message) except TypeError as e: raise ValueError(e.message) + + +@register.filter +def timesince_sortable(dt): + delta = timezone.now() - dt + # timedelta.total_seconds() not supported on python < 2.7 + seconds = delta.seconds + (delta.days * 24 * 3600) + return mark_safe("%s" % + (seconds, timesince(dt))) diff --git a/openstack_dashboard/dashboards/admin/instances/tables.py b/openstack_dashboard/dashboards/admin/instances/tables.py index 01a862c383..d75cfa3c6e 100644 --- a/openstack_dashboard/dashboards/admin/instances/tables.py +++ b/openstack_dashboard/dashboards/admin/instances/tables.py @@ -117,7 +117,9 @@ class AdminInstancesTable(tables.DataTable): verbose_name=_("Power State")) created = tables.Column("created", verbose_name=_("Uptime"), - filters=(filters.parse_isotime, timesince)) + filters=(filters.parse_isotime, + filters.timesince_sortable), + attrs={'data-type': 'timesince'}) class Meta: name = "instances" diff --git a/openstack_dashboard/dashboards/project/instances/tables.py b/openstack_dashboard/dashboards/project/instances/tables.py index c409decd2a..997f7e782a 100644 --- a/openstack_dashboard/dashboards/project/instances/tables.py +++ b/openstack_dashboard/dashboards/project/instances/tables.py @@ -596,7 +596,9 @@ class InstancesTable(tables.DataTable): verbose_name=_("Power State")) created = tables.Column("created", verbose_name=_("Uptime"), - filters=(filters.parse_isotime, timesince)) + filters=(filters.parse_isotime, + filters.timesince_sortable), + attrs={'data-type': 'timesince'}) class Meta: name = "instances" diff --git a/openstack_dashboard/usage/tables.py b/openstack_dashboard/usage/tables.py index baab5a6c1c..8d41a6d33a 100644 --- a/openstack_dashboard/usage/tables.py +++ b/openstack_dashboard/usage/tables.py @@ -5,6 +5,7 @@ from django.utils.translation import ugettext_lazy as _ # noqa from horizon import tables from horizon.templatetags import sizeformat +from horizon.utils import filters class CSVSummary(tables.LinkAction): @@ -59,7 +60,8 @@ class ProjectUsageTable(BaseUsageTable): link=get_instance_link) uptime = tables.Column('uptime_at', verbose_name=_("Uptime"), - filters=(timesince,)) + filters=(filters.timesince_sortable,), + attrs={'data-type': 'timesince'}) def get_object_id(self, datum): return datum.get('instance_id', id(datum))