Does anyone know how to redirect ondelete to a page to ask for a password and then return to the SQLFORM.grid page?
In my code below (for non-manager and non-supervisor) you can see that I've configured ondelete to call the on_delete function. This works.
The on_delete function also executes the redirection to get_approval, and the get_approval code runs, but it returns to the grid page without showing the get_approval form page.
If I comment ondelete and uncomment the links and deletable everything works. The view get_approval.html exists.
@auth.requires(lambda: (auth.requires_login() and request.env.http_referer
and ('/client' in request.env.http_referer
or '/client/get_approval' in request.env.http_referer)))
def get_approval():
"""."""
rec_id = request.args[0]
rows_dic = {**general.get_members(db, SUPERVISOR_ROLE),
**general.get_members(db, MANAGER_ROLE)}
form = SQLFORM.factory(
Field('user_id', label=T('Supervisor/Manager'),
requires=IS_IN_SET(rows_dic, zero=T('Choose one...'))),
Field('password', 'password', label=T('Password'), requires=IS_NOT_EMPTY()),
buttons=[BUTTON(T('Submit'), _type='submit', _class='btn btn-primary')],
formstyle='table3cols',
)
if form.process(keepvalues=True).accepted:
# If passwords match it is approved.
if (db.auth_user.password.validate(form.vars.password)[0]
== db.auth_user(form.vars.user_id).password):
row = db.client[rec_id]
row.update_record(cancel_approved_by=form.vars.user_id,
canceled_by=session.auth.user.id, canceled_on=request.now,
is_active=False)
redirect(URL('index', user_signature=True))
else:
response.flash = T('Wrong password')
return dict(form=form)
@auth.requires_login()
def index():
"""."""
# DON'T uncomment without testing.
# session.forget(response) # Recommended in Efficiency tricks.
# Hidden fields in grid/view form.
db.client.id.readable = False
db.client.canceled_on.readable = False
db.client.canceled_by.readable = False
db.client.cancel_approved_by.readable = False
# Hidden fields in create/edit form.
db.client.canceled_on.writable = False
db.client.canceled_by.writable = False
db.client.cancel_approved_by.writable = False
# ondelete is used in the grid and on_validation/on_update are used
# in the edit form.
if auth.has_membership(SUPERVISOR_ROLE) or auth.has_membership(MANAGER_ROLE):
grid = SQLFORM.grid(db.client, csv=False, details=False,
# noconfirm=True, # Grid only.
ondelete=on_delete, # Grid only.
onvalidation=on_validation, # Form only.
represent_none='', # Grid/view form only.
)
else:
grid = SQLFORM.grid(
db.client, create=False, csv=False, # deletable=False,
details=False,
editable=False,
# links=[lambda row: A(
# SPAN(_class='icon trash icon-trash glyphicon glyphicon-trash') + ' '
# + SPAN(T('Delete'), _class='buttontext button', _title='Delete'),
# _href=URL('get_approval', args=[row.id], user_signature=True),
# _class='button btn btn-default btn-secondary')],
ondelete=on_delete, # Grid only.
represent_none='', # Grid/view form only.
)
return dict(grid=grid)
@auth.requires(lambda: (auth.requires_login() and request.env.http_referer
and '/client' in request.env.http_referer))
def on_delete(table, rec_id):
"""Used in the grid."""
if auth.has_membership(SUPERVISOR_ROLE) or auth.has_membership(MANAGER_ROLE):
row = table[rec_id]
row.update_record(cancel_approved_by=session.auth.user.id,
canceled_by=session.auth.user.id, canceled_on=request.now,
is_active=False)
redirect(URL(user_signature=True))
else:
redirect(URL('get_approval', args=[rec_id], user_signature=True))
Thanks in advance,
JM
The redirect command must be used with client_side=True.