Commit eee4aaa3 authored by Jérome Perrin's avatar Jérome Perrin

fixup! web_shadir: PY3 compatibility

parent 07ba7f50
......@@ -26,8 +26,9 @@
#
##############################################################################
import hashlib, six.moves.http_client
import hashlib
import six
import six.moves.http_client
from Products.ERP5Type.UnrestrictedMethod import super_user
......@@ -77,7 +78,7 @@ def File_viewAsWeb(self):
# Shortcut if the file is not a Pdata.
data=self.data
if isinstance(data, str):
if isinstance(data, bytes):
# Do this way instead of 'return data'
# to bypass default caching policy manager.
RESPONSE.write(data)
......@@ -85,7 +86,9 @@ def File_viewAsWeb(self):
# For Pdata type, we must iterate and send chunk by chunk.
# And no need to continue if the client closed the connection.
while data and not RESPONSE.stdout._channel.closed:
while data:
if six.PY2 and RESPONSE.stdout._channel.closed:
break
# Send data to the client.
RESPONSE.write(data.data)
# Load next object without keeping previous chunks in memory.
......
......@@ -83,7 +83,7 @@ class TestShaCache(ShaCacheMixin, ERP5TypeTestCase):
"""
result, data = self.postFile()
self.assertEqual(result, six.moves.http_client.CREATED)
self.assertEqual(data, self.key)
self.assertEqual(data, self.key.encode())
self.tic()
......@@ -101,7 +101,7 @@ class TestShaCache(ShaCacheMixin, ERP5TypeTestCase):
"""
result, data = self.postFile()
self.assertEqual(result, six.moves.http_client.CREATED)
self.assertEqual(data, self.key)
self.assertEqual(data, self.key.encode())
self.tic()
......@@ -131,6 +131,7 @@ class TestShaCache(ShaCacheMixin, ERP5TypeTestCase):
self.assertEqual('published', document2.getValidationState())
self.assertEqual('archived', document.getValidationState())
@expectedFailure
def test_put_file_twice_no_tic(self):
self.postFile()
self.commit()
......@@ -140,5 +141,7 @@ class TestShaCache(ShaCacheMixin, ERP5TypeTestCase):
document_list = self.portal.portal_catalog(reference=self.key)
self.assertEqual(2, len(document_list))
expectedFailure(self.assertEqual)(sorted(['archived', 'published']),
# this is the expected failure
self.assertEqual(
sorted(['archived', 'published']),
sorted(q.getValidationState() for q in document_list))
......@@ -25,6 +25,7 @@
#
##############################################################################
import six
import hashlib
from base64 import b64decode
from binascii import a2b_hex
......@@ -33,6 +34,7 @@ from json import dumps, loads
from zExceptions import BadRequest
from DateTime import DateTime
from Products.ERP5Type.UnrestrictedMethod import super_user
from Products.ERP5Type.Utils import unicode2str, str2bytes
def WebSection_getDocumentValue(self, key, portal=None, language=None,\
......@@ -67,10 +69,9 @@ def WebSection_getDocumentValue(self, key, portal=None, language=None,\
validation_state='published')]
temp_file = self.newContent(temp_object=True, portal_type='File', id='%s.txt' % key)
temp_file.setData(dumps(document_list))
temp_file.setData(str2bytes(dumps(document_list)))
temp_file.setContentType('application/json')
return temp_file.getObject()
return None
def WebSection_setObject(self, id, ob, **kw):
......@@ -78,12 +79,14 @@ def WebSection_setObject(self, id, ob, **kw):
Make any change related to the file uploaded.
"""
portal = self.getPortalObject()
ob = ob.getOriginalDocument()
data = self.REQUEST.get('BODY')
try:
metadata, signature = loads(data)
metadata = loads(metadata)
# a few basic checks
b64decode(signature)
b64decode(str2bytes(signature))
if len(a2b_hex(metadata['sha512'])) != 64:
raise Exception('sha512: invalid length')
except Exception as e:
......@@ -126,7 +129,8 @@ def WebSection_putFactory(self, name, typ, body):
document = portal.portal_contributions.newContent(data=body,
filename=name,
discover_metadata=False)
return document
return document.asContext(id=name)
# The following scripts are helpers to search & clean up shadir entries.
# XXX: Due to lack of View skin for shadir, external methods are currently
......@@ -217,10 +221,10 @@ def ShaDir_search(self, filename, summary, delete=False):
document_list.append(document)
metadata = loads(loads(document.getData())[0])
del metadata[u"sha512"]
x[';'.join('%s=%r' % (k, v.encode('utf-8') if type(v) is unicode else v)
x[';'.join('%s=%r' % (k, unicode2str(v))
for k, v in sorted(metadata.iteritems()))].append(
document.getId())
r = '\n'.join('%s %s' % (k, sorted(v)) for k, v in sorted(x.iteritems()))
r = '\n'.join('%s %s' % (k, sorted(v)) for k, v in sorted(six.iteritems(x)))
if delete:
r += '\n' + _deleteDocumentList(self, document_list)
return r
......@@ -36,6 +36,8 @@ from base64 import b64encode
from unittest import expectedFailure
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from erp5.component.test.ShaDirMixin import ShaDirMixin
from Products.ERP5Type.Utils import bytes2str
class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
"""
......@@ -103,6 +105,7 @@ class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
data_set = self.portal.portal_catalog.getResultValue(
reference=self.key)
self.assertEqual(self.key, data_set.getReference())
self.assertNotEqual(self.key, data_set.getId())
self.assertEqual('published', data_set.getValidationState())
# Asserting Document
......@@ -110,6 +113,7 @@ class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
reference=self.sha512sum)
self.assertEqual(self.sha512sum, document.getTitle())
self.assertEqual(self.sha512sum, document.getReference())
self.assertNotEqual(self.sha512sum, document.getId())
self.assertEqual(self.data, document.getData())
self.assertEqual(data_set, document.getFollowUpValue())
self.assertEqual(str(self.expiration_date),
......@@ -131,7 +135,7 @@ class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
information_list = json.loads(data)
self.assertEqual(1, len(information_list))
self.assertEqual(json.dumps(information_list[0]), self.data)
self.assertEqual(json.dumps(information_list[0]), bytes2str(self.data))
def test_post_information_more_than_once(self):
"""
......@@ -162,7 +166,7 @@ class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
information_list = json.loads(data)
self.assertEqual(1, len(information_list))
self.assertEqual(json.dumps(information_list[0]), self.data)
self.assertEqual(json.dumps(information_list[0]), bytes2str(self.data))
@expectedFailure
def test_post_information_more_than_once_no_tic(self):
......@@ -202,7 +206,7 @@ class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
information_list = json.loads(data)
self.assertEqual(1, len(information_list))
self.assertEqual(json.dumps(information_list[0]), self.data)
self.assertEqual(json.dumps(information_list[0]), bytes2str(self.data))
def test_get_information_from_different_data_set(self):
"""
......
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