snapshots: don't restore link to snapshot object (#52237)

This commit is contained in:
Frédéric Péters 2021-03-19 12:10:31 +01:00
parent 9853c04ef2
commit e5a12818e2
3 changed files with 39 additions and 0 deletions

View File

@ -219,6 +219,7 @@ def test_form_snapshot_restore(pub, formdef_with_history):
assert FormDef.count() == 2
formdef = FormDef.get(resp.location.split('/')[-2])
assert formdef.url_name != formdef_with_history.url_name
assert not hasattr(formdef, 'snapshot_object')
# restore over
resp = app.get('/backoffice/forms/%s/history/' % formdef_with_history.id)
@ -439,6 +440,30 @@ def test_workflow_snapshot_browse(pub):
assert '<p>%s</p>' % localstrftime(snapshot.timestamp) in resp.text
def test_workflow_snapshot_restore(pub):
create_superuser(pub)
create_role(pub)
Workflow.wipe()
workflow = Workflow(name='test')
workflow.store()
app = login(get_app(pub))
resp = app.get('/backoffice/workflows/%s/history/' % workflow.id)
snapshot = pub.snapshot_class.select_object_history(workflow)[0]
resp = resp.click(href='%s/view/' % snapshot.id)
# restore over
resp = app.get('/backoffice/workflows/%s/history/' % workflow.id)
snapshot = pub.snapshot_class.select_object_history(workflow)[-1]
resp = resp.click(href='%s/restore' % snapshot.id)
resp.form['action'].value = 'overwrite'
resp = resp.form.submit('submit')
assert Workflow.count() == 1
assert not hasattr(Workflow.get(workflow.id), 'snapshot_object')
def test_workflow_with_model_snapshot_browse(pub):
create_superuser(pub)
create_role(pub)
@ -688,3 +713,13 @@ def test_workflow_snapshot_max_len(pub, size_limit):
assert pub.snapshot_class.get(first_id + 4).label is None
assert pub.snapshot_class.get(first_id + 4).timestamp > old_timestamp
assert pub.snapshot_class.get(first_id + 4).instance.name == 'foo bar'
def test_pickle_erroneous_snapshot_object(pub):
# check snapshot object attribute is not restored
formdef = FormDef()
formdef.name = 'basic formdef'
formdef.snapshot_object = 'whatever'
formdef.store()
assert not hasattr(FormDef.get(formdef.id), 'snapshot_object')

View File

@ -1546,6 +1546,9 @@ class FormDef(StorableObject):
self.__dict__ = dict
self._workflow = None
self._start_page = None
if hasattr(self, 'snapshot_object'):
# don't restore snapshot object that would have been stored erroneously
delattr(self, 'snapshot_object')
@classmethod
def storage_load(cls, fd, **kwargs):

View File

@ -168,6 +168,7 @@ class Snapshot:
setattr(instance, attr, getattr(current_object, attr))
delattr(instance, 'readonly')
delattr(instance, 'snapshot_object')
instance.store(
comment=_('Restored snapshot %(id)s (%(timestamp)s)')
% {'id': self.id, 'timestamp': misc.localstrftime(self.timestamp)}