如何将ondelete函数重定向到页面以询问密码然后返回到SQLFORM.grid页面?

问题描述 投票:0回答:1

有没有人知道如何将ondelete重定向到页面以询问密码然后返回到SQLFORM.grid页面?

在下面的代码中(对于非经理和非主管),您可以看到我已配置ondelete来调用on_delete函数。这有效。

on_delete函数还执行重定向到get_approval,并运行get_approval代码,但它返回到网格页面而不显示get_approval表单页面。

如果我评论删除并取消注释链接和删除一切正常。视图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

web2py
1个回答
0
投票

redirect命令必须与client_side = True一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.