From 8259db5802547924384c6244c628a2d369d5e99d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 16 Mar 2019 12:24:47 +0100
Subject: [PATCH] backoffice: keep active filters when switching from global to
 map view (#31479)

---
 tests/test_backoffice_pages.py | 14 ++++++++++++++
 wcs/backoffice/management.py   | 22 +++++++++++++++-------
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/tests/test_backoffice_pages.py b/tests/test_backoffice_pages.py
index 2788b14a8..bc72f1c5d 100644
--- a/tests/test_backoffice_pages.py
+++ b/tests/test_backoffice_pages.py
@@ -1100,6 +1100,14 @@ def test_backoffice_map(pub):
     resp = resp.click('Plot on a Map')
     assert 'tile.example.net/' in resp.body
 
+    # check filters are kept
+    resp = app.get('/backoffice/management/form-title/')
+    resp.form['filter'] = 'all'
+    resp = resp.form.submit()
+    resp = resp.click('Plot on a Map')
+    assert resp.form['filter'].value == 'all'
+
+
 def test_backoffice_geojson(pub):
     user = create_user(pub)
     create_environment(pub)
@@ -2922,6 +2930,12 @@ def test_global_map(pub):
     resp = app.get('/backoffice/management/map?q=test')
     assert re.findall(r'data-geojson-url="(.*?)"', resp.body) == ['http://example.net/backoffice/management/geojson?q=test']
 
+    resp = app.get('/backoffice/management/listing')
+    resp.forms['listing-settings']['status'] = 'all'
+    resp = resp.forms['listing-settings'].submit()
+    resp = resp.click('Map View')
+    assert resp.forms['listing-settings']['status'].value == 'all'
+
 def test_formdata_lookup(pub):
     create_user(pub)
     create_environment(pub, set_receiver=False)
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 85a673528..9e632e6a9 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -501,12 +501,16 @@ class ManagementDirectory(Directory):
             r += htmltext('<span class="actions">')
             if get_publisher().is_using_postgresql() and \
                     get_publisher().get_site_option('postgresql_views') != 'false':
-                r += htmltext('<a href="listing">%s</a>') % _('Global View')
+                r += htmltext('<a data-base-href="listing" href="listing">%s</a>') % _('Global View')
                 if get_publisher().has_site_option('per-user-view'):
                     r += htmltext(' <a href="users/">%s</a>') % _('Per User View')
             for formdef in formdefs:
                 if formdef.geolocations:
-                    r += htmltext(' <a href="map">%s</a>') % _('Map View')
+                    url = 'map'
+                    if get_request().get_query():
+                        url += '?' + get_request().get_query()
+                    r += htmltext(' <a data-base-href="map" href="%s">' % url)
+                    r += htmltext('%s</a>') % _('Map View')
                     break
             r += htmltext('</span>')
 
@@ -586,10 +590,10 @@ class ManagementDirectory(Directory):
         r += htmltext('</div>')
         return r.getvalue()
 
-    def get_global_listing_sidebar(self, limit=None, offset=None, order_by=None):
+    def get_global_listing_sidebar(self, limit=None, offset=None, order_by=None, view=''):
         get_response().add_javascript(['jquery.js'])
         DateWidget.prepare_javascript()
-        form = Form(use_tokens=False, id='listing-settings')
+        form = Form(use_tokens=False, id='listing-settings', method='get', action=view)
         form.add(SingleSelectWidget, 'status', title=_('Status'),
                 options=[
                     ('waiting', _('Waiting for an action'), 'waiting'),
@@ -931,7 +935,7 @@ class ManagementDirectory(Directory):
             return r.getvalue()
 
         get_response().filter['sidebar'] = self.get_global_listing_sidebar(
-                limit=limit, offset=offset, order_by=order_by)
+                limit=limit, offset=offset, order_by=order_by, view='listing')
         rt = TemplateIO(html=True)
         rt += htmltext('<div id="appbar">')
         rt += htmltext('<h2>%s</h2>') % _('Global View')
@@ -939,7 +943,11 @@ class ManagementDirectory(Directory):
         rt += htmltext('<a href="forms">%s</a>') % _('Forms View')
         for formdef in FormDef.select(lightweight=True):
             if formdef.geolocations:
-                rt += htmltext(' <a href="map">%s</a>') % _('Map View')
+                url = 'map'
+                if get_request().get_query():
+                    url += '?' + get_request().get_query()
+                rt += htmltext(' <a data-base-href="map" href="%s">' % url)
+                rt += htmltext('%s</a>') % _('Map View')
                 break
         rt += htmltext('</span>')
         rt += htmltext('</div>')
@@ -985,7 +993,7 @@ class ManagementDirectory(Directory):
         }
         attrs.update(get_publisher().get_map_attributes())
 
-        get_response().filter['sidebar'] = self.get_global_listing_sidebar()
+        get_response().filter['sidebar'] = self.get_global_listing_sidebar(view='map')
 
         r += htmltext('<h2>%s</h2>') % _('Global Map')
         r += htmltext('<div %s></div>' % ' '.join(['%s="%s"' % x for x in attrs.items()]))
-- 
2.20.1

