Commit ccaa343d authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki Committed by Jérome Perrin

py2/py3: str.decode('base64') and bytes.encode('base64') don't exist in Python 3.

parent 80b343f0
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
import six.moves.urllib.request import six.moves.urllib.request
from lxml import etree from lxml import etree
from erp5.component.document.Document import ConversionError from erp5.component.document.Document import ConversionError
import base64
from Products.ERP5Type.Utils import bytes2str
SVG_DEFAULT_NAMESPACE = "http://www.w3.org/2000/svg" SVG_DEFAULT_NAMESPACE = "http://www.w3.org/2000/svg"
...@@ -38,7 +40,7 @@ def getDataURI(url): ...@@ -38,7 +40,7 @@ def getDataURI(url):
except Exception as e: except Exception as e:
raise ConversionError("Error to transform url (%s) into data uri. ERROR = %s" % (url, Exception(e))) raise ConversionError("Error to transform url (%s) into data uri. ERROR = %s" % (url, Exception(e)))
return 'data:%s;base64,%s' % (data.info()["content-type"], return 'data:%s;base64,%s' % (data.info()["content-type"],
data.read().encode("base64").replace('\n', "")) bytes2str(base64.b64encode(data.read())).replace('\n', ""))
def transformUrlToDataURI(content): def transformUrlToDataURI(content):
if content is None or len(content) == 0: if content is None or len(content) == 0:
......
...@@ -192,7 +192,7 @@ ...@@ -192,7 +192,7 @@
<dictionary> <dictionary>
<item> <item>
<key> <string>_text</string> </key> <key> <string>_text</string> </key>
<value> <string>python: \'data:%s;base64,%s\' %(here.getContentType(), here.getData().encode("base64").replace(\'\\n\', ""))</string> </value> <value> <string>python: \'data:%s;base64,%s\' %(here.getContentType(), modules['Products.ERP5Type.Utils'].bytes2str(modules['base64'].b64encode(here.getData())).replace(\'\\n\', ""))</string> </value>
</item> </item>
</dictionary> </dictionary>
</pickle> </pickle>
......
...@@ -104,7 +104,7 @@ ...@@ -104,7 +104,7 @@
<draw:frame draw:style-name="fr1" draw:name="Image1" text:anchor-type="as-char" svg:width="3in" draw:z-index="0" <draw:frame draw:style-name="fr1" draw:name="Image1" text:anchor-type="as-char" svg:width="3in" draw:z-index="0"
tal:attributes="draw:name python:'embedded_erp5_image_%s' % image.getId(); tal:attributes="draw:name python:'embedded_erp5_image_%s' % image.getId();
svg:height python: '%1.1fin' % (3.0 * image.getHeight() / (image.getWidth() or 1.0))"> svg:height python: '%1.1fin' % (3.0 * image.getHeight() / (image.getWidth() or 1.0))">
<draw:image><office:binary-data tal:content="python:str(image.convert(format='png')[1]).encode('base64')" /></draw:image> <draw:image><office:binary-data tal:content="python:modules['base64'].b64encode(image.convert(format='png')[1])" /></draw:image>
</draw:frame> </draw:frame>
</text:p> </text:p>
</tal:block> </tal:block>
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.testDms import makeFileUpload from erp5.component.test.testDms import makeFileUpload
from time import time from time import time
import base64
class TestRunMyDoc(ERP5TypeTestCase): class TestRunMyDoc(ERP5TypeTestCase):
""" """
...@@ -132,7 +133,7 @@ class TestRunMyDoc(ERP5TypeTestCase): ...@@ -132,7 +133,7 @@ class TestRunMyDoc(ERP5TypeTestCase):
self.tic() self.tic()
# The right image were updated. # The right image were updated.
image_upload.seek(0) image_upload.seek(0)
self.assertEqual(image_page_2.getData(), image_upload.read().decode("base64")) self.assertEqual(image_page_2.getData(), base64.b64decode(image_upload.read()))
self.assertEqual(image_page_2.getFilename(), image_reference + '.png') self.assertEqual(image_page_2.getFilename(), image_reference + '.png')
self.assertEqual(image_page.getData(), '') self.assertEqual(image_page.getData(), '')
......
import base64
portal = context.getPortalObject() portal = context.getPortalObject()
# Update Photos # Update Photos
...@@ -9,7 +10,7 @@ for record in portal.expense_record_module.objectValues(portal_type="Expense Rec ...@@ -9,7 +10,7 @@ for record in portal.expense_record_module.objectValues(portal_type="Expense Rec
if ticket.getReference(): if ticket.getReference():
photo_data = photo_data.split(",")[1] photo_data = photo_data.split(",")[1]
image = portal.portal_contributions.newContent( image = portal.portal_contributions.newContent(
data = photo_data.decode('base64'), data = base64.b64decode(photo_data),
reference=ticket.getReference()+ "-justificatif", reference=ticket.getReference()+ "-justificatif",
title = ticket.getReference() + " Justificatif", title = ticket.getReference() + " Justificatif",
description = ticket.getDescription(), description = ticket.getDescription(),
......
import base64
import json import json
portal = context.getPortalObject() portal = context.getPortalObject()
record = context record = context
...@@ -80,7 +81,7 @@ if photo_data: ...@@ -80,7 +81,7 @@ if photo_data:
if "application/pdf" in photo_type: if "application/pdf" in photo_type:
filename="tmp.pdf" filename="tmp.pdf"
image = portal.portal_contributions.newContent( image = portal.portal_contributions.newContent(
data = photo_data.decode('base64'), data = base64.b64decode(photo_data),
reference=ticket.getReference()+ "-justificatif", reference=ticket.getReference()+ "-justificatif",
title = ticket.getReference() + " Justificatif", title = ticket.getReference() + " Justificatif",
description = ticket.getDescription(), description = ticket.getDescription(),
......
""" """
Upload a screenshot taken by the test to ERP5 Upload a screenshot taken by the test to ERP5
""" """
import base64
data_uri = context.REQUEST.form.get('data_uri', 'default') data_uri = context.REQUEST.form.get('data_uri', 'default')
image_module = getattr(context, "image_module", None) image_module = getattr(context, "image_module", None)
...@@ -16,7 +17,7 @@ if image is None or image.getPortalType() != "Image": ...@@ -16,7 +17,7 @@ if image is None or image.getPortalType() != "Image":
image.setContentType('image/png') image.setContentType('image/png')
data_text = data_uri.read() data_text = data_uri.read()
data = data_text.decode('base64') data = base64.b64decode(data_text)
image.edit(data=data, image.edit(data=data,
filename=str(image_reference) + '.png', filename=str(image_reference) + '.png',
......
...@@ -30,6 +30,7 @@ from Products.ERP5Type.tests.ERP5TypeTestCase import \ ...@@ -30,6 +30,7 @@ from Products.ERP5Type.tests.ERP5TypeTestCase import \
ERP5TypeTestCase, immediateCompilation ERP5TypeTestCase, immediateCompilation
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
from runUnitTest import tests_home from runUnitTest import tests_home
import base64
import glob import glob
import shutil import shutil
import os import os
...@@ -280,9 +281,9 @@ class TestBusinessTemplateTwoFileExport(ERP5TypeTestCase): ...@@ -280,9 +281,9 @@ class TestBusinessTemplateTwoFileExport(ERP5TypeTestCase):
self.assertEqual(image_page.getProperty(property_id), property_value) self.assertEqual(image_page.getProperty(property_id), property_value)
png_data = """iVBORw0KGgoAAAANSUhEUgAAAAUA png_data = base64.b64decode(b"""iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==""".decode("base64") 9TXL0Y4OHwAAAABJRU5ErkJggg==""")
def test_twoFileImportExportForImageIdentifyingTypeByContent(self): def test_twoFileImportExportForImageIdentifyingTypeByContent(self):
""" """
......
...@@ -7,6 +7,7 @@ from six.moves import cStringIO as StringIO ...@@ -7,6 +7,7 @@ from six.moves import cStringIO as StringIO
from json import dumps from json import dumps
from Acquisition import aq_base from Acquisition import aq_base
from six.moves.urllib.parse import urljoin from six.moves.urllib.parse import urljoin
import base64
class GadgetWidget(Widget.Widget): class GadgetWidget(Widget.Widget):
""" """
...@@ -144,7 +145,7 @@ class GadgetFieldValidator(Validator.Validator): ...@@ -144,7 +145,7 @@ class GadgetFieldValidator(Validator.Validator):
if value is not None: if value is not None:
if field.get_value('data_url'): if field.get_value('data_url'):
value=value.split(",")[1] value=value.split(",")[1]
return StringIO(value.decode('base64')) return StringIO(base64.b64decode(value))
return value return value
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment