[Zope] Programmatically deleting objects

Horak, Karl KEHORAK at sandia.gov
Thu Mar 25 15:27:33 EST 2004


Lennart,

Thanks for the breakthrough.  Your solution needed only the following
trivial rewrite in order to work:  
<A HREF="<dtml-var expr="aq_parent.absolute_url()">/goDelete?item=<dtml-var
id>">

A call to the script with context.manage_delObjects(item) worked beautifully
and now I'm just adding some fluff to display a confirmation msg and a link
back to the original tree.  

Chris, thanks for all your input on this problem.  This may not be the most
elegant solution (and its not ZPT anyway) but I think I can close the file
on this particular gottcha.  The final dtml method that I put together might
be of use to other newbies who want to manage objects with a tree-view:

<table width="67%">
<tr>
<dtml-tree branches="publicObjects" skip_unauthorized="1"
sort="title_or_id">
<td width="49%">
<dtml-if expr="meta_type == 'Folder'"><B></dtml-if>
<IMG SRC="<dtml-var icon>"> <dtml-var title_or_id>
<dtml-if expr="meta_type == 'Folder'"></B></dtml-if>
</td>
<dtml-if expr="meta_type != 'Folder'">
<td width="17%">
<a href="<dtml-var tree-item-url>/manage_propertiesForm">
Change properties
</a></td>

<td width="17%">
<A HREF="<dtml-var expr="aq_parent.absolute_url()">/goDelete?item=<dtml-var
id>">
Delete item</A>
</td>
</dtml-if>
</dtml-tree>
</tr>
</table>

The only other piece is a scrit that is called when building the tree.  The
tree branches are generated by the following Python script only slightly
modified from the Zope.org archives:

"""
Script (Python)
ID:  publicObjects
Returns sub-folders and DTML documents 
"""
results=[]
for object in context.objectValues(['Folder', 'File']):
     if object.hasProperty('title') and object.title:
          results.append(object)
return results

For completeness sake here is goDelete:

"""
Script (Python)
ID:  goDelete(item)
Deletes object
"""
context.manage_delObjects(item)
print '<B>' + item + '</B> deleted.'
return printed

-----Original Message-----
From: Lennart Regebro [mailto:regebro at nuxeo.com] 
Sent: Wednesday, March 24, 2004 4:34 PM
To: Horak, Karl; zope at zope.org
Subject: Re: [Zope] Programmatically deleting objects


From: "Horak, Karl" <KEHORAK at sandia.gov>
> <A HREF="goDelete?item=<dtml-var id>&delPath=<dtml-var
> expr="aq_parent.aq_parent.id">.<dtml-var expr="aq_parent.id">">
>
> <dtml-call expr="REQUEST.delPath.manage_delObjects([REQUEST.item])">
> (This generates an AttributeError stating 'str' object has no attribute
> 'manage_delObjects')

Yes, delPath is a request variable, and therefore a string.

It seems to me that you want to delete the item that you are viewing, there
"here" object, so to speak. All the information you need to identify which
object that is, is the path. You can probably pass <dtml-var absolute_url>
as the item. That should be enough. Possibly you need
"absolute_url(relative=1)", I'm not sure.

> <dtml-call
> expr="_.getitem(REQUEST.delPath).manage_delObjects([REQUEST.item])">

I don't seem why you call an DTML method to run python code. Use a python
script instead.

In the pythonscript youshould be able to use restricted traverse to find the
object item, get it's aq_parent, and call manage_delObjects on it. However,
even this is total overkill. :-)

<A HREF="<dtml-var
expr="aq_parent.absolute_url()+'myDeleteScript?item='+id">"> should give you
a link pointing to
http://foo.com/the/parent/object/myDeleteScript?item=objectid
The myDeleteScript then simply calls context.manage_delObjects(item)

That should do it.




Do you really want to use DTML?






More information about the Zope mailing list