Edit or delete not committing in sqlform.grid for custom query

750 views
Skip to first unread message

rahulserver

unread,
Jun 12, 2012, 8:44:38 AM6/12/12
to web...@googlegroups.com
I have the following sqlform.grid:
COMMITMENTS=SQLFORM.grid((db.Commitments.Account==session.id),user_signature=False)

When I try to edit any row of the form, the edit is not committing. Is it that the sqlform.grid does not allow edits for query arguments?

Mandar Vaze

unread,
Sep 13, 2012, 2:16:09 PM9/13/12
to web...@googlegroups.com, rahul...@gmail.com
I'm using version 2.0.7 and seeing the same problem. 
I looked at the gluon/sqlhtml.py and it doesn't look like problem is specific to custom query (Although I too have custom query passed to the sqlform.grid)

     elif deletable and len(request.args)>2 and request.args[-3]=='delete':  
         table = db[request.args[-2]]                                        
         if ondelete:                                                        
             ondelete(table,request.args[-1])                                
         ret = db(table[table._id.name]==request.args[-1]).delete()          
         db.commit() # If I add this, the record is deleted from DB too
         return ret 

I confirmed this by adding "db.commit()" just prior to returning. Without this line, the record is removed from the table - but not form DB. So next time we refresh and re-render the table, the deleted record re-appears

BTW, I have another function for bulk_delete (User selects from checkbox, and deleted selected records in one go) I am not calling db.commit() there explicitly but the records are indeed deleted from DB.

What is going on ?

-Mandar
Message has been deleted

Mandar Vaze

unread,
Sep 13, 2012, 2:24:01 PM9/13/12
to web...@googlegroups.com, rahul...@gmail.com
For now, I'm calling db.commit on my own ondelete function 

def my_ondelete_function(table, record_id):                             
    print "Deleting %s from %s" % (record_id, table)                            
    db(table[table._id.name]==record_id).delete()                               
    db.commit()

so technically same delete() gets called twice - first in my ondelete function (listed above) and then immediately in gluon/sqlhtml.py
I know it is wrong/incorrect, but seems to be working for me.

Waiting for suggestions/comments from the group.

-Mandar

Gerd

unread,
Sep 30, 2012, 10:16:33 AM9/30/12
to web...@googlegroups.com, rahul...@gmail.com
Hi!

I'm actually using V2.0.9 and the error is still active. Is it fixed in 2.1.0?

Thanks,
Gerd

Gerd

unread,
Oct 6, 2012, 2:28:32 PM10/6/12
to web...@googlegroups.com, rahul...@gmail.com
Hi!

Any updates on this?

Derek

unread,
Oct 8, 2012, 8:35:08 PM10/8/12
to web...@googlegroups.com, rahul...@gmail.com

Mandar Vaze

unread,
Dec 19, 2012, 4:40:58 AM12/19/12
to web...@googlegroups.com, rahul...@gmail.com
Problem still persists in 2.3.2


On Tuesday, October 9, 2012 6:05:08 AM UTC+5:30, Derek wrote:
did you open an issue?


-Mandar 

Niphlod

unread,
Dec 19, 2012, 4:54:31 AM12/19/12
to web...@googlegroups.com, rahul...@gmail.com
I can't reproduce the issue. Can you post a packed application showing the problem ? Both edit and deleteare working fine for me, either with a custom query or a table name.

Mandar Vaze

unread,
Dec 19, 2012, 9:06:48 AM12/19/12
to web...@googlegroups.com


On Wednesday, December 19, 2012 3:24:31 PM UTC+5:30, Niphlod wrote:
I can't reproduce the issue.

I know it is a bit tough one. Even I too wasn't able to pinpoint the exact problem. I debugged till the dal.py as well. Not sure when does the db.commit() get called.
 
Can you post a packed application showing the problem ?

Can't. Anyway, I'm pasting some relevant code below. BTW, if it matters, I started the development with 2.0.x and upgrade just the web2py code to current stable release i.e. After I upgrade web2py I do not go by to my application and make any changes (like web2py.css or any other)

In a file under modules :

from gluon import current
import datetime

def modify_grid(grid, hide_adv_search=False,
        bootstrap_pagination
=False, rename_view_btn=False,
        non_web2py_table
=False):
    T
= current.T
   
if hide_adv_search:
       
# Remove Advanced Query Slideout
       
if len(grid.elements('input[id=web2py_keywords]')):
            grid
.elements('input[id=web2py_keywords]')[0]['_onfocus'] = ""


   
if bootstrap_pagination:
       
if len(grid.elements('.web2py_paginator')):
            grid
.elements('.web2py_paginator')[0]['_class'] = 'pagination'


   
if non_web2py_table:
       
if len(grid.elements('.web2py_table')):
            grid
.elements('.web2py_table')[0]['_class'] = \
                   
'table table-striped'


   
if rename_view_btn:
       
# Change from "View" to "Details"
       
# When using Buttons
       
for btn in grid.elements('span'):
           
if btn['_title'] == "View":
                btn
['_title'] = T('Details')
               
for y in btn.elements():
                    y
[0] = T("Details")


       
# When NOT using Buttons
       
for a in grid.elements('a[title=View]'):
            a
.elements()[0]['_title'] = T('Details')



Following are in same file under controllers:

def delete_record(table, record_id):
    record
= db(table[table._id.name] == record_id).select().first()
   
if 'name' in record:
       
print "User [%s] is Deleting [%s] from [%s]" % (auth.user.first_name,
                                                        record
['name'], table)
   
else:
       
print "User [%s] is Deleting [%s] from [%s]" % (auth.user.first_name,
                                                        record_id
, table)
    db
(table[table._id.name] == record_id).delete()
    db
.commit()

@auth.requires_login()
@auth.requires_membership('some_role')
def my_method():
    textlengths
= {'table.column1': 100, 'table.column2': 60,
               
'table.column2': 60}
    grid
= SQLFORM.grid(db.table, user_signature=False, csv=False,
           ondelete
=delete_record,
           maxtextlengths
=textlengths, showbuttontext=False)
    modify_grid
(grid, hide_adv_search=True, bootstrap_pagination=True,
                rename_view_btn
=True, non_web2py_table=True)

   
return dict(grid=grid)

I hope this helps. As I mentioned in the bug report, the table has "db.table.id.readble=False" in models. But that conclusively did not tell me anything. But might help you.

-Mandar

Niphlod

unread,
Dec 19, 2012, 10:02:54 AM12/19/12
to web...@googlegroups.com

Can't. Anyway, I'm pasting some relevant code below. BTW, if it matters, I started the development with 2.0.x and upgrade just the web2py code to current stable release i.e. After I upgrade web2py I do not go by to my application and make any changes (like web2py.css or any other)
 
 
Really not relevant all the modify_grid() part. That doesn't belong to web2py, which you are addressing the bug to.
Without the table model, all code is quite useless. We don't need your app. Just an app showing the problem.


As I mentioned in the bug report, the table has "db.table.id.readble=False" in models

Setting table.id.readable = False works with grid (that fetches the id from the arguments)

Mandar Vaze / मंदार वझे

unread,
Dec 19, 2012, 11:23:18 AM12/19/12
to web...@googlegroups.com
I know it is a tough one, but can you guess what "could" be the problem ? As I mentioned, "explicitly" adding db.commit() helps "resolve" the problem (Whether in gluon/sqlhtml.py or separately in my custom ondelete)

Does that give you any hints ? I debugged but (obviously) couldn't find the culprit.

Any pointers on how to troubleshoot/debug this ? I think at least one more person has similar problem (One that started this thread)
 
Really not relevant all the modify_grid() part. That doesn't belong to web2py, which you are addressing the bug to.

I know. But it was something different - so I thought it may be relevant.
 
Without the table model, all code is quite useless. We don't need your app. Just an app showing the problem.

Ahh, I see. Let me try to create that (app) 

-Mandar

BJ

unread,
Dec 26, 2012, 2:40:15 PM12/26/12
to web...@googlegroups.com
The problem is in sqlhtml.py

In latest version from line 2293-2297 is following content:

if deletable and (not callable(deletable) or deletable(row)):
                        row_buttons.append(gridbutton(
                            'buttondelete', 'Delete',
                            callback=url(args=['delete', tablename, id]),
                            delete='tr'))

but it should be:

if deletable and (not callable(deletable) or deletable(row)):
                        row_buttons.append(gridbutton(
                            'buttondelete', 'Delete',
                            buttonurl=url(args=['delete', tablename, id]), <--- Missing line to define URL for Delete button
                            callback=url(args=['delete', tablename, id]), <--- We need this for warning message (I believe so?)
                            delete='tr'))

But we also need to update gridbutton function (line 1806) because we need button URL and warning. I have tried to add "buttonurl" but now the record is deleted even if I select "Cancel".

I don't know how to fix this because I don't how to handle callback.

Boris


Dne sreda, 19. december 2012 17:23:18 UTC+1 je oseba Mandar Vaze napisala:

Massimo Di Pierro

unread,
Dec 26, 2012, 6:48:21 PM12/26/12
to web...@googlegroups.com
Can you please open a ticket about this?

Mandar Vaze / मंदार वझे

unread,
Dec 26, 2012, 11:41:26 PM12/26/12
to web...@googlegroups.com
On Thu, Dec 27, 2012 at 5:18 AM, Massimo Di Pierro <massimo....@gmail.com> wrote:
Can you please open a ticket about this?

There is already one opened for this : http://code.google.com/p/web2py/issues/detail?id=1234
Unless your email was meant for Boris and his buttonurl code change. (It it addresses same problem, so same issue can be used ?)

-Mandar 
 
--
 
 
 

Message has been deleted

Christian Espinoza

unread,
Jan 11, 2013, 8:24:46 AM1/11/13
to web...@googlegroups.com

Niphlod

unread,
Jan 11, 2013, 8:45:55 AM1/11/13
to web...@googlegroups.com
the problem has been fixed in trunk. The issue was kinda non-existant for javascript enabled browsers, because there was an onclick attribute on the delete button that called the delete url correctly.

Now there are both the full link to the delete url and the onclick attribute, so behaviour is consistent both in javascript-enabled and javascript-disabled browsers.

Christian Espinoza

unread,
Jan 11, 2013, 2:02:54 PM1/11/13
to web...@googlegroups.com
Thanks Niphlod, I get the trunk with this feature working done now!

Christian.
Reply all
Reply to author
Forward
0 new messages