Skip to content

Commit

Permalink
Merge pull request #70 from zopefoundation/fix-_createObjectByType
Browse files Browse the repository at this point in the history
Fix  create object by type
  • Loading branch information
dataflake committed Oct 4, 2018
2 parents 3ca5ee7 + 5404ecf commit ef45cac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Changelog
2.0b2 (unreleased)
------------------

Bug fixes:

- Proper string/bytes handling for _createObjectByType.
In Python2 everything is written as bytes,
while on Python3 everything is written as text except files and images
which are stored as bytes
[ale-rt]


2.0b1 (2018-05-16)
------------------
Expand Down
29 changes: 12 additions & 17 deletions Products/GenericSetup/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,36 +518,31 @@ def getSnapshotFolder(self):
#
@security.private
def _createObjectByType(self, name, body, content_type):
encoding = self.getEncoding() or 'utf-8'

if six.PY2 and isinstance(body, six.text_type):
encoding = self.getEncoding()
if encoding is None:
body = body.encode('utf-8')
else:
body = body.encode(encoding)
body = body.encode(encoding)

if name.endswith('.py'):

ob = PythonScript(name)
ob.write(body)
return ob

elif name.endswith('.dtml'):

if name.endswith('.dtml'):
ob = DTMLDocument('', __name__=name)
ob.munge(body)
return ob

elif content_type in ('text/html', 'text/xml'):

ob = ZopePageTemplate(name, body, content_type=content_type)
if content_type in ('text/html', 'text/xml'):
return ZopePageTemplate(name, body, content_type=content_type)

elif content_type[:6] == 'image/':
if isinstance(body, six.text_type):
body = body.encode(encoding)

ob = Image(name, '', body, content_type=content_type)

else:
ob = File(name, '', body, content_type=content_type)
if content_type[:6] == 'image/':
return Image(name, '', body, content_type=content_type)

return ob
return File(name, '', body, content_type=content_type)

@security.private
def _ensureSnapshotsFolder(self, subdir=None):
Expand Down
29 changes: 29 additions & 0 deletions Products/GenericSetup/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,11 +898,40 @@ def test_writeDataFile_simple_plain_text(self):
self.assertEqual(fileobj.getContentType(), CONTENT_TYPE)
self.assertEqual(fileobj.data, digits_bytes)

@unittest.skipIf(
six.PY2, 'On Python2 writing unicode files is not supported'
)
def test_writeDataFile_simple_plain_text_unicode(self):
FILENAME = 'simple.txt'
CONTENT_TYPE = 'text/plain'
CONTENT = u'Unicode, with non-ASCII: %s.' % six.unichr(150)

site = DummySite('site').__of__(self.app)
site.setup_tool = DummyTool('setup_tool')
tool = site.setup_tool
ctx = self._makeOne(tool, 'simple', 'latin_1')
ctx.writeDataFile(FILENAME, CONTENT, CONTENT_TYPE)

snapshot = tool.snapshots._getOb('simple')

self.assertEqual(len(snapshot.objectIds()), 1)
self.assertTrue(FILENAME in snapshot.objectIds())

fileobj = snapshot._getOb(FILENAME)

self.assertEqual(fileobj.getId(), FILENAME)
self.assertEqual(fileobj.meta_type, File.meta_type)
self.assertEqual(fileobj.getContentType(), CONTENT_TYPE)
self.assertEqual(fileobj.data, CONTENT.encode(ctx._encoding))

@unittest.skipUnless(
six.PY2, 'On Python2 writing unicode files is not supported'
)
def test_writeDataFile_simple_plain_text_unicode_raises(self):
FILENAME = 'simple.txt'
CONTENT_TYPE = 'text/plain'
CONTENT = u'Unicode, with non-ASCII: %s.' % six.unichr(150)

site = DummySite('site').__of__(self.app)
site.setup_tool = DummyTool('setup_tool')
tool = site.setup_tool
Expand Down

0 comments on commit ef45cac

Please sign in to comment.