Flask-Admin batch action with form

空扰寡人 提交于 2019-12-23 04:16:15

问题


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?


回答1:


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)   

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).

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 %}


来源:https://stackoverflow.com/questions/35852255/flask-admin-batch-action-with-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!