Trac的。将自定义选择字段添加到故障单

时间:2013-12-30 11:00:29

标签: python trac genshi

我正在尝试编写一个插件,它会为故障单添加某个选择类型的自定义字段。与选择类型的常规自定义字段的不同之处在于,此字段将从数据库中获取其值,并使用optgroups创建选择。

我通过trac配置文件创建自定义选择字段,并使用Transformer

进行修改

代码如下:

        db = self.env.get_db_cnx()
        cursor = db.cursor()

        cursor.execute("SELECT name, a_id FROM a_group")
        groups = cursor.fetchall()

        cursor.execute("SELECT id, name FROM activities")
        activities = cursor.fetchall()

        for activity in activities:
            stream = stream | Transformer('.//select[@id="field-activity"]').append(tag.optgroup(label=activity[1], id="act-"+str(activity[0])))

            for group in groups:

                if int(group[1]) == activity[0]:

                    stream = stream | Transformer('.//optgroup[@id="act-' + str(activity[0]) + '"]').append(tag.option(group[0]))

问题是:当我试图保存新的票据时,我收到错误:

Warning: <field_name_goes_here> is not a valid value for the activity field.

这是因为当我通过trac自定义字段功能使用自定义字段时 - 我没有通过trac配置文件提供任何选项。

问题是 - 实现这种功能的最佳方式(如果有的话)是什么?

2 个答案:

答案 0 :(得分:0)

我想你会想要实施IRequestFilter。在post_process_request中,您可以修改options的{​​{1}}。例如,如果我想将 Component 选择更改为一组硬编码值:

select

因此,您应该能够在 def post_process_request(self, req, template, data, content_type): if data and 'fields' in data: for entry in data['fields']: if 'name' in entry and entry['name'] == 'component': entry['options'] = ['component1', 'component2', 'component3'] 中执行数据库查询以从数据库中提取值并填充字典项:post_process_request

您可能需要在自定义字段的配置文件中至少放置一个虚拟选项。

答案 1 :(得分:0)

错误来自trac.ticket.web_ui中的'_validate_ticket'。如源注释标题中所述,选项字段将“始终验证已知值”。

由于您已经破坏了自定义字段外观,因此尝试更改其输入字段类型也许值得。我们的想法是假装它是一个简单的(文本)输入字段,在没有验证的情况下接受任何值,你知道吗?

其他方法会涉及搞乱Trac核心代码,这不是一个好主意,特别是如果你计划在未来几年内遵循上游代码。