[Zope] PUT_factory solution

Adam Warner lists@consulting.net.nz
02 Feb 2002 13:10:07 +1300


Hi all,

Thanks for the PUT_factory tip last night. I see this is a common issue
with proposed solutions:

http://www.zope.org/Wikis/DevSite/Proposals/ObjectTypeAssociationAndDeathToIndexHtml

My initial solution is based upon a 12 October 2001 post by Andreas
Wandel.

My objective was to turn all text/* content into the content type file
(NOT DTML content). And allow for DTML content to be uploaded using a
.dtml suffix.

You just create an external method named externalmethodname.py in the
Extensions folder:

	def PUT_factory(self,name,typ,body):
		from OFS.Image import Image,File
		from OFS.DTMLDocument import DTMLDocument

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

		elif name[-5:]=='.dtml':
			ob=DTMLDocument('',__name__=name)

		else:
			ob=File(name,'',body,content_type=typ)
		return ob

	import webdav.NullResource
	webdav.NullResource.NullResource._default_PUT_factory = PUT_factory


And create a link to that External Method using the Zope management
interface:

ID		externalmethodname
Title
Module Name	externalmethodname
Function Name	PUT_factory

I think I will also modify the external method so that anything I upload
called 'index_html' is turned into dtml content by default.

Thanks everyone. I misunderstood Chris' initial post as implying that
this was only a cosmetic error message issue. A descriptive error
message would have reduced the number of frustrating hours trying to
work out what was wrong. Instead of the error message being "426 Error
creating file" perhaps it could be:

   Zope tried to create a [insert DTML Document/DTML method/etc.] object.
   However your content could not be correctly parsed. Please investigate
   writing your own custom PUT_factory external method.

By the way, if Zope was really smart it could automatically turn any
text content that turns out to be illegal DTML into a text file type
instead of rejecting the file.

Regards,
Adam