Projet

Général

Profil

0001-workflows-store-attachment-references-as-templates-6.patch

Frédéric Péters, 28 juin 2022 11:09

Télécharger (7,77 ko)

Voir les différences:

Subject: [PATCH] workflows: store attachment references as templates (#66398)

 tests/admin_pages/test_workflow.py | 46 +++++++++++++++++++++---------
 wcs/workflows.py                   | 21 +++++++++++---
 2 files changed, 49 insertions(+), 18 deletions(-)
tests/admin_pages/test_workflow.py
1061 1061

  
1062 1062
    # attachments without backoffice fields: python expressions
1063 1063
    resp = app.get(item_url)
1064
    assert "Attachments (templates or Python expressions)" in resp.text
1064
    assert "Attachments (templates)" in resp.text
1065 1065
    resp.form['attachments$element0'] = 'form_var_upload_raw'
1066 1066
    resp = resp.form.submit('submit')
1067 1067
    assert resp.location
1068 1068
    resp = app.get(item_url)
1069
    assert "Attachments (templates or Python expressions)" in resp.text
1069
    assert "Attachments (templates)" in resp.text
1070 1070
    assert resp.form['attachments$element0'].value == 'form_var_upload_raw'
1071 1071
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1072 1072
    assert sendmail.attachments == ['form_var_upload_raw']
......
1082 1082
    workflow.store()
1083 1083
    resp = app.get(item_url)
1084 1084
    assert "Attachments" in resp.text
1085
    assert "Attachments (templates or Python expressions)" not in resp.text
1086
    assert resp.form['attachments$element0$choice'].value == 'form_var_upload_raw'
1085
    assert "Attachments (templates)" not in resp.text
1086
    assert resp.form['attachments$element0$choice'].value == '{{form_var_upload_raw}}'
1087 1087
    assert len(resp.form['attachments$element0$choice'].options) == 5
1088 1088
    resp = resp.form.submit('attachments$add_element')  # add one
1089
    resp.form['attachments$element1$choice'] = 'form_var_upload2_raw'
1089
    resp.form['attachments$element1$choice'] = '{{form_var_upload2_raw}}'
1090 1090
    resp = resp.form.submit('submit')
1091 1091
    assert resp.location
1092 1092
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1093
    assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw']
1093
    assert sendmail.attachments == ['{{form_var_upload_raw}}', '{{form_var_upload2_raw}}']
1094 1094

  
1095 1095
    resp = app.get(item_url)
1096 1096
    resp = resp.form.submit('attachments$add_element')  # add one
1097
    resp.form['attachments$element2$choice'] = 'form_fbo3_3x'
1097
    resp.form['attachments$element2$choice'] = '{{form_fbo3_3x}}'
1098 1098
    resp = resp.form.submit('submit')
1099 1099
    assert resp.location
1100 1100
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1101
    assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw', 'form_fbo3_3x']
1101
    assert sendmail.attachments == ['{{form_var_upload_raw}}', '{{form_var_upload2_raw}}', '{{form_fbo3_3x}}']
1102 1102

  
1103 1103
    resp = app.get(item_url)
1104 1104
    resp = resp.form.submit('attachments$add_element')  # add one
......
1108 1108
    assert resp.location
1109 1109
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1110 1110
    assert sendmail.attachments == [
1111
        '{{form_var_upload_raw}}',
1112
        '{{form_var_upload2_raw}}',
1113
        '{{form_fbo3_3x}}',
1114
        '{"content":"foo", "filename":"bar.txt"}',
1115
    ]
1116

  
1117
    # check old attachments python expressions are converted to templates
1118
    workflow.refresh_from_storage()
1119
    workflow.get_status(st1.id).items[0].attachments = [
1111 1120
        'form_var_upload_raw',
1112 1121
        'form_var_upload2_raw',
1113
        'form_fbo3_3x',
1122
        '{"content":"foo", "filename":"bar.txt"}',
1123
    ]
1124
    workflow.store()
1125
    resp = app.get(item_url)
1126
    resp = resp.form.submit('submit')
1127
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1128
    assert sendmail.attachments == [
1129
        '{{form_var_upload_raw}}',
1130
        '{{form_var_upload2_raw}}',
1114 1131
        '{"content":"foo", "filename":"bar.txt"}',
1115 1132
    ]
1116 1133

  
......
1125 1142
    assert resp.location
1126 1143
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1127 1144
    assert sendmail.attachments == [
1128
        'form_var_upload_raw',
1129
        'form_var_upload2_raw',
1145
        '{{form_var_upload_raw}}',
1146
        '{{form_var_upload2_raw}}',
1130 1147
        '{"content":"foo", "filename":"bar.txt"}',
1131 1148
    ]
1149

  
1132 1150
    # remove all backoffice fields
1133 1151
    workflow = Workflow.get(workflow.id)
1134 1152
    workflow.backoffice_fields_formdef.fields = []
1135 1153
    workflow.store()
1136 1154
    resp = app.get(item_url)
1137
    assert "Attachments (templates or Python expressions)" in resp.text
1155
    assert "Attachments (templates)" in resp.text
1138 1156
    resp = resp.form.submit('submit')
1139 1157
    assert resp.location
1140 1158
    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
1141 1159
    assert sendmail.attachments == [
1142
        'form_var_upload_raw',
1143
        'form_var_upload2_raw',
1160
        '{{form_var_upload_raw}}',
1161
        '{{form_var_upload2_raw}}',
1144 1162
        '{"content":"foo", "filename":"bar.txt"}',
1145 1163
    ]
1146 1164

  
wcs/workflows.py
2154 2154
            if any(x for x in attribute_value if isinstance(x, int)):
2155 2155
                setattr(self, roles_attribute, [str(x) for x in attribute_value])
2156 2156
                changed = True
2157
        if getattr(self, 'attachments', None):
2158
            if any(x for x in self.attachments if not x.startswith('{{')):
2159
                # convert old attachment python expression to templates
2160
                for field in self.parent.parent.get_backoffice_fields():
2161
                    if field.key != 'file':
2162
                        continue
2163
                    if field.varname:
2164
                        codename = 'form_var_%s_raw' % field.varname
2165
                    else:
2166
                        codename = 'form_f%s' % field.id.replace('-', '_')  # = form_fbo<...>
2167
                    if codename in self.attachments:
2168
                        changed = True
2169
                        self.attachments = ['{{%s}}' % x if x == codename else x for x in self.attachments]
2157 2170
        return changed
2158 2171

  
2159 2172
    def render_as_line(self):
......
2250 2263
                form.add(
2251 2264
                    WidgetList,
2252 2265
                    '%sattachments' % prefix,
2253
                    title=_('Attachments (templates or Python expressions)'),
2266
                    title=_('Attachments (templates)'),
2254 2267
                    element_type=StringWidget,
2255 2268
                    value=attachments,
2256 2269
                    add_element_label=_('Add attachment'),
......
2568 2581
            if field.key != 'file':
2569 2582
                continue
2570 2583
            if field.varname:
2571
                codename = 'form_var_%s_raw' % field.varname
2584
                codename = '{{form_var_%s_raw}}' % field.varname
2572 2585
            else:
2573
                codename = 'form_f%s' % field.id.replace('-', '_')  # = form_fbo<...>
2586
                codename = '{{form_f%s}}' % field.id.replace('-', '_')  # = form_fbo<...>
2574 2587
                varnameless.append(codename)
2575 2588
            attachments_options.append((codename, field.label, codename))
2576 2589
        # filter: do not consider removed fields without varname
2577 2590
        attachments = [
2578 2591
            attachment
2579 2592
            for attachment in self.attachments or []
2580
            if ((not attachment.startswith('form_fbo')) or (attachment in varnameless))
2593
            if ((not attachment.startswith('{{form_fbo')) or (attachment in varnameless))
2581 2594
        ]
2582 2595
        return attachments_options, attachments
2583 2596

  
2584
-