changeset 5625:78f1be3c8777

MoinMoin.widget.browser: introduced feature for sorting tables (http://moinmo.in/FeatureRequests/SortableTables). DataBrowserWidget.setData: got two new optional keywords. (Defaults: sort_columns=None, reverse=False) MoinMoin.action.AttachFile.do_admin_browser: feature added to sort by first and second column (Page and Filename) MoinMoin.userform.admin.do_user_browser: feature added to sort by the first column (Username)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Wed, 10 Mar 2010 16:14:56 +0100
parents ed7044b59397
children 07aa5349ad8b
files MoinMoin/action/AttachFile.py MoinMoin/userform/admin.py MoinMoin/widget/browser.py
diffstat 3 files changed, 46 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Wed Mar 10 05:20:47 2010 +0300
+++ b/MoinMoin/action/AttachFile.py	Wed Mar 10 16:14:56 2010 +0100
@@ -1155,7 +1155,8 @@
             for filename in files:
                 filepath = os.path.join(page_dir, filename)
                 data.addRow((
-                    Page(request, pagename).link_to(request, querystr="action=AttachFile"),
+                    (Page(request, pagename).link_to(request,
+                                querystr="action=AttachFile"), wikiutil.escape(pagename, 1)),
                     wikiutil.escape(filename.decode(config.charset)),
                     os.path.getsize(filepath),
                 ))
@@ -1164,7 +1165,7 @@
         from MoinMoin.widget.browser import DataBrowserWidget
 
         browser = DataBrowserWidget(request)
-        browser.setData(data)
+        browser.setData(data, sort_columns=[0, 1])
         return browser.render(method="GET")
 
     return ''
--- a/MoinMoin/userform/admin.py	Wed Mar 10 05:20:47 2010 +0300
+++ b/MoinMoin/userform/admin.py	Wed Mar 10 16:14:56 2010 +0100
@@ -94,7 +94,7 @@
             jabber_link = ''
 
         data.addRow((
-            request.formatter.rawHTML(namelink),
+            (request.formatter.rawHTML(namelink), account.name),
             request.formatter.rawHTML(grouppage_links),
             email_link,
             jabber_link,
@@ -105,7 +105,7 @@
         from MoinMoin.widget.browser import DataBrowserWidget
 
         browser = DataBrowserWidget(request)
-        browser.setData(data)
+        browser.setData(data, sort_columns=[0])
         return browser.render()
 
     # No data
--- a/MoinMoin/widget/browser.py	Wed Mar 10 05:20:47 2010 +0300
+++ b/MoinMoin/widget/browser.py	Wed Mar 10 16:14:56 2010 +0100
@@ -2,13 +2,46 @@
 """
     MoinMoin - DataBrowserWidget
 
-    @copyright: 2002 Juergen Hermann <jh@web.de>
+    @copyright: 2002 Juergen Hermann <jh@web.de>,
+                2010 MoinMoin:ReimarBauer,
+                2010 MoinMoin:EugeneSyromyatnikov
     @license: GNU GPL, see COPYING for details.
 """
-
 from MoinMoin.widget import base
 from MoinMoin import wikiutil
 
+def _compare(idx, text):
+    """
+    compare function for sorted
+    """
+    txt = text[idx]
+    if isinstance(txt, tuple):
+        txt = txt[1]
+    try:
+        decimal_string = txt
+        decimal_value = float(decimal_string)
+        txt = u""
+    except ValueError:
+        decimal_value = float('Infinity')
+        decimal_string = u""
+    return (decimal_value, decimal_string, txt)
+
+def sort_table(rows, sort_columns=None, reverse=False):
+    """
+    sorts table rows
+
+    @param rows: table rows to sort
+    @param index: column to sort. By a given list it does a multiple sort
+    @param reverse: reverse sort
+    """
+    if not (sort_columns and isinstance(sort_columns, list) or
+            isinstance(sort_columns, tuple)):
+        # don't sort if no list is given
+        return rows
+    for idx in reversed(sort_columns):
+        rows = sorted(rows, key=lambda x: _compare(idx, x), reverse=reverse)
+    return rows
+
 class DataBrowserWidget(base.Widget):
 
     def __init__(self, request, show_header=True, **kw):
@@ -28,14 +61,19 @@
         self.__filter = 'filter'
         self._show_header = show_header
 
-    def setData(self, dataset):
+    def setData(self, dataset, sort_columns=None, reverse=False):
         """ Sets the data for the browser (see MoinMoin.util.dataset).
 
         @param dataset: dataset containing either ascii, unicode or tuples.
                         If a dataset entry contains a tuple then the first
                         item in the tuple is displayed and the second item
                         is used for autofilters.
+        @param index: list of column index number for sorting
+        @param reverse: reverse sort
         """
+        if sort_columns:
+            dataset.data = sort_table(dataset.data, sort_columns, reverse=reverse)
+
         self.data = dataset
         if dataset.data_id:
             self.unqual_data_id = 'dbw.%s.' % dataset.data_id