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
This commit is contained in:
Georgi Demirchev 2013-10-21 15:28:40 +03:00
parent 2b51396819
commit 44cd16753c
5 changed files with 37 additions and 3 deletions

View File

@ -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({

View File

@ -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("<span data-seconds=\"%d\">%s</span>" %
(seconds, timesince(dt)))

View File

@ -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"

View File

@ -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"

View File

@ -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))