Skip to content

Commit

Permalink
- Enable WebDAV PUT factories to change a newly created object's ID
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Jan 6, 2022
1 parent 15fff90 commit 870366c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
4.6.4 (unreleased)
------------------

- Enable WebDAV PUT factories to change a newly created object's ID
(`#997 <https://github.com/zopefoundation/Zope/issues/997>`_)

- Fix potential race condition in ``App.version_txt.getZopeVersion``
(`#999 <https://github.com/zopefoundation/Zope/issues/999>`_)

Expand Down
3 changes: 3 additions & 0 deletions src/webdav/NullResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def PUT(self, REQUEST, RESPONSE):
(ob.__class__, repr(parent), sys.exc_info()[1],)
raise Unauthorized(sMsg)

# A PUT factory may have changed the object's ID
name = ob.getId() or name

# Delegate actual PUT handling to the new object,
# SDS: But just *after* it has been stored.
self.__parent__._setObject(name, ob)
Expand Down
17 changes: 17 additions & 0 deletions src/webdav/tests/testPUT_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,20 @@ def testCollector2261(self):
'PUT factory should not acquire content')
# check for the newly created file
self.assertEqual(str(self.app.A.B.a), 'bar')

def testPUT_factory_changes_name(self):
# A custom PUT factory may want to change the object ID,
# for example to remove file name extensions.
from OFS.Image import File

def custom_put_factory(name, typ, body):
new_name = 'newname'
if not isinstance(body, bytes):
body = body.encode('UTF-8')
return File(new_name, '', body, content_type=typ)
self.app.folder.PUT_factory = custom_put_factory

request = self.app.REQUEST
put = request.traverse('/folder/doc')
put(request, request.RESPONSE)
self.assertTrue('newname' in self.folder.objectIds())

0 comments on commit 870366c

Please sign in to comment.