On Monday, December 4, 2017 at 11:34:54 AM UTC-5, rafi farchi wrote:
>
> Hi
> Thanks , i assume it will apply on any ui Such as form grid etc . in this 
> case what message the user will receive on attempt
> To delete ? 
>

The before_delete callback operates at the level of the DAL (i.e., when the 
.delete() method is called on a DAL Set object), so by default, no message 
will be displayed in the UI. You will be responsible for checking for the 
failed delete in your code and then displaying the appropriate message to 
the user.

If you are using SQLFORM.grid, you can make use of the "deletable" and/or 
"ondelete" arguments. The former can be a callable that takes the record to 
be deleted and returns a falsey value if it should not be deleted (though I 
do not think any message gets displayed in the UI if the delete fails). The 
latter can be a callable that takes the DAL table and the id of the record 
to be deleted -- the only way to prevent the delete using this approach is 
for the ondelete function to do a redirect or raise an HTTP error, which 
will abandon the current request (you could set a flag in the session 
before the redirect and add logic to the view to display an error message 
based on that flag).
 

> The example in the book is very basic . can you please give an example of 
> a pre delete call back for example prohibit delete of order header if it 
> has order lines?


The before_delete callback takes a DAL Set object, such as db(db.mytable.id 
== some_id), so you can use that as you would any Set object to generate 
queries and retrieve records. To check whether any records in a set have 
any referencing records in another table, you could do something like:

def my_before_delete(dbset):
    return not dbset(db.mytable.id == db.othertable.mytable).isempty()

The above query does a join with the referencing table, and the function 
returns True if the join query is not empty (implying there are referencing 
records).

Anthony 

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to