有条件地允许在列表视图中进行Flask管理员ModelView的编辑 [英] Conditionally allow editing in the list view for Flask admin ModelView

查看:58
本文介绍了有条件地允许在列表视图中进行Flask管理员ModelView的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在视图类中拥有这样的属性:

I can have a property like so in the view class:

class MyExampleView(ModelView):

    @property
    def can_edit(self):
        # Return True or False based on some conditional logic

如何访问行的属性,以便例如可以有条件地在表中显示编辑列,也许如果行的 is_active 属性为True或False.

How can I access the row's attribute so I can for example conditionally show the edit column in the table, maybe if the row's is_active property is True or False.

推荐答案

您可以使用一些模板操作增强功能来实现此功能,并在其中添加一种检查行可用性的方法:

You can make it with some template actions enhancements where you add a method to check row availability:

from flask_admin.model import template

class AccessMixin:
    def has_access(self, row):
        raise NotImplementedError()

class ViewRowAction(template.ViewRowAction, AccessMixin):
    def has_access(self, row):
        return True

class EditRowAction(template.EditRowAction, AccessMixin):
    def has_access(self, row):
        return row.is_active

class DeleteRowAction(template.DeleteRowAction, AccessMixin):
    def has_access(self, row):
        return row.is_active

然后,您需要覆盖 list.html 模板中的 list_row_actions 块以使用此新方法:

Then you need to override list_row_actions block in list.html template to use this new method:

{% extends 'admin/model/list.html' %}

{% block list_row_actions scoped %}
  {% for action in list_row_actions %}
    {% if action.has_access(row) %}
      {{ action.render_ctx(get_pk_value(row), row) }}
    {% endif %}
  {% endfor %}
{% endblock %}

然后,您需要模型类来使用覆盖的行操作和列表模板:

And then you need your model class to use the overridden row actions and list template:

from flask_admin.contrib.sqla.view import ModelView

class MyExampleView(ModelView):
    list_template = 'app_list.html'

    def get_list_row_actions(self):
        return (
            ViewRowAction(),
            EditRowAction(),
            DeleteRowAction(),
        )

请注意,原始的 get_list_row_actions 方法使用的是 can_view_details can_edit can_delete details_modal edit_modal 属性来定义已使用操作的列表.您可以通过重写此逻辑来放弃它.

Note that original get_list_row_actions method uses can_view_details, can_edit, can_delete, details_modal and edit_modal attributes to define the list of used actions. You discard this logic by overriding it.

这也不会阻止用户实际编辑或删除对象(例如,通过在浏览器中手动键入编辑视图URL).您需要在视图方法中实现访问权限检查,例如:

Also it does not prevent user from actually editing or deleting objects (e.g. by manually typing the edit view URL in the browser). You need to implement access rights checking in the views methods for example:

from flask import flash, redirect, request
from flask_admin import expose
from flask_admin.babel import gettext
from flask_admin.helpers import get_redirect_target
from flask_admin.model.helpers import get_mdict_item_or_list

class MyExampleView(ModelView):
    @expose('/edit/', methods=('GET', 'POST'))
    def edit_view(self):
        """This code was copied from the
           flask_admin.model.base.BaseModelView.edit_view method"""
        return_url = get_redirect_target() or self.get_url('.index_view')

        id = get_mdict_item_or_list(request.args, 'id')
        if id is None:
            return redirect(return_url)

        model = self.get_one(id)

        if model is None or not model.is_active:
            flash(gettext('Record does not exist or you have 
                          not enough access rights.'), 'error')
            return redirect(return_url)

        return super(MyExampleView, self).edit_view()

这篇关于有条件地允许在列表视图中进行Flask管理员ModelView的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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