forms: add an _url variant to vars pointing to form worflow files (#8031)

This commit is contained in:
Frédéric Péters 2015-08-20 11:54:06 +02:00
parent d2ad37617f
commit 7cdd62d119
3 changed files with 25 additions and 3 deletions

View File

@ -48,4 +48,10 @@ xref="misc-substvars">variables de substitution</link>, sous la forme
champ</var></code> (par exemple : <code>contact_interne_var_telephone</code>).
</p>
<note><p>
Pour les champs de type fichier, la variable contiendra le nom du fichier.
L'adresse du fichier sera présente dans la variable nommée
<code><var>variable du formulaire</var>_var_<var>variable du champ</var>_url</code>.
</p></note>
</page>

View File

@ -1078,6 +1078,10 @@ def test_formdata_form_file_download(pub):
formdata = formdef.data_class().select()[0]
assert 'xxx_var_yyy_raw' in formdata.workflow_data
resp = resp.test_app.get(resp.location + 'files/form-xxx-yyy/test.txt')
assert resp.content_type == 'text/plain'
assert resp.body == 'foobar'
download = resp.test_app.get(resp.location + 'files/form-xxx-yyy/test.txt')
assert download.content_type == 'text/plain'
assert download.body == 'foobar'
# go back to the status page, this will exercise the substitution variables
# codepath.
resp = resp.follow()

View File

@ -17,6 +17,7 @@
import copy
import datetime
import json
import re
import sys
import time
@ -426,6 +427,17 @@ class FormData(StorableObject):
if self.workflow_data:
d.update(self.workflow_data)
# pass over uploaded files and attach an extra attribute with the
# url to the file.
for k, v in self.workflow_data.items():
if isinstance(v, Upload):
try:
formvar, fieldvar = re.match('(.*)_var_(.*)_raw$', k).groups()
except AttributeError:
continue
d[k.rsplit('_', 1)[0] + '_url'] = '%sfiles/form-%s-%s/%s' % (
d['form_url'], formvar, fieldvar,
self.workflow_data['%s_var_%s' % (formvar, fieldvar)])
d = copy.deepcopy(d)
flatten_dict(d)