[Zope] conceptual problem with acquisition

Rik Hoekstra rik.hoekstra@inghist.nl
Mon, 05 Mar 2001 15:37:29 +0100


Oliver Frommel wrote:
> 
> > include a PythonScript in your zclass (or somewhere else) with the following
> > code (name: findlogo; standard bindings):
> >
> > try:
> >     getattr(context.pics, 'logo') # if logo is the name of the picture
> >     logo = context.pics.logo
> > except AttributeError:
> >     logo = context.<fallbackdirectory>.pics.logo # in which fallback directory
> > is your rootdir
> >
> > return logo
> >
> >
> 
> yes, thank you very much!
> However I changed the PythonScript a bit to
> a) take a variable
> b) operate on strings, i.e. also return a string
> 
> PythonScript findimage, parameter "image":
> 
> try:
>     getattr(context.pic, image)
>     image = "pics/" + image
> except AttributeError:
>     image = "dir/pics/" + image
> return image


why do you need to return it a string?

> 
> I call it from a DTML method like that:
> 
> <img src="<dtml-var "findimage('logo.gif')">">
> 
> yes, this is not nice, and I am not really content with it ..
> 
> how could I e.g. make your example work when using an image with a "normal"
> name, i.e. period and extension. I think I can hardly make all Zope end users
> (Designers, Editors ..) make switching from that to underscore naming!
> 
> >     getattr(context.pics, 'logo') # if logo is the name of the picture
> >     logo = context.pics.logo
> 
> thanks for your help


I changed my example to work with a 'normal' name and with a customizable
'default directory'. It will also return a url instead of an image. If you ask
me, using 
<dtml-var "findlogo(imagename='somename' defaultdir='somedirectory')"> is easier
for endusers/designers etc. than the verbose 
<img src="<dtml-var "findlogo(imagename='logo.jpg',
defaultdir='somedirectory')">"> and the result is the same...
Note that you could change the 'defaultdir' argument to a keyword argument. 
Note also that you could change the hardcoded 'pics' to another (keyword)
argument.

------------
## Script (Python) "findlogo"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=imagename, defaultdir
##title=
##
if not getattr(context.pics, imagename):
    directory = getattr(context, defaultdir)
else: directory = context
picsdir = getattr(directory, 'pics')
logo = getattr(picsdir, imagename)

return logo.absolute_url()
------------

hth

Rik