misc: allow prefilling file fields with a dictionary (#25385)
gitea/wcs/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2024-03-22 15:33:57 +01:00
parent e0857ce653
commit 679af42ded
2 changed files with 42 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from wcs.blocks import BlockDef
from wcs.categories import Category
from wcs.formdef import FormDef
from wcs.qommon.errors import ConnectionError
from wcs.wscalls import NamedWsCall
from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
from .test_all import create_user
@ -491,6 +492,40 @@ def test_form_file_field_prefill(pub):
assert formdata.data['0'].get_content().startswith(b'\x89PNG')
@responses.activate
def test_form_file_field_dict_prefill(pub):
NamedWsCall.wipe()
wscall = NamedWsCall()
wscall.name = 'Hello'
wscall.request = {'url': 'http://example.net'}
wscall.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'test'
formdef.fields = [
fields.FileField(
id='0',
label='file',
prefill={'type': 'string', 'value': '{{ webservice.hello }}'},
)
]
formdef.store()
responses.get(
'http://example.net',
json={'b64_content': 'aGVsbG8K', 'filename': 'hello.txt', 'content_type': 'text/plain'},
)
resp = get_app(pub).get('/test/')
assert resp.form['f0$token']
assert resp.click('hello.txt').content_type == 'text/plain'
resp = resp.form.submit('submit') # -> validation
resp = resp.form.submit('submit') # -> submit
formdata = formdef.data_class().select()[0]
assert formdata.data['0'].base_filename == 'hello.txt'
assert formdata.data['0'].get_content() == b'hello\n'
SVG_CONTENT = b'''<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 63.72 64.25" style="enable-background:new 0 0 63.72 64.25;" xml:space="preserve"> <g> </g> </svg>'''

View File

@ -924,6 +924,13 @@ class FileWithPreviewWidget(CompositeWidget):
return False
def set_value(self, value):
if isinstance(value, dict):
from wcs.fields.file import FileField
try:
value = FileField.convert_value_from_anything(value)
except ValueError:
value = None
try:
self.value = value
if self.value and self.get_value_from_token: