带表单的Flask-Admin批处理操作 [英] Flask-Admin batch action with form

查看:97
本文介绍了带表单的Flask-Admin批处理操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Flask-SQLAlchemy Flask-Admin Flask 应用程序.

I have a Flask application with Flask-SQLAlchemy and Flask-Admin.

我想执行一个批处理操作,但是要使用表格.例如,我想为属性表单设置相同的文本值,但是该值由用户输入.

I would like to perform a batch action, but with form. For example I would like to set a same text value to the form attributed, but this value is entered by the user.

我看到了有关批处理操作的文档,但是它仅提供预定义数据的示例.

I saw documentation for batch actions, but it has example only for predefined data.

是否可能,或者对此有一些解决方法?

Is it possible, or maybe there's some workaround for this?

推荐答案

我实现此目标的方法是在 @action 方法中进行内部POST.

The way I achieve this is to do an internal POST in the @action method.

class AddressView(AdminView):

    # ... ....

    @action('merge', 'Merge', 'Are you sure you want to merge selected addresses?')
    def action_merge(self, ids):

        if len(ids) < 2:
            flash("Two or more addresses need to be selected before merging")
            return

        return_url = request.referrer
        return redirect(url_for('mergeaddresses.index', return_url=return_url), code=307)   

然后定义两条路线,一条用于内部发布,然后一条从接收用户输入数据的表单(在以下情况下为 MergeAddressForm )接收提交的POST.

Then define two routes, one for the internal post and then one to receive the submit POST from the form that receives the user's input data (MergeAddressForm in the case below).

在下面的示例中,我碰巧正在使用Flask-Admin BaseView处理路由.请注意,如何检索flask-admin列表视图中原始已检查的ID,然后将其作为编码的逗号分隔列表隐藏字段存储在表单中,然后将 return_url 返回到flask-admin列表视图.

In the example below I happen to be using a Flask-Admin BaseView to handle the routes. Note how the original checked IDs in the flask-admin list view are retrieved and then stored in the form as an encoded comma delimited list hidden field and likewise the return_url back to the flask-admin list view.

class MergeAddressesView(BaseView):

    form_base_class = BaseForm

    def __init__(self, name=None, category=None,
                 endpoint=None, url=None,
                 template='admin/index.html',
                 menu_class_name=None,
                 menu_icon_type=None,
                 menu_icon_value=None):
        super(MergeAddressesView, self).__init__(name,
                                             category,
                                             endpoint,
                                             url or '/admin',
                                             'static',
                                             menu_class_name=menu_class_name,
                                             menu_icon_type=menu_icon_type,
                                             menu_icon_value=menu_icon_value)
        self._template = template

    def is_visible(self):
        return False

    @expose('/', methods=['POST'])
    def index(self):
        if request.method == 'POST':
            # get the original checked id's
            ids = request.form.getlist('rowid')

            merge_form = MergeAddressForm()
            merge_form.process()
            joined_ids = ','.join(ids)
            encoded = base64.b64encode(joined_ids)
            merge_form.ids.data = encoded
            _return_url = request.args['return_url']
            merge_form.return_url.data = _return_url
            self._template_args['form'] = merge_form
            self._template_args['cancel_url'] = _return_url
            return self.render(self._template)

    @expose('/merge', methods=['POST'])
    def merge(self):

        if request.method == 'POST':
            merge_form = MergeAddressForm(selection_choices=[])
            decoded = base64.b64decode(merge_form.ids.data)
            ids = decoded.split(',')
            # do the merging
            address_merger = AddressMerger(ids=ids, primary_id=merge_form.primary_address.data)
            address_merger.merge()
            # return to the original flask-admin list view
            return redirect(merge_form.return_url.data)

我的用户输入表单模板如下所示.请注意操作网址.

My template for the user input form looks something like below. Note the action url.

{% extends 'admin/base.html' %}
{% import "bootstrap/wtf.html" as wtf %}

{% block body %}
    <h3>{{ admin_view.name|title }}</h3>
    <form role="form" action="{{ url_for('mergeaddresses.merge') }}" method="post" name="form">

        {{ form.hidden_tag() }}

        {{ wtf.form_errors(form) }}

        {{ wtf.form_field(form.primary_address) }}

        <button type="submit" class="btn btn-primary">Merge</button>
        <a href="{{ cancel_url }}" class="btn btn-danger" role="button">Cancel</a>
    </form>
{% endblock %}

这篇关于带表单的Flask-Admin批处理操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆