snapshots: add navigation links (#50010)

This commit is contained in:
Lauréline Guérin 2021-01-12 15:39:09 +01:00
parent 910fe6597d
commit e833ab2ba0
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 88 additions and 0 deletions

View File

@ -339,6 +339,25 @@ def test_form_snapshot_browse(pub, formdef_with_history):
resp = resp.click(href='%s/view/' % snapshot.id)
assert 'This form is readonly' in resp.text
assert '<p>%s</p>' % localstrftime(snapshot.timestamp) in resp.text
assert '<a class="button disabled" href="#">&Lt;</a>' in resp.text
assert '<a class="button disabled" href="#">&LT;</a>' in resp.text
assert '<a class="button" href="../../%s/view/">&GT;</a>' % (snapshot.id - 1) in resp.text
assert '<a class="button" href="../../%s/view/">&Gt;</a>' % (snapshot.id - 6) in resp.text
resp = resp.click(href='../../%s/view/' % (snapshot.id - 1))
assert '<a class="button" href="../../%s/view/">&Lt;</a>' % snapshot.id in resp.text
assert '<a class="button" href="../../%s/view/">&LT;</a>' % snapshot.id in resp.text
assert '<a class="button" href="../../%s/view/">&GT;</a>' % (snapshot.id - 2) in resp.text
assert '<a class="button" href="../../%s/view/">&Gt;</a>' % (snapshot.id - 6) in resp.text
resp = resp.click(href='../../%s/view/' % (snapshot.id - 6))
assert '<a class="button" href="../../%s/view/">&Lt;</a>' % snapshot.id in resp.text
assert '<a class="button" href="../../%s/view/">&LT;</a>' % (snapshot.id - 5) in resp.text
assert '<a class="button disabled" href="#">&GT;</a>' in resp.text
assert '<a class="button disabled" href="#">&Gt;</a>' in resp.text
resp = resp.click(href='../../%s/view/' % snapshot.id)
resp = resp.click('Description')
assert resp.form['description'].value == 'this is a description (5)'
assert [x[0].name for x in resp.form.fields.values() if x[0].tag == 'button'] == ['cancel']

View File

@ -62,4 +62,21 @@ def snapshot_info_block(snapshot):
parts.append(misc.localstrftime(snapshot.timestamp))
r += htmltext('<br />').join(parts)
r += htmltext('</p>')
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))
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))
else:
# currently browsing the last snapshot, display links as disabled
r += htmltext(' <a class="button disabled" href="#">&GT;</a>')
r += htmltext(' <a class="button disabled" href="#">&Gt;</a>')
r += htmltext('</p>')
return r.getvalue()

View File

@ -1920,6 +1920,10 @@ ul.snapshots-list .new-day, ul.snapshots-list .has-label {
display: block;
}
p.snapshots-navigation {
text-align: center;
}
#sidebar-custom-views .as-data-source {
font-weight: normal;
}

View File

@ -102,6 +102,54 @@ class Snapshot:
self._user = UnknownUser()
return self._user
def load_history(self):
if not self.instance:
self._history = []
return
history = get_publisher().snapshot_class.select_object_history(self.instance)
self._history = [s.id for s in history]
@property
def previous(self):
if not hasattr(self, '_history'):
self.load_history()
try:
idx = self._history.index(self.id)
except ValueError:
return None
if idx == 0:
return None
return self._history[idx - 1]
@property
def next(self):
if not hasattr(self, '_history'):
self.load_history()
try:
idx = self._history.index(self.id)
except ValueError:
return None
try:
return self._history[idx + 1]
except IndexError:
return None
@property
def first(self):
if not hasattr(self, '_history'):
self.load_history()
return self._history[0]
@property
def last(self):
if not hasattr(self, '_history'):
self.load_history()
return self._history[-1]
def restore(self, as_new=True):
instance = self.instance
if as_new: