From 618109bdb968d53bd35f3d7a4f3f839a9768a39d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 15 Oct 2020 16:55:20 +0200
Subject: [PATCH 4/4] backoffice: use order_by param in exports (#47629)

---
 tests/backoffice_pages/test_export.py | 38 +++++++++++++++++++++++++++
 wcs/backoffice/management.py          |  9 ++++---
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/tests/backoffice_pages/test_export.py b/tests/backoffice_pages/test_export.py
index 181a8c6e..1e8322ee 100644
--- a/tests/backoffice_pages/test_export.py
+++ b/tests/backoffice_pages/test_export.py
@@ -327,6 +327,44 @@ def test_backoffice_csv_export_block(pub):
     assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['XfooY', 'Xfoo2Y', '']
 
 
+def test_backoffice_csv_export_ordering(pub):
+    if not pub.is_using_postgresql():
+        pytest.skip('this requires SQL')
+        return
+
+    create_superuser(pub)
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form title'
+    formdef.fields = [
+        fields.ItemField(
+            id='1', label='field 1', type='item',
+            items=['foo', 'bar', 'baz'],
+            display_locations=['validation', 'summary', 'listings']),
+    ]
+    formdef.workflow_roles = {'_receiver': 1}
+    formdef.store()
+
+    formdef.data_class().wipe()
+    formdata = formdef.data_class()()
+    formdata.data = {'1': 'foo', '1_display': 'foo'}
+    formdata.jump_status('new')
+    formdata.store()
+    formdata = formdef.data_class()()
+    formdata.data = {'1': 'bar', '1_display': 'bar'}
+    formdata.jump_status('new')
+    formdata.store()
+
+    app = login(get_app(pub))
+    resp_csv = app.get('/backoffice/management/form-title/csv')
+    assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['-', 'bar', 'New']
+    assert resp_csv.text.splitlines()[2].split(',')[-3:] == ['-', 'foo', 'New']
+    resp_csv = app.get('/backoffice/management/form-title/csv?order_by=id')
+    assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['-', 'foo', 'New']
+    assert resp_csv.text.splitlines()[2].split(',')[-3:] == ['-', 'bar', 'New']
+
+
 def test_backoffice_ods(pub):
     create_superuser(pub)
 
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 70f798fc..a3110a94 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -1963,6 +1963,7 @@ class FormPage(Directory):
         user = get_request().user
         query = get_request().form.get('q')
         criterias = self.get_criterias_from_query()
+        order_by = misc.get_order_by_or_400(get_request().form.get('order_by', None))
 
         class Exporter(object):
             def __init__(self, formpage, formdef, fields, selected_filter):
@@ -1979,7 +1980,7 @@ class FormPage(Directory):
 
                 items, total_count = FormDefUI(self.formdef).get_listing_items(
                         fields, self.selected_filter, user=user, query=query,
-                        criterias=criterias)
+                        criterias=criterias, order_by=order_by)
 
                 for filled in items:
                     csv_output.writerow(tuple(
@@ -2064,6 +2065,7 @@ class FormPage(Directory):
         user = get_request().user
         query = get_request().form.get('q')
         criterias = self.get_criterias_from_query()
+        order_by = misc.get_order_by_or_400(get_request().form.get('order_by', None))
 
         class Exporter(object):
             def __init__(self, formpage, formdef, fields, selected_filter):
@@ -2081,7 +2083,7 @@ class FormPage(Directory):
 
                 items, total_count = FormDefUI(self.formdef).get_listing_items(
                         fields, self.selected_filter, user=user, query=query,
-                        criterias=criterias)
+                        criterias=criterias, order_by=order_by)
 
                 for i, filled in enumerate(items):
                     for j, item in enumerate(self.formpage.get_spreadsheet_line(fields, filled)):
@@ -2129,6 +2131,7 @@ class FormPage(Directory):
         user = get_user_from_api_query_string() or get_request().user
         query = get_request().form.get('q')
         criterias = self.get_criterias_from_query()
+        order_by = misc.get_order_by_or_400(get_request().form.get('order_by', None))
 
         class Exporter(object):
             def __init__(self, formpage, formdef, fields, selected_filter):
@@ -2146,7 +2149,7 @@ class FormPage(Directory):
 
                 items, total_count = FormDefUI(self.formdef).get_listing_items(
                         fields, self.selected_filter, user=user, query=query,
-                        criterias=criterias)
+                        criterias=criterias, order_by=order_by)
 
                 for i, formdata in enumerate(items):
                     for j, item in enumerate(self.formpage.get_spreadsheet_line(fields, formdata)):
-- 
2.28.0

