We did exactly what you want. https://github.com/4teamwork/ftw.subsite/tree/master/ftw/subsite/portlets It's a teaser portlet, unfortunately not in a single package. but you can adapt the code to your needs.
We defined a browser view in image.py:
from Acquisition import aq_inner
from zope.publisher.browser import BrowserView
class ImageView(BrowserView):
"""View the image field of the image portlet. We steal header details
from zope.app.file.browser.file and adapt it to use the dublin
core implementation that the Image object here has."""
def __call__(self):
context = aq_inner(self.context)
image = context.image
self.request.response.setHeader('Content-Type', image.contentType)
self.request.response.write(image.data)
Register with zcml (Important the image view is for
your portlet Assignment):
<browser:page
for=".yourportlet.Assignment"
name="image"
class=".image.ImageView"
permission="zope.Public"
/>
In your portlet Renderer you have to define a method, which gets the image (traverse).
from plone.app.portlets.portlets import base
class MyPortletRenderer(base.Renderer)
def image_tag(self):
state = getMultiAdapter((self.context, self.request),
name="plone_portal_state")
portal = state.portal()
assignment_url = \
portal.unrestrictedTraverse(
self.data.assignment_context_path).absolute_url()
return "<img src='%s/%s/@@image' alt=''/>" % (
assignment_url,
self.data.__name__)
Now you are able to use the image in your portlet:
<tal:image content="structure view/image_tag />
I don't know if there is a simpler solution. Naming could be better :-) But it works and it's tested https://github.com/4teamwork/ftw.subsite/blob/master/ftw/subsite/tests/test_subsiteportlet.py