conceptual problem with acquisition
Hello, although I already created some sites with Zope I now encountered a problem that I am not able to solve with acquisition, i.e. to make a parent subfolder work as a fallback resource. Directories look like this: /pics/logo.gif /dir/subdir1/pics/logo.gif /dir/subdir1/index_html (subdir1 is a zclass and index_html a method of it) /dir/subdir2/pics/logo.gif /dir/subdir2/index_html (subdir2 is a zclass and index_html a method of it) /dir/subdir3/pics/ (empty!) /dir/subdir3/index_html (subdir3 is a zclass and index_html a method of it) The index_html contains a reference to the pic: <img src="pics/logo.gif"> Now when the subdirX/pics/ doesn't not contain a logo.gif, acquisition also does not work because it stops on finding the pics folder in the subdirX. This is exactly the scenario, Rik Hoekstra describes in http://www.zope.org/Members/Hoekstra/ChangingZopeContexts. Unfortunately non of the workarounds he mentions are appropriate because a) I need an image folder for each subdir (see below) b) I can't reference images from the parent c) the pic directories need to have the same name this all because the rationale behind this is to enable index_html to look differently _dependent_ on the existence of logo.gif. the site designers are meant to be able to change index_html's appearance just by uploading a new logo.gif. If this is not present, the one of the parent should be used. I think it would work if I put the images directly in subdirX and in the root dir but this is pretty ugly and should only serve as a last ressort IMHO. is there any way / product / trick to make this concept work with subdirs?? thanks for your help --Oliver
Oliver Frommel wrote:
Hello,
although I already created some sites with Zope I now encountered a problem that I am not able to solve with acquisition, i.e. to make a parent subfolder work as a fallback resource. Directories look like this:
/pics/logo.gif /dir/subdir1/pics/logo.gif /dir/subdir1/index_html (subdir1 is a zclass and index_html a method of it) /dir/subdir2/pics/logo.gif /dir/subdir2/index_html (subdir2 is a zclass and index_html a method of it) /dir/subdir3/pics/ (empty!) /dir/subdir3/index_html (subdir3 is a zclass and index_html a method of it)
The index_html contains a reference to the pic: <img src="pics/logo.gif"> Now when the subdirX/pics/ doesn't not contain a logo.gif, acquisition also does not work because it stops on finding the pics folder in the subdirX. This is exactly the scenario, Rik Hoekstra describes in http://www.zope.org/Members/Hoekstra/ChangingZopeContexts.
Unfortunately non of the workarounds he mentions are appropriate because a) I need an image folder for each subdir (see below) b) I can't reference images from the parent c) the pic directories need to have the same name
this all because the rationale behind this is to enable index_html to look differently _dependent_ on the existence of logo.gif. the site designers are meant to be able to change index_html's appearance just by uploading a new logo.gif. If this is not present, the one of the parent should be used.
I think it would work if I put the images directly in subdirX and in the root dir but this is pretty ugly and should only serve as a last ressort IMHO.
is there any way / product / trick to make this concept work with subdirs??
an explicit PythonScript (or DTML Method) should do the trick: 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 instead of the <img srcs="pics.logo.gif"), make your index_html refer to <dtml-var findlogo> this should do the trick. I tested it on a very simple setup, where it worked. Let me know if this helps. hth Rik
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 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 --Oliver
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
/pics/logo.gif /dir/subdir1/pics/logo.gif /dir/subdir1/index_html (subdir1 is a zclass and index_html a method of it) /dir/subdir2/pics/logo.gif /dir/subdir2/index_html (subdir2 is a zclass and index_html a method of it) /dir/subdir3/pics/ (empty!) /dir/subdir3/index_html (subdir3 is a zclass and index_html a method of it)
...
I think it would work if I put the images directly in subdirX and in the root dir but this is pretty ugly and should only serve as a last ressort IMHO.
is there any way / product / trick to make this concept work with subdirs??
as a sidenote there's a way I can produce the desired behaviour using a DTML construct: in index_html: <dtml-call "REQUEST.set('img', 'trans.gif')"> <dtml-if "_.hasattr(pics, img)"> <img src="pics/trans_gif" width="10" height="10"></td> <dtml-else> <img src="dir/pics/trans_gif" width="10" height="10"></td> </dtml-if> but this is ugly and pretty inconvenient to do for every image and also I am not sure if it works for every conceivable situation. In general I think my use case is something that can be very useful for many sites, so I think there should be a generic solution. --Oliver
On Mon, 5 Mar 2001, Oliver Frommel wrote:
<dtml-call "REQUEST.set('img', 'trans.gif')"> <dtml-if "_.hasattr(pics, img)"> <img src="pics/trans_gif" width="10" height="10"></td> <dtml-else> <img src="dir/pics/trans_gif" width="10" height="10"></td> </dtml-if>
but this is ugly and pretty inconvenient to do for every image
Then so not do this for every image - write one DTML method that takes imge name as a parameter, and call it for every image. Oleg. ---- Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
participants (3)
-
Oleg Broytmann -
Oliver Frommel -
Rik Hoekstra