如何将ondelete函数重定向到页面以要求输入密码,然后返回到SQLFORM.grid页面?

时间:2019-03-03 17:59:37

标签: web2py

有人知道如何将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))

预先感谢

JM

1 个答案:

答案 0 :(得分:0)

重定向命令必须与client_side = True一起使用。