[Zope3-Users] Problem with @@absolute_url

Darryl Cousins darryl at darrylcousins.net.nz
Mon Apr 30 19:09:15 EDT 2007


Hi,

On Mon, 2007-04-30 at 22:23 +0200, Maciej Wisniowski wrote:
> > class StudentDetails:
> >     """Helper to return a student photo."""
> >     def getPhoto(self):
> >         student = self.context
> >         return StudentPhoto(student)
> > 
> > In my page template, I can see that the IImage object is being created 
> > correctly..
> > 
> > <div tal:define="photo view/getPhoto">
> >     <span tal:content="photo">#</span>
> > </div>
> > 
> > gives me <zope.app.file.image.Image object at 0xb75c6c6c>.  But to turn this 
> > object into a URL so that I can put it in an <img> tag is beyond me.  When I 
> > try to do an @@absolute_url on the photo object I get the error saying 
> > there's not enough context.
> > 
> > Am I going about this the wrong way?  Is there some other thing I need to be 
> > doing in order for there to be enough context?
> Hm... not sure if it is good solution but it is just a quick though so
> possibly somebody may give you something better. Maybe there are ready
> solutions for this. In general if you want
> to return image then you have to return it's data to the browser (not
> Image object) and you also have to set proper headers like content-type
> and content-length. I don't remember exactly how it should go but you
> should find examples with google or see how z3c.image does this etc.
> This should be something like:
> 
> 
> class StudentDetails:
>      """Helper to return a student photo."""
>      def __call__(self):
>          student = self.context
>          self.request.setHeader('content-type', 'image/png')
>          # determine and set content-length here
>          return StudentPhoto(student).data # not sure if it was 'data'
>                                            # check image.py
> 
> If you declare this in zcml as:
> 
> <browser:view
>    name='myphoto'
>    class=StudentDetails'
>    permission='zope.Public'
>    for=.....
> 
> then you may use url just like:
> 
> student_object/myphoto
> 
> and this will execute __call__ method of the view class.
> 

Maciej's code should do it for you but will need to be a browser view:

class StudentPhotoView(BrowserView):
	etc

<browser:view
   name='myphoto'
   class=StudentPhotoView'
   permission='zope.Public'
   for=IStudent />

Also take a look at zope.app.file.browser.image.py for ImageData which
takes the trouble to set the correct headers (which Maciej also
implies).

Hope this helps.

Darryl



More information about the Zope3-users mailing list