Projet

Général

Profil

0001-workflows-allow-add-attachment-with-only-a-varname-3.patch

Thomas Noël, 10 mai 2019 14:31

Télécharger (4,52 ko)

Voir les différences:

Subject: [PATCH] workflows: allow add attachment with only a varname (#32983)

 tests/test_form_pages.py | 33 +++++++++++++++++++++++++++++++++
 wcs/wf/attachment.py     |  5 +++--
 wcs/workflows.py         |  5 +++--
 3 files changed, 39 insertions(+), 4 deletions(-)
tests/test_form_pages.py
2772 2772
    attach = AddAttachmentWorkflowStatusItem()
2773 2773
    attach.id = '_attach'
2774 2774
    attach.by = ['_submitter']
2775
    assert attach.attach_to_history == True
2775 2776
    st1.items.append(attach)
2776 2777
    attach.parent = st1
2777 2778
    wf.store()
......
2813 2814
    st1 = wf.add_status('Status1', 'st1')
2814 2815
    attach = AddAttachmentWorkflowStatusItem()
2815 2816
    attach.varname = 'attached_doc'
2817
    assert attach.attach_to_history == True
2816 2818
    attach.id = '_attach'
2817 2819
    attach.by = ['_submitter']
2818 2820
    st1.items.append(attach)
......
2866 2868
    assert content_disposition.split(';')[0] == 'attachment'
2867 2869
    assert resp.request.environ['PATH_INFO'].endswith(attachment_variable.filename)
2868 2870

  
2871
    # do not show in history, only in substitution variables
2872
    attach.attach_to_history = False
2873
    wf.store()
2874
    formdef.data_class().wipe()
2875

  
2876
    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
2877
    resp = resp.forms[0].submit('submit')
2878
    resp = resp.forms[0].submit('submit')
2879
    resp = resp.follow()
2880
    assert 'The form has been recorded' in resp.body
2881
    resp.forms[0]['attachment_attach'] = Upload('test.txt', 'foobar', 'text/plain')
2882
    resp = resp.forms[0].submit('button_attach')
2883
    assert formdef.data_class().count() == 1
2884
    formdata = formdef.data_class().select()[0]
2885
    assert formdata.evolution[-1].parts[0].__class__.__name__ == 'AttachmentEvolutionPart'
2886
    attachment = formdata.evolution[-1].parts[0]
2887
    assert attachment.content_type == 'text/plain'
2888
    assert attachment.orig_filename == 'test.txt'
2889

  
2890
    resp = resp.follow()  # back to form page
2891
    # no link to the document
2892
    assert 'test.txt' not in resp.body
2893
    # substitution variable is ok
2894
    variables = formdef.data_class().select()[0].get_substitution_variables()
2895
    assert 'attachments' in variables
2896
    attachments = variables['attachments']
2897
    assert attachments is not None
2898
    doc = attachments.attached_doc
2899
    # cannot download the document
2900
    resp = login(get_app(pub), username='foo', password='foo').get(doc.url, status=404)
2901

  
2869 2902
def test_formdata_attachment_download_to_backoffice_file_field(pub):
2870 2903
    create_user(pub)
2871 2904
    wf = Workflow(name='status')
wcs/wf/attachment.py
135 135
                    filename,
136 136
                    content_type,
137 137
                    outstream.read())
138
            if self.attach_to_history:
138
            if self.attach_to_history or self.varname:
139 139
                f.fp.seek(0)
140
                evo.add_part(AttachmentEvolutionPart.from_upload(f, varname=self.varname))
140
                evo.add_part(AttachmentEvolutionPart.from_upload(f, varname=self.varname,
141
                                                                 is_visible=bool(self.attach_to_history)))
141 142

  
142 143
    def get_parameters(self):
143 144
        parameters = ('by', 'required', 'title', 'display_title', 'button_label',
wcs/workflows.py
179 179
        self.is_visible = is_visible
180 180

  
181 181
    @classmethod
182
    def from_upload(cls, upload, varname=None):
182
    def from_upload(cls, upload, varname=None, is_visible=True):
183 183
        return AttachmentEvolutionPart(
184 184
                upload.base_filename,
185 185
                upload.fp,
186 186
                upload.orig_filename,
187 187
                upload.content_type,
188 188
                upload.charset,
189
                varname=varname)
189
                varname=varname,
190
                is_visible=is_visible)
190 191

  
191 192
    def get_file_pointer(self):
192 193
        return open(self.filename)
193
-