Trac 插件 - 在创建工单时使用工单 ID 设置字段值

Trac plugin - set field value using ticket id at ticket creation

前提:我的票有一个自定义字段 recipe,它应该包含一个 link 到以票 ID 命名的 wiki 页面。 例如,工单 #1 应将 recipe 设置为 [wiki:TicketRecipe_1],工单 #1234 应设置 [wiki:TicketRecipe_1234],依此类推。

因为我希望 link 在票证 creation/modify 上自动填充,所以我基于 ITicketManipulator 入口点构建了一个非常简单的插件。

from trac.core import Component, implements
from trac.ticket.api import ITicketManipulator


class WikiLinkPopulator(Component):
    implements(ITicketManipulator)

    def prepare_ticket(self, req, ticket, fields, actions):
        pass

    def validate_ticket(self, req, ticket):

        wikilink = "[wiki:TicketRecipe_%s]" % (ticket.id)
        if ticket['recipe_link'] != wikilink:
            ticket['recipe_link'] = wikilink

        return []

这在我 修改 现有工单时有效,但当我创建新工单时,结果是 [wiki:TicketRecipe_None]。 可能是调用入口点的时候ticketid/number还是未知数?

有没有办法在创建工单时也设置值

您可以实施 ITicketChangeListener。以下插件应与 Trac 1.0+ 一起使用:

from trac.core import Component, implements
from trac.ticket.api import ITicketChangeListener

class WikiLinkPopulator(Component):
    implements(ITicketChangeListener)

    def ticket_created(self, ticket):
        wikilink = "[wiki:TicketRecipe_%s]" % (ticket.id)
        self.env.db_transaction("""
            UPDATE ticket_custom SET value=%s WHERE name=%s AND ticket=%s
            """, (wikilink, 'recipe_link', ticket.id))

    def ticket_changed(self, ticket, comment, author, old_values):
        pass

    def ticket_deleted(self, ticket):
        pass

    def ticket_comment_modified(self, ticket, cdate, author, comment, old_comment):
        pass

    def ticket_change_deleted(self, ticket, cdate, changes):
        pass

    def prepare_ticket(self, req, ticket, fields, actions):
        pass