[Zope-CVS] CVS: Packages/Moztop/idesupport/http - configure.zcml:1.1 put.py:1.1

Sidnei da Silva sidnei@x3ng.com.br
Mon, 10 Mar 2003 15:59:34 -0500


Update of /cvs-repository/Packages/Moztop/idesupport/http
In directory cvs.zope.org:/tmp/cvs-serv3218/http

Added Files:
	configure.zcml put.py 
Log Message:
experimental put support. header sniffing for content type name

=== Added File Packages/Moztop/idesupport/http/configure.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/zope'>

<content class=".put.NullResource">
  <allow interface="zope.app.interfaces.http.INullResource" />
</content>

<view 
  for="zope.app.interfaces.http.INullResource"
  name="PUT"
  type="zope.publisher.interfaces.http.IHTTPPresentation"
  factory=".put.NullPUT"
  permission="zope.Public"
  allowed_attributes="PUT"
  />

<adapter 
  for="zope.app.interfaces.content.folder.IFolder"
  provides="zope.app.interfaces.file.IFileFactory"
  name="PageTemplate"
  factory="zope.app.content.zpt.ZPTFactory"
  permission="zope.ManageContent"
  />

</zopeConfigure>


=== Added File Packages/Moztop/idesupport/http/put.py ===
##############################################################################
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: put.py,v 1.1 2003/03/10 20:59:33 sidnei Exp $
"""
__metaclass__ = type

from zope.component import getAdapter, queryAdapter
from zope.app.interfaces.http import INullResource
from zope.app.interfaces.file import IWriteFile, IWriteDirectory, IFileFactory
from zope.app.interfaces.container import IZopeWriteContainer
from zope.app.event import publish
from zope.app.event.objectevent import ObjectCreatedEvent
        
class NullPUT:
    """Put handler for null resources (new file-like things)

    This view creates new objects in containers.
    """

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def PUT(self):
        request = self.request

        for name in request:
            if name.startswith('HTTP_CONTENT_'):
                # Unimplemented content header
                request.response.setStatus(501)
                return ''

        body = request.bodyFile
        name = self.context.name
        container = self.context.container

        # Find the object type
        type_name = request.getHeader('X-Zope-Type-Name', '')

        # Find the extension
        ext_start = name.rfind('.')
        if ext_start > 0:
            ext = name[ext_start:]
        else:
            ext = "."

        # Get a "directory" surrogate for the container
        dir = queryAdapter(container, IWriteDirectory)

        # Get the zope adapter for that
        dir = getAdapter(dir, IZopeWriteContainer)

        # Now try to get a custom factory for he container by
        # type name
        factory = queryAdapter(container, IFileFactory, name=type_name)

        if factory is None:
            # Now try to get a custom factory for he container
            factory = queryAdapter(container, IFileFactory, name=ext)

        # Fall back to a non-custom one
        if factory is None:
            factory = getAdapter(container, IFileFactory)

        # XXX Need to add support for large files
        data = body.read()

        newfile = factory(name, request.getHeader('content-type', ''), data)
        publish(self.context, ObjectCreatedEvent(newfile))
        dir.setObject(name, newfile)

        request.response.setStatus(201)
        return ''