gpt4 book ai didi

python - flask-admin:如何根据其他列的值将列设置为只读?

转载 作者:太空狗 更新时间:2023-10-30 00:53:52 28 4
gpt4 key购买 nike

我建立了一个系统,允许用户申请代码审查并等待经理批准。

现在我想要实现的是如下:

  1. 如果它被批准enter image description here然后所有字段都变为只读(我在这里手动将项目名称设置为只读):

enter image description here

  1. 如果它被拒绝

    enter image description here

    然后所有字段都变得可编辑。当然,在创建新项目时,所有字段都应该是可编辑的。
    enter image description here

    ProjectProjectView的代码如下:

     from flask_sqlalchemy import SQLAlchemy
    from flask_admin.contrib import sqla
    from flask_security import current_user

    # Create Flask application
    app = Flask(__name__)
    app.config.from_pyfile('config.py')
    db = SQLAlchemy(app)

    class Project(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    project_name = db.Column(db.Unicode(128))
    version = db.Column(db.Unicode(128))
    SVN = db.Column(db.UnicodeText)
    approve = db.Column(db.Boolean())

    def __unicode__(self):
    return self.name

    class ProjectView(sqla.ModelView):
    def is_accessible(self):
    if not current_user.is_active or not current_user.is_authenticated:
    return False
    return False

    @property
    def _form_edit_rules(self):
    return rules.RuleSet(self, self.form_rules)

    @_form_edit_rules.setter
    def _form_edit_rules(self, value):
    pass

    @property
    def _form_create_rules(self):
    return rules.RuleSet(self, self.form_rules)

    @_form_create_rules.setter
    def _form_create_rules(self, value):
    pass

    @property
    def form_rules(self):
    form_rules = [
    rules.Field('project_name'),
    rules.Field('version'),
    rules.Field('SVN'),
    ]
    if not has_app_context() or current_user.has_role('superuser'):
    form_rules.append('approve')

    在我看来,由于 approve 是一个 boolean 变量,所以应该有一个条件判断判断是 0 还是 1,然后该字段相应变为只读或可编辑。

    感谢您提前提出任何建议。

最佳答案

正如您已经注意到的,为字段设置 readonly 属性是 rather simple但使其动态化有点棘手。

首先你需要一个自定义字段类:

from wtforms.fields import StringField

class ReadOnlyStringField(StringField):
@staticmethod
def readonly_condition():
# Dummy readonly condition
return False

def __call__(self, *args, **kwargs):
# Adding `readonly` property to `input` field
if self.readonly_condition():
kwargs.setdefault('readonly', True)
return super(ReadOnlyStringField, self).__call__(*args, **kwargs)

def populate_obj(self, obj, name):
# Preventing application from updating field value
# (user can modify web page and update the field)
if not self.readonly_condition():
super(ReadOnlyStringField, self).populate_obj(obj, name)

设置form_overrides您的 View 的属性:

class ProjectView(sqla.ModelView):
form_overrides = {
'project_name': ReadOnlyStringField
}

您需要将自定义 readonly_condition 函数传递给 ReadOnlyStringField 实例。我发现最简单的方法是覆盖 edit_form方法:

class ProjectView(sqla.ModelView):
def edit_form(self, obj=None):
def readonly_condition():
if obj is None:
return False
return obj.approve
form = super(ProjectView, self).edit_form(obj)
form.project_name.readonly_condition = readonly_condition
return form

编码愉快!

关于python - flask-admin:如何根据其他列的值将列设置为只读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222741/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com