将项目添加到 Trac 中的可用报告(通过插件)

Adding item to Available Reports in Trac (via Plugin)

是否可以通过插件将工单报告添加到可用报告中,以便在安装插件后自动可用?还是必须手动保存包含预期列和过滤器的自定义查询?

如果可以通过 python 代码实现,要实现的 Trac 接口是什么?

您可以通过实施 IEnvironmentSetupParticipant. Example usage in env.py. In env.py the reports are inserted in environment_created. You'll want to insert reports in upgrade_environment. Example code for inserting the report can be found in report.py. needs_upgrade determines whether upgrade_environment is called on Environment startup so you'll need to provide the logic that determines whether your reports are already present, returning False if they are present and True if they are not. If True is returned, then upgrade_environment will get called (for your case environment_created can just be pass, or it can directly call upgrade_environment, the latter being a minor optimization discussed in #8172). See the Database Schema 插入报告以获取有关 report table 的信息。

在 Trac 1.2(尚未发布)中,我们试图通过向 DatabaseManager class. DatabaseManager for Trac 1.1.6 includes methods set_database_version and get_database_version. This is useful for reducing the amount of code needed in IEnvironmentSetupParticipant.needs_upgrade when checking whether the database tables need to be upgraded (even simpler would be to just call DatabaseManager.needs_upgrade) 添加方法来简化使用数据库的操作。还有一个 insert_into_tables 方法可用于插入报告。

也就是说,我不确定您是否需要使用 set_database_versionsystem table 中为您的插件添加一个条目。您可能只查询 report table 并检查您的报告是否存在,使用该检查 return 来自 IEnvironmentSetupParticipant.needs_upgrade 的布尔值,这将确定是否 IEnvironmentSetupParticipant.upgrade_environment 被调用。如果您正在为 Trac 1.0.x 开发,您可以从 Trac 1.1.6 中的 DatabaseManager class 复制代码。 CodeReviewerPlugin, for which I made a compat.py 模块可以看到另一种方法,它添加了我需要的方法。 compat.py 方法的优点是可以从 DatabaseManager class 复制方法,而不会污染代码库的主要模块。将来当您的插件不再支持 Trac < 1.2 时,您只需删除 compat.py 模块并修改插件代码中的导入,而不必更改任何主要插件逻辑。