【问题标题】:Trac. Adding a custom select field to a ticket跟踪。将自定义选择字段添加到工单
【发布时间】:2013-12-30 11:00:29
【问题描述】:

我正在尝试编写一个插件,该插件会将某个选择类型的自定义字段添加到工单中。与选择类型的常规自定义字段的区别在于,该字段将从数据库中获取其值并使用 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 配置文件提供任何选项。

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

【问题讨论】:

    标签: python trac genshi


    【解决方案1】:

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

    由于您已经修改了自定义字段的外观,因此可能值得尝试更改其输入字段类型。这个想法是假装它是一个简单的(文本)输入字段,它将接受任何值而无需验证,你明白吗?

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

    【讨论】:

      【解决方案2】:

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

          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 中执行数据库查询以从数据库中提取值并填充字典项:entry['options'] = self.env.db_query(...)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-10
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        相关资源
        最近更新 更多