flask admin - 移动删除按钮编辑视图

时间:2017-07-20 17:19:07

标签: python flask flask-admin

我是新的瓶子管理员,我需要将删除按钮移动到编辑视图。

这是我的其他视图继承的AdminModelView类。

class AdminModelView(sqla.ModelView):
    can_view_details = True


    # column_extra_row_actions = [ViewRowAction()]

    def is_accessible(self):
        if not current_user.is_active or not current_user.is_authenticated:
            return False
        if current_user.can('administrator'):
            return True
        return False

    def _handle_view(self, name, **kwargs):
        """
        Override builtin _handle_view in order to redirect users when a view is not accessible.
        """
        if not self.is_accessible():
            if current_user.is_authenticated:
                # permission denied
                abort(403)
            else:
                # login
                return redirect(url_for('auth.login', next=request.url))
            print(self._edit_form_class)


    def get_list_row_actions(self):
        """
            Return list of row action objects, each is instance of
            :class:`~flask_admin.model.template.BaseListRowAction`
        """
        actions = []

        if self.can_view_details:
            if self.details_modal:
                actions.append(template.ViewPopupRowAction())
            else:
                actions.append(template.ViewRowAction())

        return actions + (self.column_extra_row_actions or [])

我已重新定义get_list_row_actions以从列表视图中删除编辑和删除按钮。我想知道我的AdminModelView课程的一部分是否可以更改,或者我是否需要更改编辑表单的模板。

1 个答案:

答案 0 :(得分:2)

我的解决方案:

class AdminModelView(sqla.ModelView):

上写了新的终点
@expose('/delete', methods=('DELETE',))
def delete_view(self):
    """
        Delete model
    """
    id = request.args.get('id')
    if id is None:
        return jsonify({"success": False}), 404
    model = self.get_one(id)
    db.session.delete(model)
    db.session.commit()
    flash("{0} deleted".format(model))
    return jsonify({"success": True}), 200

使用这些宏为编辑视图模板呈现删除按钮和模态。

{% if admin_view.can_delete and model %}
  {{ add_modal_button(url='#', title='Delete', content='Delete', modal_window_id='delete_modal', btn_class='btn btn-danger') }}
{% endif %}

{% macro delete_modal() %}
<div class="modal fade" id="delete_modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
      {# bootstrap version > 3.1.0 required for this to work #}
    <div class="modal-content">
      <div class="panel panel-danger">
        <div class="panel-heading">Delete Record</div>
        <div class="panel-body">
          <p>You are about to delete this record. This action cannot be undone.</p>
          <p>Would you like to proceed?</p>
        </div>
        <div class="panel-footer text-center">
          <button type="button" class="btn btn-secondary" id="cancel_delete">Cancel</button>
          <button type="button" class="btn btn-danger" id="confirm_delete">Delete</button>
        </div>
      </div>
    </div>
  </div>
</div>
{% endmacro %}

Hybrid Jinja2和JavaScript。这应该重构。在发出AJAX请求之前,{{model.id}}可以存储在DOM中并使用jQuery检索。

$("#cancel_delete").click(function() {
  $("#delete_modal").modal("toggle")
});

$("#confirm_delete").click(function() {
  $.ajax({
    url: "../delete?id={{ model.id }}",
    method: "DELETE"
  }).done(function() {
    window.location.replace("{{ return_url }}");
  }).fail(function() {
    $(".actions-nav").before(
      '<div class="alert alert-danger alert-dismissable fade in">' +
      '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>' +
      '<strong>Error:</strong> This object failed to delete.' +
      '</div>'
    )
  })
})