[Zope3-Users] images in a content object

Duncan McGreggor duncan.mcgreggor at gmail.com
Wed Sep 28 11:50:04 EDT 2005


On Sep 27, 2005, at 12:21 PM, Duncan McGreggor wrote:

> On Sep 27, 2005, at 12:09 PM, Jean-Marc Orliaguet wrote:
>
>> http://svn.nuxeo.org/trac/pub/file/z3lab/cpsskins/branches/jmo- 
>> perspectives/browser/caching.py
>
>
> On Sep 27, 2005, at 12:13 PM, Benji York wrote:
>
>> Chapter 35 of Stephan's book.
>>
>> I couldn't find "traverse" in the index of Philipp's book, but I'm  
>> sure he covers it somewhere.
>>
>> Also note that it might be better to create a content space image  
>> object instead and just keep a reference to it in your other content  
>> object, then you *could* use absolute URL.
>
> You guys are awesome -- these were just the tips I needed to get past  
> this block -- thanks for your help!

In addition to these fine suggestions, I'll add another. From Stephan  
Richter's code in the schoolbell repository:

   http://tinyurl.com/83bg8

There is a person content object that has a photo attached. To use it,  
I only needed to:

1) add some browser view code, and
2) add the zcml configuration for it.

The original part of my schema that I had posted was this:

class IPage(Interface):
     ...
     ...
     image_file = Bytes(
         title=u"Image",
         description=u"Upload an associated image here",
         required=False)

Then I added this browser view code:

from zope.app.file.image import Image

class ImageView(BrowserView):
     """View that returns a page image."""

     __used_for__ = IPage

     def __call__(self):
         data = self.context.image_file
         image = Image(data)
         if not image:
             raise NotFound(self.context, u'image_file', self.request)
         self.request.response.setHeader('Content-Type',  
image.contentType)
         return data

The final step was the zcml:

   <browser:page
       name="image_file"
       for=".interfaces.IPage"
       class=".browser.ImageView"
       permission="zope.View"
       />

I was then able to create an instance of my content object (a Page,  
implementing IPage), and access the "attached" image via the url  
http://localhost/mypage.html/image_file

Thanks again for all the help, and I hope this is useful for others :-)

d



More information about the Zope3-users mailing list