misc: add get_base64_content() to get file content as base64 (#39306)

This commit is contained in:
Frédéric Péters 2020-01-27 16:16:18 +01:00
parent 67f74027c7
commit 4bba415725
2 changed files with 9 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import base64
import json
import datetime
import os
@ -3665,6 +3666,8 @@ def test_set_backoffice_field_file(http_requests, two_pubs):
assert formdata.data['bo1'].base_filename == 'test.jpeg'
assert formdata.data['bo1'].content_type == 'image/jpeg'
assert formdata.data['bo1'].get_content() == open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read()
assert formdata.data['bo1'].get_base64_content() == base64.encodestring(
open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb').read())
# same test with PicklableUpload wcs.qommon.form
from wcs.qommon.form import PicklableUpload as PicklableUpload2

View File

@ -866,6 +866,12 @@ class PicklableUpload(Upload):
return open(filename, 'rb').read()
return None
def get_base64_content(self):
content = self.get_content()
if content:
return base64.encodestring(content)
return None
class EmailWidget(StringWidget):
HTML_TYPE = 'email'