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

时间:2017-11-21 15:14:36

标签: python trac

前提:我的票证有一个自定义字段recipe,其中应包含指向以票证ID命名的Wiki页面的链接。 例如,#1票证的recipe设置为[wiki:TicketRecipe_1],票号#1234应为[wiki:TicketRecipe_1234],等等。

由于我希望在创建/修改故障单时自动填充该链接,因此我根据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]。 也许是因为当调用入口点时,票证ID /号码仍然是未知的?

是否可以在创建故障单时将值设置为

1 个答案:

答案 0 :(得分:0)

您可以实施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