From f4ef67c199029d707c3ff43eacdf5b0a375e6ed1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 28 Sep 2021 17:04:34 +0200
Subject: [PATCH] snapshots: fix navigation links on fields page (#57319)

---
 tests/test_snapshots.py |  8 ++++++++
 wcs/admin/fields.py     |  2 +-
 wcs/admin/utils.py      | 19 ++++++++++++++-----
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/tests/test_snapshots.py b/tests/test_snapshots.py
index d7c01ac5..99728f84 100644
--- a/tests/test_snapshots.py
+++ b/tests/test_snapshots.py
@@ -401,6 +401,14 @@ def test_form_snapshot_browse(pub, formdef_with_history):
     assert [x[0].name for x in resp.form.fields.values() if x[0].tag == 'button'] == ['cancel']
     assert pub.custom_view_class.count() == 0  # custom views are not restore on preview
 
+    resp = app.get('/backoffice/forms/%s/history/' % formdef_with_history.id)
+    resp = resp.click(href='%s/view/' % (snapshot.id - 1))
+    resp = resp.click(href='fields/')
+    assert '<a class="button" href="../../../%s/view/fields/">&Lt;</a>' % snapshot.id in resp.text
+    assert '<a class="button" href="../../../%s/view/fields/">&LT;</a>' % snapshot.id in resp.text
+    assert '<a class="button" href="../../../%s/view/fields/">&GT;</a>' % (snapshot.id - 2) in resp.text
+    assert '<a class="button" href="../../../%s/view/fields/">&Gt;</a>' % (snapshot.id - 6) in resp.text
+
 
 def test_form_snapshot_browse_with_import_error(pub):
     create_superuser(pub)
diff --git a/wcs/admin/fields.py b/wcs/admin/fields.py
index 1625d4ab..d2a65d07 100644
--- a/wcs/admin/fields.py
+++ b/wcs/admin/fields.py
@@ -351,7 +351,7 @@ class FieldsDirectory(Directory):
             )
             if hasattr(self.objectdef, 'snapshot_object'):
                 get_response().filter['sidebar'] += utils.snapshot_info_block(
-                    snapshot=self.objectdef.snapshot_object
+                    snapshot=self.objectdef.snapshot_object, url_prefix='../../../', url_suffix='fields/'
                 )
         else:
             get_response().filter['sidebar'] = str(self.get_new_field_form(self.page_id))
diff --git a/wcs/admin/utils.py b/wcs/admin/utils.py
index 9cbdefc6..74592d1e 100644
--- a/wcs/admin/utils.py
+++ b/wcs/admin/utils.py
@@ -47,7 +47,7 @@ def last_modification_block(obj):
     return r.getvalue()
 
 
-def snapshot_info_block(snapshot):
+def snapshot_info_block(snapshot, url_prefix='../../', url_suffix=''):
     r = TemplateIO(html=True)
     r += htmltext('<p>')
     parts = []
@@ -64,15 +64,24 @@ def snapshot_info_block(snapshot):
     if snapshot.previous or snapshot.next:
         r += htmltext('<p class="snapshots-navigation">')
         if snapshot.id != snapshot.first:
-            r += htmltext(' <a class="button" href="../../%s/view/">&Lt;</a>' % (snapshot.first))
-            r += htmltext(' <a class="button" href="../../%s/view/">&LT;</a>' % (snapshot.previous))
+            r += htmltext(
+                ' <a class="button" href="%s%s/view/%s">&Lt;</a>' % (url_prefix, snapshot.first, url_suffix)
+            )
+            r += htmltext(
+                ' <a class="button" href="%s%s/view/%s">&LT;</a>'
+                % (url_prefix, snapshot.previous, url_suffix)
+            )
         else:
             # currently browsing the first snapshot, display links as disabled
             r += htmltext(' <a class="button disabled" href="#">&Lt;</a>')
             r += htmltext(' <a class="button disabled" href="#">&LT;</a>')
         if snapshot.id != snapshot.last:
-            r += htmltext(' <a class="button" href="../../%s/view/">&GT;</a>' % (snapshot.next))
-            r += htmltext(' <a class="button" href="../../%s/view/">&Gt;</a>' % (snapshot.last))
+            r += htmltext(
+                ' <a class="button" href="%s%s/view/%s">&GT;</a>' % (url_prefix, snapshot.next, url_suffix)
+            )
+            r += htmltext(
+                ' <a class="button" href="%s%s/view/%s">&Gt;</a>' % (url_prefix, snapshot.last, url_suffix)
+            )
         else:
             # currently browsing the last snapshot, display links as disabled
             r += htmltext(' <a class="button disabled" href="#">&GT;</a>')
-- 
2.33.0

