由 Trac 动态创建的文件的下载链接 - Wikimacro
我接到的任务是为 Trac 编写一个插件。 它应该提供问题跟踪系统中提交的票数和估计的燃尽数据。 用户将其请求写为 wikimacro,并提供一个链接/按钮,用于将燃尽图下载为 csv 文件,还计划输出为图表,但优先级较低。
我有一个处理数据的可行解决方案,但仍然存在以下问题。
我的问题
如何在维基页面上为根据用户请求动态创建的文件提供下载链接/- 按钮?
我见过一些在 trac 源本身和其他插件中发送文件的尝试,但由于我是网络编程新手,这并没有真正的帮助。
更新1 我一直在尝试按照菲利克斯建议的方式解决问题,这给我带来了一个新问题。 这个(愚蠢的)例子应该说明我的问题。 我的宏生成以下 URL 并将其添加为维基页面的链接。
//http://servername.com/projectname/wiki/page_name?teddy=bear
但即使条件返回 true,RequestHandler 也不会做出反应。 编辑:这段代码现在显示了示例的工作版本。
新网址:
#example url
#http://127.0.0.1:8000/prove/files/new
class CustomRequestHandlerModule(Component):
implements(IRequestHandler)
def match_request(self,req):
#old, not working
#return "teddy=bear"== str(req.path_info).split('?')[1]
#new
accept="/files/new"== str(req.path_info)
return accept
def process_request(self,req):
csvfile = self.create_csv()
req.send_response(200)
req.send_header('Content-Type', 'text/csv')
req.send_header('Content-length', len(csvfile))
req.send_header('Content-Disposition','filename=lala.csv')
req.end_headers()
req.write(csvfile)
raise RequestDone
更新2 插入日志语句显示 match_request 永远不会被调用。
我做错了什么? (是的,create_csv() 已经存在)
更新 3 谢谢,帮忙=)
I've been given the task to write a plugin for Trac.
It should provide burndown data for the ticketcount and estimations filed in the issue tracking system.
The user writes his request as a wikimacro and is provided a link/ button for downloading the burndown as a csv-file, output as a chart is also planned, but has lower priority.
I've got a working solution for processing the data but I'm left with the following problem.
My Question
How can I provide a downloadlink/- button on the Wikipage for a file which is dynamically created by the users request?
I've seen some attempts to send files in the trac source itself and other plugins, but since I'm new to web programming that doesn't really help.
Update1
I've been trying to solve the problem the way Felix suggested, which opened up a new problem for me.
This (stupid) example should demonstrate my problem.
My Macro generates the following URL and adds it as a link to the wikipage.
//http://servername.com/projectname/wiki/page_name?teddy=bear
But the RequestHandler doesn't react, even if the condition returns true.
Edit: This piece of code now shows the working version for the example.
New URL:
#example url
#http://127.0.0.1:8000/prove/files/new
class CustomRequestHandlerModule(Component):
implements(IRequestHandler)
def match_request(self,req):
#old, not working
#return "teddy=bear"== str(req.path_info).split('?')[1]
#new
accept="/files/new"== str(req.path_info)
return accept
def process_request(self,req):
csvfile = self.create_csv()
req.send_response(200)
req.send_header('Content-Type', 'text/csv')
req.send_header('Content-length', len(csvfile))
req.send_header('Content-Disposition','filename=lala.csv')
req.end_headers()
req.write(csvfile)
raise RequestDone
Update2
Inserting loggingstatements shows match_request never gets called.
What am I doing wrong? (Yes, the create_csv() exists already)
Update 3 Thx, for helping =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
match_request
没有被调用,那么process_request
永远没有机会执行。假设您的插件没有任何问题导致 Trac 无法正确加载它,则可能发生的情况是在您的match_request
版本被调用之前另一个处理程序正在匹配该 URL。尝试将日志级别提高到“调试”,看看它是否提供了足够的信息来告诉谁正在处理该请求。另一种选择是为自动生成的文件创建自定义“命名空间”。尝试将生成的 URL 中的“wiki”替换为“文件”之类的内容。这应该可以防止任何内置处理程序在调用插件的
match_request
方法之前处理请求。If
match_request
isn't getting called, thenprocess_request
never has a chance to execute. Assuming that there's nothing wrong with your plugin that's preventing Trac from loading it correctly, what's probably happening is that another handler is matching the URL before your version ofmatch_request
gets called. Try increasing your log level to "Debug" and see if it provides enough information to tell who is processing that request.Another option is to create a custom "namespace" for your auto-generated files. Try replacing 'wiki' in the generated URLs with something like 'files'. This should prevent any of the built-in handlers from handling the request before your plugin's
match_request
method gets called.基本上,您需要编写自己的 IRequestHandler 来处理特定的 URL 并返回动态创建的数据。之后,您的宏应该返回一个为您的请求处理程序配置的 url。
Basically you need to write your own IRequestHandler which handles a specific URL and returns your dynamically created data. Afterwards you macro should return a url which is configured for your request handler.