changeset 492:000965a6e265

CollectLists: introduced sort_column_name and sort_reverse. With sort_column_name given as list you can sort the table by multiple columns. And sort_reverse=True makes it reverse.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 04 Feb 2010 11:05:31 +0100
parents 435dc7e10d78
children 9b22669a0690
files data/plugin/macro/CollectLists.py
diffstat 1 files changed, 47 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/macro/CollectLists.py	Wed Feb 03 20:39:25 2010 +0100
+++ b/data/plugin/macro/CollectLists.py	Thu Feb 04 11:05:31 2010 +0100
@@ -24,7 +24,7 @@
     @copyright: 2008-2010 by MoinMoin:ReimarBauer (completly rewritten)
     @license: GNU GPL, see COPYING for details.
 """
-import re
+import operator, re
 from MoinMoin import wikiutil, search
 from MoinMoin.Page import Page
 from MoinMoin.util.dataset import TupleDataset, Column
@@ -33,6 +33,39 @@
 
 Dependencies = ["pages"]
 
+def sort_table(table, cols=0, reverse=False):
+    """
+    sorts by column number. If a list is given multiple columns become sorted
+    """
+    for col in reversed(cols):
+        table = sorted(table, key=operator.itemgetter(col), reverse=reverse)
+    return table
+
+def table2rows(table):
+    """
+    transforms a data.data table of widget data browser into an array of lists without tuples 
+    """
+    rows = []
+    for line in table:
+        tmp = []
+        for element in line:
+            if isinstance(element, tuple):
+                tmp.append(element[1])
+            else:
+                tmp.append(element)
+        rows.append(tmp)
+    return rows
+
+def rows2table(rows):
+    """
+    transforms an array of lists into an array of lists with tuples compatible to widget data browser data.data
+    """
+    table_data = []
+    for line in rows:
+        tmp = [(element, element) for element in line]
+        table_data.append(tmp)
+    return table_data
+
 def macro_CollectLists(macro, pagename=unicode,
                       align=("left", "center", "right"),
                       column_heading=u'',
@@ -40,6 +73,8 @@
                       dictionary=u'',
                       transpose=False,
                       filter_column_value=u'',
+                      sort_column_name=u'',
+                      sort_reverse=False,
                       parser=u'text_moin_wiki',
                       search_expression=None,
                       filter_selection=u'NeverExistingDefaultFilter'):
@@ -105,7 +140,7 @@
     if Page(request, dictionary).exists():
         alias_dict = Dict(request, dictionary)
         heading_keys = [alias_dict[key] for key in column_heading_keys]
-        
+
     data = TupleDataset()
     data.columns = []
     data.columns.extend([Column(pagename.strip('/'), label=pagename.strip('/'), align=align)])
@@ -157,6 +192,16 @@
                     data.columns.extend([Column(key, label=alias_dict[key], align=align)])
 
     table = DataBrowserWidget(request)
+    if sort_column_name:
+        try:
+            names = sort_column_name.strip().split(',')
+            index = [column_heading_keys.index(name.strip()) + 1 for name in names]
+        except ValueError:
+            index = 0
+
+        rows = table2rows(data.data)
+        rows = sort_table(rows, cols=index, reverse=sort_reverse)
+        data.data = rows2table(rows)
     table.setData(data)
     html = ''.join(table.format(method='GET'))
     # seems to work together with