Returning a random image from a folder
Going mad with this one... I'm trying to write a Python script to return a random image from a specified folder. For example, here's how I may want to call it from a ZPT page: <span tal:replace="randomImage('Advertisements/BannerAds') / > My randomImage Python script is as follows (where 'directory' is the parameter being passed to the script): import string import random directory_context = string.replace("/", ".", directory) return directory + "/" + random.choice(context.directory_context.objectValues('Image')) Some of you may recognize this as being almost an exact copy of the script on p151 of "The Zope Book", with the only difference being that I'm trying to pass a folder name as a parameter. I'm pretty sure I need to do the directory_context step, but I can't get it to work either with or without it. Where I'm really struggling is in getting my head around the context stuff. Can anyone offer me some assistance? Thanks in advance Dave Mitchell
Take a look at the restrictedTraverse() method to traverse to an object by its path e.g. context.restrictedTraverse('/path/to/ads/bannerads'). -aj --On Samstag, 1. März 2003 19:47 +1100 David Mitchell <djmitchell@optushome.com.au> wrote:
Going mad with this one...
I'm trying to write a Python script to return a random image from a specified folder. For example, here's how I may want to call it from a ZPT page:
<span tal:replace="randomImage('Advertisements/BannerAds') / >
My randomImage Python script is as follows (where 'directory' is the parameter being passed to the script):
import string import random
directory_context = string.replace("/", ".", directory) return directory + "/" + random.choice(context.directory_context.objectValues('Image'))
Some of you may recognize this as being almost an exact copy of the script on p151 of "The Zope Book", with the only difference being that I'm trying to pass a folder name as a parameter.
I'm pretty sure I need to do the directory_context step, but I can't get it to work either with or without it. Where I'm really struggling is in getting my head around the context stuff.
Can anyone offer me some assistance?
Thanks in advance
Dave Mitchell
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
-- --------------------------------------------------------------------- - Andreas Jung http://www.andreas-jung.com - - EMail: andreas at andreas-jung.com - - "Life is too short to (re)write parsers" - ---------------------------------------------------------------------
My randomImage Python script is as follows (where 'directory' is the parameter being passed to the script):
import string import random
directory_context = string.replace("/", ".", directory) return directory + "/" + random.choice(context.directory_context.objectValues('Image'))
Passing in a string makes this trickier, as I guess you've already realized. What you're doing in context.directory_context.objectValues('Image') is trying to call a string (directory_context) like an ObjectManager. Not going to work. Python lets you get away with a lot, but not that much. As suggested previously, you can turn the string into an object by restrictedTraverse(). In similar situations, involving non-traversable attributes, you might use Python's getattr() built-in. --jcc
participants (3)
-
Andreas Jung -
David Mitchell -
J Cameron Cooper