Compare commits

..

1 Commits

Author SHA1 Message Date
Frédéric Péters 18a0a14f07 misc: allow prefilling file fields with a dictionary (#25385)
gitea/wcs/pipeline/head There was a failure building this commit Details
2024-03-22 16:01:54 +01:00
3 changed files with 56 additions and 2 deletions

View File

@ -526,6 +526,38 @@ def test_form_file_field_dict_prefill(pub):
assert formdata.data['0'].get_content() == b'hello\n'
@responses.activate
def test_form_file_field_url_prefill(pub):
FormDef.wipe()
formdef = FormDef()
formdef.name = 'test'
formdef.fields = [
fields.FileField(
id='0',
label='file',
prefill={'type': 'string', 'value': 'http://example.net/hello.txt'},
)
]
formdef.store()
responses.get('http://example.net/hello.txt', body=b'Hello\n', content_type='text/plain')
resp = get_app(pub).get('/test/')
assert resp.form['f0$token'].value
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'
pub.loggederror_class.wipe()
responses.get('http://example.net/hello.txt', status=404)
resp = get_app(pub).get('/test/')
assert not resp.form['f0$token'].value
assert 'hello.txt' not in resp.text
assert [x.summary for x in pub.loggederror_class.select()] == ['Failed to convert value for field "file"']
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

@ -16,6 +16,7 @@
import base64
import os
import urllib.parse
import xml.etree.ElementTree as ET
from django.utils.encoding import force_bytes, force_str
@ -139,6 +140,20 @@ class FileField(WidgetField):
upload = PicklableUpload(value.filename, value.content_type)
upload.receive([value.content])
return upload
value = misc.unlazy(value)
if isinstance(value, str) and urllib.parse.urlparse(value).scheme in ('http', 'https'):
try:
response, status, data, dummy = misc.http_get_page(value, raise_on_http_errors=True)
except misc.ConnectionError:
pass
else:
value = {
'filename': os.path.basename(urllib.parse.urlparse(value).path) or _('file.bin'),
'content': data,
'content_type': response.headers.get('content-type'),
}
if isinstance(value, dict):
# if value is a dictionary we expect it to have a content or
# b64_content key and a filename keys and an optional

View File

@ -924,13 +924,20 @@ class FileWithPreviewWidget(CompositeWidget):
return False
def set_value(self, value):
if isinstance(value, dict):
if isinstance(value, (str, dict)):
from wcs.fields.file import FileField
try:
value = FileField.convert_value_from_anything(value)
except ValueError:
except ValueError as e:
value = None
if getattr(self, 'field', None):
get_publisher().record_error(
_('Failed to convert value for field "%s"') % self.field.label,
formdef=getattr(self, 'formdef', None),
exception=e,
)
try:
self.value = value
if self.value and self.get_value_from_token: