Search code examples
pythonajaxweb2py

web2py: Prevent delete action on the A() helper depending on the result of the callback


Is it possible to prevent the delete action on 'tr' depending on the return value of service_record_del_request callback:

{{=A('Delete', callback=URL('service_record_del_request', vars={'id':r.id}), delete='tr')}}

Example callback:

@auth.requires_login()
def service_record_del_request():
    id = request.vars.id
    if db(db.service_record.id==id).delete(): #debug
        return 'true'  #--> ok, delete the 'tr'
    else:
        return 'false'  #--> db delete failed, don't delete 'tr'

Solution

  • The delete operation in the browser will only happen if the Ajax request is successful (i.e., the HTTP status is 200). So, to prevent the delete, you can return an HTTP status other than 200. For example:

    @auth.requires_login()
    def service_record_del_request():
        id = request.vars.id
        if not db(db.service_record.id==id).delete():
            raise HTTP(400)