create_formdata: add attach_to_history option (#39639)

This commit is contained in:
Benjamin Dauvergne 2020-02-07 19:52:59 +01:00 committed by Frédéric Péters
parent 662e3e9008
commit 38ceac5dcb
2 changed files with 35 additions and 5 deletions

View File

@ -5791,8 +5791,8 @@ def test_lazy_eval_with_conditional_workflow_form(pub):
assert context['form_var_foo_bar'] == 'go'
@pytest.fixture
def create_formdata(pub):
@pytest.fixture(params=[{'attach_to_history': True}, {}])
def create_formdata(request, pub):
admin = create_user(pub, is_admin=True)
FormDef.wipe()
@ -5835,6 +5835,7 @@ def create_formdata(pub):
create_formdata.formdef_slug = target_formdef.url_name
create_formdata.keep_user = True
create_formdata.backoffice_submission = True
create_formdata.attach_to_history = request.param.get('attach_to_history', False)
create_formdata.mappings = [
Mapping(field_id='0', expression='=form_var_toto_string'),
Mapping(field_id='1', expression='=form_var_toto_file_raw'),
@ -5949,6 +5950,7 @@ def test_backoffice_create_formdata_map_fields_by_varname(create_formdata):
# create source formdata
formdata = create_formdata['source_formdef'].data_class()()
create_formdata['formdata'] = formdata
upload = PicklableUpload('/foo/bar', content_type='text/plain')
upload.receive([b'hello world'])
formdata.data = {
@ -5997,3 +5999,11 @@ def test_backoffice_create_formdata_map_fields_by_varname(create_formdata):
pq = resp.pyquery.remove_namespaces()
assert pq('.field-type-string .value').text() == 'coucou'
assert pq('.field-type-file .value').text() == 'bar'
resp = app.get(create_formdata['formdata'].get_url(backoffice=True))
pq = resp.pyquery.remove_namespaces()
assert pq('.field-type-string .value').text() == 'coucou'
if create_formdata['create_formdata'].attach_to_history:
assert pq('.wf-links')
else:
assert not pq('.wf-links')

View File

@ -116,12 +116,13 @@ class MappingsWidget(WidgetListAsTable):
class LinkedFormdataEvolutionPart(object):
formdef_class = FormDef
def __init__(self, formdata, varname=None):
def __init__(self, formdata, varname, attach_to_history):
self._formdef = formdata.formdef
self._formdata = formdata
self.formdef_id = formdata.formdef.id
self.formdata_id = formdata.id
self.varname = varname
self.attach_to_history = attach_to_history
@property
def formdef(self):
@ -152,6 +153,16 @@ class LinkedFormdataEvolutionPart(object):
d['form_links_%s' % (part.varname or '*')] = part
return d
def view(self):
if self.attach_to_history:
return htmltext('<p class="wf-links">%s <a href="%s">%s %s</a>') % (
_('Created new form'),
self.formdata and self.formdata.get_url(backoffice=get_request().is_in_backoffice()),
self.formdef.name if self.formdef else _('Deleted'),
self.formdata_id)
else:
return ''
class LazyFormDataLinks(object):
def __init__(self, formdata):
@ -183,6 +194,7 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
mappings = None
varname = None
map_fields_by_varname = False
attach_to_history = False
@classmethod
def is_available(cls, workflow=None):
@ -254,6 +266,10 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
HtmlWidget(
htmltext(
'<div class="infonotice">%s %s</div>') % (_('Common varnames:'), common_varnames)))
if 'attach_to_history' in parameters:
form.add(CheckboxWidget, '%sattach_to_history' % prefix,
title=_('Include new form in the form history'),
value=self.attach_to_history)
def _common_varnames(self):
'''Compute common varnames between the targeted formdef and all formdefs related to the parent workflow.'''
@ -269,7 +285,7 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
def get_parameters(self):
return ('draft', 'formdef_slug', 'map_fields_by_varname', 'mappings', 'backoffice_submission',
'keep_user', 'keep_submission_context', 'varname')
'keep_user', 'keep_submission_context', 'varname', 'attach_to_history')
def perform(self, formdata):
formdef = self.formdef
@ -313,7 +329,11 @@ class CreateFormdataWorkflowStatusItem(WorkflowStatusItem):
get_session().mark_anonymous_formdata(new_formdata)
evo = formdata.evolution[-1]
evo.add_part(self.evolution_part_class(new_formdata, varname=self.varname))
evo.add_part(
self.evolution_part_class(
new_formdata,
varname=self.varname,
attach_to_history=self.attach_to_history))
formdata.store()
def apply_mappings(self, dest, src):