[Zope] Re: hookable PUT

Tres Seaver tseaver@palladion.com
Fri, 01 Jun 2001 01:23:18 -0400


Dondi Bogusky wrote:

> Please help. I've looked at
> http://dev.zope.org/Wikis/DevSite/Proposals/HookablePUTCreation and I think
> this will solve many migration problems, for moving existing Apache wrapped
> sever parsed pages into Zope. How do I use the hookable_PUT to add
> ZopePageTemplates or HTMLDocuments?


You create a method called 'PUT_factory' (an ExternalMethod is easiest,
or a method of a Python class;  PythonScripts will typically need help
to get at the "raw" classes they need for this).  This method looks at the
three parameters which Zope's PUT machinery pases to it (see
webdav.NullResource), in order to determine what kind of object should be
instantiated in response to a WebDAV/FTP/HTTP PUT directed to a non-existant
URL.

For example, here is the version from the CMF SkinsTool, formatted to fit
your screen :)::

   from Products.Pythonscripts import PythonScript
   from OFS.Image import Image
   from OFS.DTMLMethod import DTMLMethod
   try:
       from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
       SUPPORTS_PAGE_TEMPLATES=1
   except ImportError:
       SUPPORTS_PAGE_TEMPLATES=0

   def PUT_factory( self, name, typ, body ):
       """
           Create an appropriate "skin method".
       """
       major, minor = split( typ, '/' )

       if major == 'image':
           return Image( id=name
                       , title=''
                       , file=''
                       , content_type=typ
                       )

       if major == 'text':

           if minor == 'x-python':
               return PythonScript( id=name )

           if minor in ( 'html', 'xml' ) and SUPPORTS_PAGE_TEMPLATES:
               return ZopePageTemplate( name )

           return DTMLMethod( __name__=name )

       return None # Let NullResource figure it out, then

Note a couple of conventions here:

   - The put factory is just supposed to make an "empty" instance
     of the appropriate class;  the NullResource is going to delegate
     the actual PUT invocation to the new instance immediately, so
     you don't need to handle that.  The only reason the body is passed
     is to allow the PUT_factory to "sniff" it, if name and typ aren't
     sufficient.

   - The PUT_factory is not responsible for "seating" the new instance
     into its container.

   - Returning None from the PUT_factory allows NullResource to do the
     standard default, which is:

      o all 'image/*' -> OFS.Image.Image

      o all 'text/*' -> OFS.DTMLDocument.DTMLDocument (*not* Method!)

      o all else -> OFS.Image.File

Hope that helps,

Tres.
--===============================================================
Tres Seaver                                tseaver@digicool.com
Digital Creations     "Zope Dealers"       http://www.zope.org