Skip to content

Commit

Permalink
Port implementation to DTMLDocument.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Jun 12, 2018
1 parent e4cf806 commit 95413c1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ https://github.com/zopefoundation/Zope/blob/4.0a6/CHANGES.rst
- Add a minimum ``buildout.cfg`` suggestion in the docs for creating ``wsgi``
instances.

- Fix ZMI upload of `DTMLMethod` and `DTMLDocument` to store the DTML as a
native ``str`` on both Python versions.


4.0b5 (2018-05-18)
------------------
Expand Down
14 changes: 11 additions & 3 deletions src/OFS/DTMLDocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
from AccessControl.class_init import InitializeClass
from DocumentTemplate.permissions import change_dtml_methods
from DocumentTemplate.permissions import change_dtml_documents
from six import PY2
from six import PY3
from six import binary_type
from six import text_type
from six.moves.urllib.parse import quote
from zExceptions import ResourceLockedError
from zExceptions.TracebackSupplement import PathTracebackSupplement
Expand Down Expand Up @@ -55,10 +58,15 @@ def manage_upload(self, file='', REQUEST=None):
if self.wl_isLocked():
raise ResourceLockedError('This document has been locked.')

if not isinstance(file, binary_type):
if REQUEST and not file:
raise ValueError('No file specified')
if REQUEST and not file:
raise ValueError('No file specified')

if hasattr(file, 'read'):
file = file.read()
if PY3 and isinstance(file, binary_type):
file = file.decode('utf-8')
if PY2 and isinstance(file, text_type):
file = file.encode('utf-8')

self.munge(file)
self.ZCacheable_invalidate()
Expand Down
35 changes: 35 additions & 0 deletions src/OFS/tests/test_DTMLDocument.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import io
import unittest


Expand All @@ -15,6 +17,39 @@ def test_class_conforms_to_IWriteLock(self):
from OFS.interfaces import IWriteLock
verifyClass(IWriteLock, self._getTargetClass())

def test_manage_upload__bytes(self):
"""It stores uploaded bytes as a native str."""
doc = self._makeOne()
data = u'bÿtës'.encode('utf-8')
self.assertIsInstance(data, bytes)
doc.manage_upload(data)
self.assertEqual(doc.read(), 'bÿtës')
self.assertIsInstance(doc.read(), str)

def test_manage_upload__str(self):
"""It stores an uploaded str as a native str."""
doc = self._makeOne()
data = 'bÿtës'
doc.manage_upload(data)
self.assertEqual(doc.read(), 'bÿtës')
self.assertIsInstance(doc.read(), str)

def test_manage_upload__StringIO(self):
"""It stores StringIO contents as a native str."""
doc = self._makeOne()
data = io.StringIO(u'bÿtës')
doc.manage_upload(data)
self.assertIsInstance(doc.read(), str)
self.assertEqual(doc.read(), 'bÿtës')

def test_manage_upload__BytesIO(self):
"""It stores BytesIO contents as a native str."""
doc = self._makeOne()
data = io.BytesIO(u'bÿtës'.encode('utf-8'))
doc.manage_upload(data)
self.assertEqual(doc.read(), 'bÿtës')
self.assertIsInstance(doc.read(), str)


class FactoryTests(unittest.TestCase):

Expand Down

0 comments on commit 95413c1

Please sign in to comment.