From 4bba4157253ddc5affe7ea0d2c8f4128a6685e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 27 Jan 2020 16:16:18 +0100 Subject: [PATCH] misc: add get_base64_content() to get file content as base64 (#39306) --- tests/test_workflows.py | 3 +++ wcs/qommon/form.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 037f764b2..5e1f13e5b 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -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 diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 15edd7beb..b0b0d4f10 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -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'