web2py - 如何将 ondelete 函数重定向到页面要求输入密码,然后返回 SQLFORM.grid 页面?
问题描述
有谁知道如何将 ondelete 重定向到页面以要求输入密码,然后返回 SQLFORM.grid 页面?
在下面的代码中(对于非经理和非主管),您可以看到我已将 ondelete 配置为调用 on_delete 函数。这行得通。
on_delete 函数也执行重定向到 get_approval,get_approval 代码运行,但它返回到网格页面,没有显示 get_approval 表单页面。
如果我评论 ondelete 并取消评论链接和可删除一切正常。视图 get_approval.html 存在。
@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))
提前致谢,
杰米
解决方案
重定向命令必须与 client_side=True 一起使用。
推荐阅读
- python - 条件 if 语句 GEKKO
- java - Java GUI Swing 文本字段
- wordpress - 在 WooCommerce 中批量更改运输类别
- angular - 将组件添加到角度 11 时出现 InvalidInputOptions 错误
- html - 带有columns-*的css flex项目拉伸太多
- mysql - 创建多列索引,包括相关表中的列
- swift - 无法从 ORSSerialPort 接收数据
- javascript - 如何存储变量的原始形式?
- javascript - 如何捕获并报告“由于其 MIME 类型(...)和严格的 MIME 类型(...)而拒绝执行脚本”问题?
- ios - UICollectionView:旋转设备时保持单元格可见