November 11th, 2010

We’ve configured our Plone site so that our news section appears in Google news by regularly submitting a news sitemap to Google. Unfortunately news items that appear in Google don’t have a picture associated with them. Having looked at the Google news guidelines for images it seems this is almost certainly because the image url for the image added to the Plone News item doesn’t include a file extension.

Following a suggestion on the Plone forum To get round this we’re going to try the following:

  • Create a BrowserView with a custom traverser that renders the image
  • Override the news item template to use the url expected by the new browser view

I’ve managed to get this working quite neatly:

from mimetypes import guess_type

from import BrowserView

from zope.publisher.interfaces import NotFound

class ImageView(BrowserView):
"""A view of a new item's image that supports file extensions"""

    def __getitem__(self, partid):
        (type, encoding) = guess_type(partid)
        image_type = self.context.getField('image').getContentType(self.context)

        if not type == image_type:
            raise NotFound(self.context, partid, self.request)

        return self.context.__bobo_traverse__(self.request, partid[:partid.find('.')])

This simple bit of code will work for all image scales and validates that the extension used matches the content type of the image.

Comments are closed.