[web2py] Re: Conditional deletes (ondelete etc) in sqlform.grid
With your suggested changes, it looks like any time an ondelete is specified, the grid code would no longer actually do the delete. I don't think we want that. If your ondelete determines that no delete should take place, then it can do a redirect or raise HTTP() in order to abandon the delete. Anthony On Thursday, February 20, 2014 4:00:49 PM UTC-5, ksotiris wrote: > > >deletable = False > >if 'edit' in request.args: > >record = db[table](request.args[-1]) > >if some condition of record: > >deletable = True > >grid = SQLFORM.grid(db[table], deletable = deletable) > > > Plus to above > Idea: > We have smartgrid with "orders" link table "products_in_order" and we want > not delete the order if we have products_in_order ( ..if we are not using > > ON DELETE CASCADE ..) > How to prevent deletion ? In "edit" we can use the above solution, in > "view" > we can do the followings. > > PS.: Can add this to github for the sqlhtml.py? > > --- > > if in sqlhtml.py line 2110 up to 2122 > > -- > from > -- > if not callable(deletable): > if ondelete: > ondelete(table, request.args[-1]) > db(table[table._id.name] == request.args[-1]).delete() > else: > record = table(request.args[-1]) or redirect(URL('error')) > if deletable(record): > if ondelete: > ondelete(table, request.args[-1]) > record.delete_record() > -- > to > -- > if not callable(deletable): > if ondelete: > ondelete(table, request.args[-1]) > else: > db(table[table._id.name] == > request.args[-1]).delete() > > else: > record = table(request.args[-1]) or redirect(URL('error')) > if deletable(record): > if ondelete: > ondelete(table, request.args[-1]) > else: > record.delete_record() > > --- > > controler : > > def order_delete(table,record_id): > #count prodcuts_in order > records = db(db.products_in_order.orders_id==record_id).count() > if request.args[-3]=='delete': > if records>0: > session.flash=T('%s product(s) in order. Delete not > perimitted.' % records) > redirect(URL('default/orders/orders')) > elif records==0: > db(db.orders.id == record_id).delete() > db.commit() > pass > > def orders(): > deletable = True > if 'edit' in request.args: > record_id=request.args[-1] > records = db(db.orders_products.orders_id==record_id).count() > if records>0: > deletable = False > > grid=SQLFORM.smartgrid(db.orders,linked_tables=linked_tables, > fields=showFields,orderby=orderFields, deletable=dict(orders=deletable), > ondelete=order_delete) > > response.view='default/index.html' > return dict(grid=grid, form_name=T('Orders')) > > > > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: Conditional deletes (ondelete etc) in sqlform.grid
>deletable = False >if 'edit' in request.args: >record = db[table](request.args[-1]) >if some condition of record: >deletable = True >grid = SQLFORM.grid(db[table], deletable = deletable) Plus to above Idea: We have smartgrid with "orders" link table "products_in_order" and we want not delete the order if we have products_in_order ( ..if we are not using ON DELETE CASCADE ..) How to prevent deletion ? In "edit" we can use the above solution, in "view" we can do the followings. PS.: Can add this to github for the sqlhtml.py? --- if in sqlhtml.py line 2110 up to 2122 -- from -- if not callable(deletable): if ondelete: ondelete(table, request.args[-1]) db(table[table._id.name] == request.args[-1]).delete() else: record = table(request.args[-1]) or redirect(URL('error')) if deletable(record): if ondelete: ondelete(table, request.args[-1]) record.delete_record() -- to -- if not callable(deletable): if ondelete: ondelete(table, request.args[-1]) else: db(table[table._id.name] == request.args[-1]).delete() else: record = table(request.args[-1]) or redirect(URL('error')) if deletable(record): if ondelete: ondelete(table, request.args[-1]) else: record.delete_record() --- controler : def order_delete(table,record_id): #count prodcuts_in order records = db(db.products_in_order.orders_id==record_id).count() if request.args[-3]=='delete': if records>0: session.flash=T('%s product(s) in order. Delete not perimitted.' % records) redirect(URL('default/orders/orders')) elif records==0: db(db.orders.id == record_id).delete() db.commit() pass def orders(): deletable = True if 'edit' in request.args: record_id=request.args[-1] records = db(db.orders_products.orders_id==record_id).count() if records>0: deletable = False grid=SQLFORM.smartgrid(db.orders,linked_tables=linked_tables, fields=showFields,orderby=orderFields, deletable=dict(orders=deletable), ondelete=order_delete) response.view='default/index.html' return dict(grid=grid, form_name=T('Orders')) -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: Conditional deletes (ondelete etc) in sqlform.grid
deletable = False if 'edit' in request.args: record = db[table](request.args[-1]) if some condition of record: deletable = True grid = SQLFORM.grid(db[table], deletable = deletable) On Sunday, April 14, 2013 3:12:31 PM UTC-4, Dan wrote: > > Mark Kirkwood writes: > > > > > > > I've worked around this by tackling the issue a different way: > > - disable the delete button on the grid altogether with > > deletable=False > > - selectively enable it in edit mode for records that are > > safe/appropriate to delete > > How did you do that? I'm experiencing the same problem! > > -- --- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: Conditional deletes (ondelete etc) in sqlform.grid
Mark Kirkwood writes: > > > I've worked around this by tackling the issue a different way: > - disable the delete button on the grid altogether with > deletable=False > - selectively enable it in edit mode for records that are > safe/appropriate to delete How did you do that? I'm experiencing the same problem! -- --- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: Conditional deletes (ondelete etc) in sqlform.grid
I've worked around this by tackling the issue a different way: - disable the delete button on the grid altogether with deletable=False - selectively enable it in edit mode for records that are safe/appropriate to delete Maybe I should have done it this way to begin with - as it is nicer if the user only sees actions that are going to work. On 14/02/12 13:11, Mark Kirkwood wrote: I am attempting to implement a check that allows deletes only under some circumstances. I figured I would do this via the ondelete arg in the grid. Unfortunately I have run into a few stumbling blocks with this: 1/ The 1.99.4 code is busted - references 'ret' before it is defined Fortunately a simple fix (I think you guys have done something like this in trunk) - sqlhtml.py:1294 if ondelete: #ondelete(table,request.args[-1],ret) ondelete(table,request.args[-1]) 2/ The ondelete function is only called from the main grid delete action, not the delete checkbox in edit mode I would be great if the logic could be applied there too - otherwise I guess I can workaround by switching the delete option off in the edit screen. 3/ Am unclear about how to signal back that I want delete to stop I figure I am just being dense in this case, but ondelete function accepts table and row id variables (not a form) so I don't see any way to set the form's error status. Thanks for your help Mark