misc: provide formdata context when updating relations (#89652)
gitea/wcs/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2024-04-16 16:23:41 +02:00
parent 992bc9720a
commit b9b6912385
2 changed files with 80 additions and 0 deletions

View File

@ -1519,6 +1519,80 @@ def test_card_update_related_with_custom_view(pub):
assert formdata.data['1_display'] == 'view-card1-change1'
def test_card_update_related_with_items_dynamic_custom_view(pub):
CardDef.wipe()
FormDef.wipe()
pub.custom_view_class.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.fields = [
StringField(id='1', label='Test', varname='foo'),
StringField(id='2', label='Test2'),
]
carddef.digest_templates = {
'default': '{{ form_var_foo }}',
'custom-view:view': 'view-{{ form_var_foo }}',
}
carddef.store()
carddef.data_class().wipe()
carddata1 = carddef.data_class()()
carddata1.data = {'1': 'card1', '2': 'ok'}
carddata1.just_created()
carddata1.store()
carddata2 = carddef.data_class()()
carddata2.data = {'1': 'card2', '2': 'ok'}
carddata2.just_created()
carddata2.store()
custom_view = pub.custom_view_class()
custom_view.title = 'view'
custom_view.formdef = carddef
custom_view.columns = {'list': [{'id': 'id'}]}
custom_view.filters = {}
custom_view.filters = {'filter-2': 'on', 'filter-2-value': '{{ form_var_data }}'}
custom_view.visibility = 'datasource'
custom_view.store()
formdef = FormDef()
formdef.name = 'foo'
formdef.fields = [
StringField(id='0', label='Foo', varname='data'),
ItemsField(id='1', label='Test', data_source={'type': 'carddef:foo:view'}),
]
formdef.store()
formdata = formdef.data_class()()
formdata.data = {'0': 'ok', '1': ['1']}
formdata.data['1_display'] = 'view-card1'
assert formdata.data['1_display'] == 'view-card1'
formdata.just_created()
formdata.store()
# check usual situation, carddata changed but is still present in the result set
pub.cleanup()
carddef = carddef.get(carddef.id)
carddata1 = carddef.data_class().get(carddata1.id)
carddata1.data = {'1': 'card1-change1', '2': 'ok'}
carddata1.store()
formdata.refresh_from_storage()
assert formdata.data['1'] == ['1']
assert formdata.data['1_display'] == 'view-card1-change1'
# check with a card that will no longer be part of the custom view result set
pub.cleanup()
carddef = carddef.get(carddef.id)
carddata1 = carddef.data_class().get(carddata1.id)
carddata1.data = {'1': 'card1-change2', '2': 'ko'}
carddata1.store()
formdata.refresh_from_storage()
assert formdata.data['1_display'] == 'view-card1-change1' # no update, but data still here
def test_card_update_related_cascading(pub):
BlockDef.wipe()
CardDef.wipe()

View File

@ -163,6 +163,7 @@ class UpdateRelationsAfterJob(AfterJob):
return
klass = {'carddef': CardDef, 'formdef': FormDef}
publisher = get_publisher()
# check all known reverse relations
for obj_ref in {x['obj'] for x in carddef.reverse_relations}:
@ -207,6 +208,11 @@ class UpdateRelationsAfterJob(AfterJob):
if objdata_seen_key in update_related_seen:
# do not allow updates to cycle back
continue
publisher.reset_formdata_state()
publisher.substitutions.feed(objdata.formdef)
publisher.substitutions.feed(objdata)
objdata_changed = False
for field in fields:
if getattr(field, 'block_field', None):