workflows: add history entry after successful edit action, not before (#52629)

This commit is contained in:
Frédéric Péters 2021-04-03 20:11:15 +02:00
parent 3fff9b48a5
commit 29ac5ab0d3
5 changed files with 38 additions and 7 deletions

View File

@ -2534,6 +2534,7 @@ def test_backoffice_wfedit(pub):
resp = app.get('/backoffice/management/form-title/%s/' % number31.id)
assert (' with the number %s.' % number31.get_display_id()) in resp.text
assert len(form_class().get(number31.id).evolution) == 2 # (just submitted, new)
resp = resp.form.submit('button_wfedit')
resp = resp.follow()
assert 'http://www.example.com/test.pdf' in resp.text # make sure sidebar has details
@ -2546,6 +2547,8 @@ def test_backoffice_wfedit(pub):
resp = resp.form.submit('submit')
resp = resp.follow()
assert form_class().get(number31.id).data['2'] == 'bar'
assert len(form_class().get(number31.id).evolution) == 3
assert form_class().get(number31.id).evolution[-1].who == str(user.id)
number31.store()

View File

@ -2504,6 +2504,8 @@ def test_form_multi_page_post_edit(pub):
resp = page.forms[0].submit('button_editable')
assert resp.location.startswith('http://example.net/test/%s/wfedit-' % data_id)
resp = resp.follow()
# check there's no new "phantom" history entry
assert len(formdef.data_class().get(data_id).evolution) == 1
assert resp.forms[0]['f1'].value == 'foo'
resp.forms[0]['f1'] = 'foo2'
@ -2518,6 +2520,9 @@ def test_form_multi_page_post_edit(pub):
resp = resp.follow()
assert 'foo2' in resp.text # modified value is there
assert 'barXYZ' in resp.text # unchanged value is still there
assert len(formdef.data_class().get(data_id).evolution) == 2 # new history entry
assert formdef.data_class().get(data_id).evolution[-1].who == '_submitter'
assert formdef.data_class().get(data_id).evolution[-1].status is None
# modify workflow to jump to another status after the edition
st2 = workflow.add_status('Status2', 'st2')
@ -2545,6 +2550,9 @@ def test_form_multi_page_post_edit(pub):
assert 'foo3' in resp.text # modified value is there
assert 'barXYZ' in resp.text # unchanged value is still there
assert formdef.data_class().get(data_id).status == 'wf-%s' % st2.id
assert len(formdef.data_class().get(data_id).evolution) == 3 # single new history entry
assert formdef.data_class().get(data_id).evolution[-1].who == '_submitter'
assert formdef.data_class().get(data_id).evolution[-1].status == 'wf-%s' % st2.id
# jump to a nonexistent status == do not jump, but add a LoggedError
if pub.is_using_postgresql():

View File

@ -630,7 +630,7 @@ class FormData(StorableObject):
except KeyError:
return None
def jump_status(self, status_id):
def jump_status(self, status_id, user_id=None):
if status_id == '_previous':
previous_status = self.pop_previous_marked_status()
if not previous_status:
@ -655,6 +655,7 @@ class FormData(StorableObject):
evo = Evolution(self)
evo.time = time.localtime()
evo.status = status
evo.who = user_id
self.evolution.append(evo)
self.status = status
self.store()

View File

@ -34,7 +34,7 @@ from quixote.html import TemplateIO, htmltext
from quixote.util import randbytes
from wcs.categories import Category
from wcs.formdata import FormData
from wcs.formdata import Evolution, FormData
from wcs.formdef import FormDef
from wcs.forms.common import FormStatusPage, FormTemplateMixin
from wcs.qommon.admin.texts import TextsDirectory
@ -1414,10 +1414,24 @@ class FormPage(Directory, FormTemplateMixin):
url = None
for item in wf_status.items:
if item.id == self.edit_action_id:
user = get_request().user
user_id = None
if user:
if get_request().is_in_frontoffice() and self.edited_data.is_submitter(user):
user_id = '_submitter'
else:
user_id = user.id
wf_status = item.get_target_status(self.edited_data)
if wf_status:
self.edited_data.jump_status(wf_status[0].id)
self.edited_data.jump_status(wf_status[0].id, user_id=user_id)
url = self.edited_data.perform_workflow()
else:
# add history entry
evo = Evolution()
evo.time = time.localtime()
evo.who = user_id
self.edited_data.evolution.append(evo)
self.edited_data.store()
break
return redirect(url or '.')

View File

@ -1675,10 +1675,15 @@ class WorkflowStatus:
break
if next_url:
if not form.has_errors():
filled.evolution.append(evo)
if evo.status:
filled.status = evo.status
filled.store()
if evo.parts or evo.status or evo.comment or evo.status:
# add evolution entry only if there's some content
# within, i.e. do not register anything in the case of
# a single edit action (where the evolution should be
# appended only after successful edit).
filled.evolution.append(evo)
if evo.status:
filled.status = evo.status
filled.store()
return next_url
if form.has_errors():