[Zope] Returning a random image from a folder

Andreas Jung Andreas Jung <lists@andreas-jung.com>
Sat, 01 Mar 2003 09:57:36 +0100


Take a look at the restrictedTraverse() method to traverse to an
object by its path e.g.=20
context.restrictedTraverse('/path/to/ads/bannerads').

-aj

--On Samstag, 1. M=E4rz 2003 19:47 +1100 David Mitchell=20
<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=3D"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 =3D 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 )




--=20
    ---------------------------------------------------------------------
   -    Andreas Jung                     http://www.andreas-jung.com   -
  -   EMail: andreas at andreas-jung.com                              -
   -            "Life is too short to (re)write parsers"               -
    ---------------------------------------------------------------------