Hi, I'm not sure if the subject line accurately describes what I'm trying to do, anyway let me try and explain. I have a set of folders like this: Site root object |---- Attributes object |---- Layout ZPT |---- get_attobject (python script) |---- get_attributes (python script) |---- Folder object A |-----Attributes object |---- Folder object B My 'get_attobject' script is called from my 'Layout zpt' using this code: <div tal:define="attobject here/get_attobject"></div> And my get_attobject script contains the following code: attobjects = context.objectValues('Attributes object') if attobjects: return attobjects[0] return None Then my get_attributes script uses the attobjects value returned from the get_attobject script to output the values of the 'Attributes object'. Now this all works fine in the 'Site root object' and in 'Folder object A' but when I get to 'Folder object B' there is nothing returned since I have no 'Attributes object' present. But what I'd like to do in this situation is use acquisition to retrieve the values from the parent 'Attributes object' located in the site root. So could someone please explain how I could achieve this? Hope someone can help me, many thanks. If any more information is required please ask. Jon
On Fri, Jul 22, 2005 at 04:10:03PM +0100, Jon Bowlas wrote:
And my get_attobject script contains the following code:
attobjects = context.objectValues('Attributes object')
if attobjects:
return attobjects[0]
return None
(snip)
But what I'd like to do in this situation is use acquisition to retrieve the values from the parent 'Attributes object' located in the site root. So could someone please explain how I could achieve this?
As you've discovered, objectValues() does not use acquisition, it only looks for real sub-objects of the folder you call it on. Same is true for objectItems() and objectIds(). This kind of thing is typically done by always using the same id for your "attributes object". Then just acquire that id. In a sense, the id becomes part of your API. Your script would become: attrobject = getattr(context, 'some_id', None) return attrobject -- Paul Winkler http://www.slinkp.com
Many thanks for your help, worked a treat. Also thanks going out to Malcolm, although he got beat to the solution by Paul. -----Original Message----- From: zope-bounces@zope.org [mailto:zope-bounces@zope.org] On Behalf Of Paul Winkler Sent: 22 July 2005 16:21 To: zope@zope.org Subject: Re: [Zope] object acquisition via python scripts On Fri, Jul 22, 2005 at 04:10:03PM +0100, Jon Bowlas wrote:
And my get_attobject script contains the following code:
attobjects = context.objectValues('Attributes object')
if attobjects:
return attobjects[0]
return None
(snip)
But what I'd like to do in this situation is use acquisition to retrieve the values from the parent 'Attributes object' located in the site root. So could someone please explain how I could achieve this?
As you've discovered, objectValues() does not use acquisition, it only looks for real sub-objects of the folder you call it on. Same is true for objectItems() and objectIds(). This kind of thing is typically done by always using the same id for your "attributes object". Then just acquire that id. In a sense, the id becomes part of your API. Your script would become: attrobject = getattr(context, 'some_id', None) return attrobject -- Paul Winkler http://www.slinkp.com _______________________________________________ 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 )
On Fri, 22 Jul 2005 16:10:03 +0100, Jon Bowlas wrote:
attobjects = context.objectValues('Attributes object')
...
But what I'd like to do in this situation is use acquisition to retrieve the values from the parent 'Attributes object' located in the site root. So could someone please explain how I could achieve this?
Looking up your attributes object through objectValues will prevent acquisition - rather than just asking the object to find an attributes object for you, you're asking it for any attributes object amongst its children only. To search for your attributes object with acquisition, you could do: getattr(context, 'Attributes object') If the id of your attributes object did not contain a space (or anything else illegal in a python identifier), for example AttributesObject, you could just do: context.AttributesObject Thanks, Malcolm. -- [] j a m k i t web solutions for charities malcolm cleaton T: 020 7549 0520 F: 020 7490 1152 M: 07986 563852 W: www.jamkit.com
participants (3)
-
Jon Bowlas -
Malcolm Cleaton -
Paul Winkler