[Zope] adding properties trough pythonscript

Dieter Maurer dieter at handshake.de
Fri Oct 15 13:32:57 EDT 2004


rv000299 wrote at 2004-10-15 12:24 +0200:
> ...
>  ov=map(lambda x:(x.id,x), container.objectValues())

You get the same effect with:

    ov = container.objectItems()

>  ov.sort()
>  for (oid,obj) in ov:
>    if not (hasattr(obj,"isPortalContent") or obj.isPrincipiaFolderish):

Be aware of acquisition. Both "hasattr" and attribute lookup
may acquire and thus do not what you except.

Use: "obj.aq_inner.aq_explicit" instead of "obj" at places
where you want to avoid acquisition.


> ...
>       try:
>         context.manage_addProperty('prop', 0, 'boolean')
>         print "prop added<br>"
>       except:
>         print "adding prop to",str(obj.id),"failed<br>"

Do not use "try: ... except:...". It is dangerous. It can easily
lead to ZODB inconsistencies...
> ...
>The problem is not getting the property added, it does, but editing it
>afterwards in the ZMI is impossible. When I hit the Properties tab of the
>item, the newly added property is not on the page. However when I call the
>prop with a dtml-var it is rendered.

You add the property to "context" but, probably, you wanted it
to get added to "obj"?

-- 
Dieter


More information about the Zope mailing list