flask admin - 将删除按钮移动到编辑视图
flask admin - move delete button to edit view
我是 flask admin 的新手,我需要将删除按钮移到编辑视图。
这是我的其他视图继承自的 AdminModelView class。
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
class 是否有我可以更改的部分,或者我是否需要更改编辑表单的模板。
我的解决方案:
在 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 %}
混合 Jinja2 和 JavaScript。这应该重构。 {{model.id}}
可以存储在 DOM 中,并在发出 AJAX 请求之前用 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">×</a>' +
'<strong>Error:</strong> This object failed to delete.' +
'</div>'
)
})
})
我是 flask admin 的新手,我需要将删除按钮移到编辑视图。
这是我的其他视图继承自的 AdminModelView class。
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
class 是否有我可以更改的部分,或者我是否需要更改编辑表单的模板。
我的解决方案:
在 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 %}
混合 Jinja2 和 JavaScript。这应该重构。 {{model.id}}
可以存储在 DOM 中,并在发出 AJAX 请求之前用 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">×</a>' +
'<strong>Error:</strong> This object failed to delete.' +
'</div>'
)
})
})