Re: restrictedTraverse() and encapsulated image Codesource with VMH
Kit BLAKE <[email protected]> Thu, 16 Jul 2009 17:33:15 +0200
| Newsgroups | gmane.comp.web.zope.silva.devel |
|---|---|
| Message-ID | <[email protected]> |
On 16 Jul 2009, at 10:02, Heiko Besemann wrote:
> thanks for your info. I think that we can live with the workaround
> (adding
> the path) or simply put the pictures in the same or subfolder as
> mentioned
> by you. On the other hand I had the same idea, fixing the code source
> template and adding some additional program logic to find out if we
> are in
> a vhost tree or not.
>
> So for now I played around with something like this (path is the
> argument
> passed to the script which is the absolute path to test.jpg like
> /bilder/test.jpg):
>
> -- snip --
> strPath = ''
> lstObjstr = context.getPhysicalPath() # get complete path from cms
> root to
> object
> for strObj in lstObjstr:
> strTmpPath = '/'.join(lstObjstr) # create full absolute path (like
> /cms/watt/bilder/test.jpg)
> iPos = strTmpPath.find(strObj) # find position of my current
> context
> object
> strTmpPath = strTmpPath[:iPos] # now roll back from current
> object to
> cms root ...
> oObj = context.restrictedTraverse(strTmpPath)
> try:
> binding = context.service_metadata.getMetadata(oObj)
> boStatus = binding.get('silva-layout-vhost-root',
> element_id='vhostroot') #
> ... and check if we found a vhost meta data property
> if boStatus:
> strPath = '%s%s' % (strTmpPath, path) # if yes, return the path to
> vhost
> publication and add the relative path (/bilder/test.jpg)
> return strPath
> except:
> pass # not all objects do have meta data so ignore this exception
> return path # if no vhost meta data property was found return the
> relative
> path as it must be like the relative path
> -- snapp --
> The above code works when I execute it from the pages context (i.e.
> put
> that script into the folder object) and I am not using the same
> object ids
> twice (find would get confused, but should be fixed with a few more
> lines
> python).
>
> However when I put this into code source (my cs is located on an upper
> level behind vhost) I get an error, because
> context.getPyhsicalRoot() will
> get the path for the code source location and not the context of the
> silva
> page location. I think that I have to pass an additional argument
> from the
> zpt to the script telling it the page's context.
For the record/archive, we always use 'model' for the content object
location. It should be in the request. In a python script you can do:
model = context.REQUEST.model
Here's a different example. The following code is in the cs_multi_toc
code source (should be in your service_codesources) where we define
'model', not as the doc in which the code source is embedded, but an
object somewhere else in the site:
<tal:def define="
model python:context.restrictedTraverse(top.split('/'));
publictree nocall:model/get_public_tree;
...
Here we're getting some container's tree.
> So for now this is ok and I will switch DNS entries in a few days to
> get an
> old website running on new Silva cms ;-)
Let us know, always nice to see a new Silva site. I might even use it
someday if I travel up that way. :)
Kit
ps: A detail for the future: I notice you're using the silva-layout-
vhost-root metadata value. In the next generation Silva skins, that
metadata element won't exist anymore.
We don't need it because there's an adapter that can tell us all we
need to know. If you avoid using that particular Silva Layout metadata
element, your skin code will migrate easier.
--
Kit BLAKE · Infrae · http://infrae.com/ + 31 10 243 7051