workflows: allow a card to modify itself (#53789)
This commit is contained in:
parent
51d935656a
commit
6a37360efb
|
@ -6915,6 +6915,57 @@ def test_edit_carddata_manual_targeting(two_pubs):
|
||||||
assert carddef.data_class().get(7).data['2'] == '3'
|
assert carddef.data_class().get(7).data['2'] == '3'
|
||||||
|
|
||||||
|
|
||||||
|
def test_edit_carddata_targeting_itself(pub):
|
||||||
|
CardDef.wipe()
|
||||||
|
carddef = CardDef()
|
||||||
|
carddef.name = 'Foo Card'
|
||||||
|
carddef.fields = [
|
||||||
|
StringField(id='0', label='foo', varname='foo'),
|
||||||
|
]
|
||||||
|
carddef.store()
|
||||||
|
carddef.data_class().wipe()
|
||||||
|
|
||||||
|
# card workflow: update itself then jump to second status
|
||||||
|
card_wf = Workflow(name='Card workflow')
|
||||||
|
st1 = card_wf.add_status('Status1')
|
||||||
|
st2 = card_wf.add_status('Status2')
|
||||||
|
|
||||||
|
edit = EditCarddataWorkflowStatusItem()
|
||||||
|
edit.id = '_edit'
|
||||||
|
edit.formdef_slug = carddef.url_name
|
||||||
|
edit.target_mode = 'manual'
|
||||||
|
edit.target_id = '{{ form_internal_id }}' # itself
|
||||||
|
edit.mappings = [
|
||||||
|
Mapping(field_id='0', expression='bar {{ form_internal_id }}'),
|
||||||
|
]
|
||||||
|
edit.parent = st1
|
||||||
|
st1.items.append(edit)
|
||||||
|
|
||||||
|
jump = JumpWorkflowStatusItem()
|
||||||
|
jump.id = '_jump'
|
||||||
|
jump.status = st2.id
|
||||||
|
st1.items.append(jump)
|
||||||
|
jump.parent = st1
|
||||||
|
|
||||||
|
card_wf.store()
|
||||||
|
|
||||||
|
carddef.workflow = card_wf
|
||||||
|
carddef.store()
|
||||||
|
|
||||||
|
# create some cardata
|
||||||
|
for i in range(1, 4):
|
||||||
|
carddata = carddef.data_class()()
|
||||||
|
carddata.data = {
|
||||||
|
'0': 'foo %s' % i,
|
||||||
|
}
|
||||||
|
carddata.store()
|
||||||
|
# run workflow, verify that carddata is modified
|
||||||
|
carddata.just_created()
|
||||||
|
carddata.perform_workflow()
|
||||||
|
assert carddata.data['0'] == 'bar %s' % carddata.id
|
||||||
|
assert carddata.status == 'wf-%s' % st2.id
|
||||||
|
|
||||||
|
|
||||||
def test_edit_carddata_from_created_object(pub):
|
def test_edit_carddata_from_created_object(pub):
|
||||||
FormDef.wipe()
|
FormDef.wipe()
|
||||||
CardDef.wipe()
|
CardDef.wipe()
|
||||||
|
|
|
@ -47,10 +47,15 @@ class EditCarddataWorkflowStatusItem(CreateCarddataWorkflowStatusItem, ExternalW
|
||||||
if not carddef:
|
if not carddef:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
formdata.store()
|
||||||
|
|
||||||
for target_data in self.iter_target_datas(formdata, carddef):
|
for target_data in self.iter_target_datas(formdata, carddef):
|
||||||
self.apply_mappings(dest=target_data, src=formdata)
|
self.apply_mappings(dest=target_data, src=formdata)
|
||||||
with get_publisher().substitutions.freeze():
|
with get_publisher().substitutions.freeze():
|
||||||
target_data.store()
|
target_data.store()
|
||||||
|
|
||||||
|
# update local object as it may have modified itself
|
||||||
|
formdata.refresh_from_storage()
|
||||||
|
|
||||||
|
|
||||||
register_item_class(EditCarddataWorkflowStatusItem)
|
register_item_class(EditCarddataWorkflowStatusItem)
|
||||||
|
|
Loading…
Reference in New Issue