[Zope-dev] Deleting objects by the users

Casey Duncan cduncan@kaivo.com
Wed, 21 Mar 2001 09:14:09 -0700


Menno Brandsen wrote:
> 
> Hello all, I have a question conserning deleting objects(images) by the user
> 
> The situation is this :
> 
> I have a page that lists the contents of the folder below, containing images
> of a site.
> In that list a want to let the user/client delete the image that he wants.
> 
> I've tried using this syntax :
> 
> <a href="#" onClick="<dtml-call
> "image_folder.manage_delObjects(ids=_['id'])">"> DELETE </a>
> 
> What happens now is that as soon as the page loads it inmediatly deletes the
> images.
> Can anybody tell me a sollution for this problem ?
> I would be very gratefull ! =)
> 
> Greetings,
> Menno Brandsen
> 
This is a question better served on the main Zope mailing list. That
said, here is the answer:

All DTML is executed on the server before the page goes to the client.
That is why your images get deleted by viewing the page. The onclick
attribute is only useful for executing client side scripts like
JavaScripts, etc. To execute server side code, you must make the user
request the method that deletes the images. This is done by sending the
server a page request. So, you need to create two objects in Zope to
accomplish this. One that displays the images and another that deletes
the requested image and redraws (by a redirect) the image display page.
Here's an example:

dtml method view_images:

    <dtml-in name="objectValues('Image')">
        <dtml-var name="sequence-item">
        <a href="&dtml-absolute_url;/delete_image">Delete this image</a>
    </dtml-in>

dtml method delete_image:

    <dtml-if expr="meta_type == 'Image'">
        <dtml-call expr="manage_delObjects(getId())">
        <dtml-call expr="RESPONSE.redirect(URL2 + '/view_images')">
    </dtml-if>

This code is generic enough so that it would work in any folder. You
could place these methods above the folder(s) containing the images for
greater generality.

It might seem a bit confusing how this works if you don't understand
acquisition. The view_images method creates a link to each image in the
folder you call it for. The link is to the image object itself with the
delete_image method appended. This tells Zope to call delete_image in
the context of the image the user selects.

manage_delObjects is not a method of the image, but the image acquires
it from the Folder that contains it. So delete_image passes the id of
the image (using getId()) to the manage_delObjects method of the folder
containing it. It then redirects back to the view_images method which
will be sans the deleted image.

hth
-- 
| Casey Duncan
| Kaivo, Inc.
| cduncan@kaivo.com
`------------------>