From 0541f7e13f271e77c179b77425c380b96cc83308 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 4 Jan 2020 14:51:39 +0100
Subject: [PATCH] myspace: upgrade sort and remove dedicated display form
 (#38761)

---
 auquotidien/modules/myspace.py | 13 ++-----
 auquotidien/modules/root.py    | 65 +---------------------------------
 tests/test_user_pages.py       | 22 ++++++------
 3 files changed, 15 insertions(+), 85 deletions(-)

diff --git a/auquotidien/modules/myspace.py b/auquotidien/modules/myspace.py
index 948cfca..657067f 100644
--- a/auquotidien/modules/myspace.py
+++ b/auquotidien/modules/myspace.py
@@ -50,14 +50,7 @@ class MyInvoicesDirectory(Directory):
         invoices = []
         invoices.extend(Invoice.get_with_indexed_value(
             str('user_id'), str(user.id)))
-
-        def cmp_invoice(a, b):
-            t = cmp(a.regie_id, b.regie_id)
-            if t != 0:
-                return t
-            return -cmp(a.date, b.date)
-
-        invoices.sort(cmp_invoice)
+        invoices.sort(key=lambda x: (x.regie_id, -x.date))
 
         last_regie_id = None
         unpaid = False
@@ -127,7 +120,7 @@ class JsonDirectory(Directory):
         for formdef in formdefs:
             user_forms.extend(formdef.data_class().get_with_indexed_value(
                         'user_id', self.user.id))
-        user_forms.sort(lambda x,y: cmp(x.receipt_time, y.receipt_time))
+        user_forms.sort(key=lambda x: x.receipt_time)
 
         get_response().set_content_type('application/json')
 
@@ -193,7 +186,7 @@ class MyspaceDirectory(wcs.myspace.MyspaceDirectory):
             for formdef in formdefs:
                 user_forms.extend(formdef.data_class().get_with_indexed_value(
                             'user_id', user.id))
-            user_forms.sort(lambda x,y: cmp(x.receipt_time, y.receipt_time))
+            user_forms.sort(key=lambda x: x.receipt_time)
 
         profile_links = []
         if not get_cfg('sp', {}).get('idp-manage-user-attributes', False):
diff --git a/auquotidien/modules/root.py b/auquotidien/modules/root.py
index a823274..d2fc06b 100644
--- a/auquotidien/modules/root.py
+++ b/auquotidien/modules/root.py
@@ -19,6 +19,7 @@ except ImportError:
 import wcs
 import wcs.root
 from wcs import qommon
+from wcs.forms.root import RootDirectory as FormsRootDirectory
 from wcs.qommon import get_cfg, get_logger
 from wcs.qommon import template
 from wcs.qommon import errors
@@ -70,70 +71,6 @@ Category.get_limit = category_get_limit
 Category.TEXT_ATTRIBUTES = ['name', 'url_name', 'description', 'homepage_position']
 Category.INT_ATTRIBUTES = ['position', 'limit']
 
-
-class FormsRootDirectory(wcs.forms.root.RootDirectory):
-
-    def _q_index(self, *args):
-        get_response().filter['is_index'] = True
-        return wcs.forms.root.RootDirectory._q_index(self, *args)
-
-    def user_forms(self, user_forms):
-        r = TemplateIO(html=True)
-        base_url = get_publisher().get_root_url()
-
-        draft = [x for x in user_forms if x.is_draft() and not x.formdef.is_disabled()]
-        if draft:
-            r += htmltext('<h4 id="drafts">%s</h4>') % _('My Current Drafts')
-            r += htmltext('<ul>')
-            for f in draft:
-                if f.formdef.category:
-                    category_url = '%s' % f.formdef.category.url_name
-                else:
-                    category_url = '.'
-                r += htmltext('<li><a href="%s%s/%s/%s">%s</a>, %s') % (base_url,
-                    category_url,
-                    f.formdef.url_name, f.id, f.formdef.name,
-                    misc.localstrftime(f.receipt_time))
-                r += htmltext(' (<a href="%s%s/%s/%s?remove-draft">%s</a>)') % (base_url,
-                    category_url,
-                    f.formdef.url_name, f.id, _('delete'))
-                r += htmltext('</li>')
-            r += htmltext('</ul>')
-
-        forms_by_status_name = {}
-        for f in user_forms:
-            if f.is_draft():
-                continue
-            status = f.get_visible_status()
-            if status:
-                status_name = status.name
-            else:
-                status_name = None
-            if status_name in forms_by_status_name:
-                forms_by_status_name[status_name].append(f)
-            else:
-                forms_by_status_name[status_name] = [f]
-        for status_name in forms_by_status_name:
-            if status_name:
-                r += htmltext('<h4>%s</h4>') % _('My forms with status "%s"') % status_name
-            else:
-                r += htmltext('<h4>%s</h4>') % _('My forms with an unknown status') % status_name
-            r += htmltext('<ul>')
-            forms_by_status_name[status_name].sort(lambda x,y: cmp(x.receipt_time, y.receipt_time))
-            for f in forms_by_status_name[status_name]:
-                if f.formdef.category_id:
-                    category_url = f.formdef.category.url_name
-                else:
-                    category_url = '.'
-                r += htmltext('<li><a href="%s%s/%s/%s/">%s</a>, %s</li>') % (
-                        base_url,
-                        category_url,
-                        f.formdef.url_name, f.id, f.formdef.name, 
-                        misc.localstrftime(f.receipt_time))
-            r += htmltext('</ul>')
-        return r.getvalue()
-
-
 OldRegisterDirectory = wcs.root.RegisterDirectory
 
 class AlternateRegisterDirectory(OldRegisterDirectory):
diff --git a/tests/test_user_pages.py b/tests/test_user_pages.py
index 65b0711..8ff9ad8 100644
--- a/tests/test_user_pages.py
+++ b/tests/test_user_pages.py
@@ -94,13 +94,14 @@ def test_myspace_with_user_forms():
 
     app = login(get_app(pub), username='user', password='user')
     resp = app.get('/myspace/')
-    assert 'Status1' in resp
-    assert '<a href="/cat/test/%s/"' % formdata.id in resp
-    assert 'Draft' in resp
-    assert '<a href="/cat/test/%s"' % draft.id in resp
-    resp = app.get('/cat/test/%s' % formdata.id)
+    assert formdata.id != draft.id
+    assert '<a href="test/%s/"' % formdata.id in resp
+    assert '<a href="test/%s"' % draft.id in resp
+    resp = app.get('/test/%s' % formdata.id)
     resp.status_int = 200
-    resp = app.get('/cat/test/%s' % draft.id, status=302)
+    resp = app.get('/test/%s' % draft.id, status=302)
+    resp = resp.follow(status=302)
+    resp.location.startswith('http://example.net/test/?mt=')
     resp = resp.follow(status=302)
     resp.location.startswith('http://example.net/cat/test/?mt=')
     resp = resp.follow(status=200)
@@ -109,15 +110,14 @@ def test_myspace_with_user_forms():
     formdef.disabled = True
     formdef.store()
     resp = app.get('/myspace/')
-    assert 'Status1' in resp
-    assert '<a href="/cat/test/%s/"' % formdata.id in resp
-    assert not 'Draft' in resp
-    assert not '<a href="/cat/test/%s"' % draft.id in resp
+    assert formdata.id != draft.id
+    assert '<a href="test/%s/"' % formdata.id in resp
+    assert not '<a href="test/%s"' % draft.id in resp
     resp = app.get('/cat/test/%s' % formdata.id)
     resp.status_int = 200
     resp = app.get('/cat/test/%s' % draft.id, status=302)
     resp = resp.follow(status=302)
-    resp.location.startswith('http://example.net/cat/test/?mt=')
+    resp.location.startswith('http://example.net/test/?mt=')
     resp = resp.follow(status=403)
 
 def test_form_category_redirection():
-- 
2.24.1

