Selecting random images from the skin in Plone 3 (from a viewlet)

by Matt Sital-Singh on Jan 11, 2008
Filed Under:

How to easily create a viewlet that displays a random image selected from the skin:

from Products.CMFCore.utils import getToolByName
from random import shuffle

class BannerViewlet:
   """ utility methods for banner """

   def getPhotos(self):
       skins = getToolByName(self.context, 'portal_skins')
       # Looks in the 'photos' subdir of the
       # 'myskin_custom_images' skins folder (this folder
       # must be registered in skins.zcml with recursive=True to include
       # the 'photos' subdir)
       photos = skins['my_custom_images']['photos'].objectIds()
       shuffle(photos)
       # Return the relative urls of our 5 random images
       return [ ('photos/%s' % x) for x in photos[:5] ]
Filed under: , ,

Commenting has now closed on this post.

Follow us

— via Twitter

Is proudly sponsoring #BlueLightCamp today. If you want to come talk Open Source content management @HammerToe is there #blcamp
last month