| [5529] | 1 | """ |
|---|
| 2 | TicketSidebarProvider |
|---|
| 3 | a plugin for Trac to provide content alongside the ticket |
|---|
| 4 | http://trac.edgewall.org/wiki/TracTicketsCustomFields |
|---|
| 5 | """ |
|---|
| 6 | |
|---|
| 7 | from genshi.builder import tag |
|---|
| 8 | from genshi.filters import Transformer |
|---|
| [11697] | 9 | from pkg_resources import resource_filename |
|---|
| [5529] | 10 | |
|---|
| 11 | from trac.config import Option |
|---|
| 12 | from trac.core import * |
|---|
| 13 | from trac.web.api import ITemplateStreamFilter |
|---|
| 14 | from trac.web.chrome import add_stylesheet |
|---|
| 15 | from trac.web.chrome import ITemplateProvider |
|---|
| 16 | |
|---|
| [11697] | 17 | from ticketsidebarprovider.interface import ITicketSidebarProvider |
|---|
| 18 | |
|---|
| 19 | |
|---|
| [5529] | 20 | class TicketSidebarProvider(Component): |
|---|
| 21 | |
|---|
| 22 | implements(ITemplateStreamFilter, ITemplateProvider) |
|---|
| [11697] | 23 | |
|---|
| [5529] | 24 | providers = ExtensionPoint(ITicketSidebarProvider) |
|---|
| 25 | |
|---|
| 26 | ### method for ITemplateStreamFilter : |
|---|
| 27 | ### Filter a Genshi event stream prior to rendering. |
|---|
| 28 | |
|---|
| 29 | def filter_stream(self, req, method, filename, stream, data): |
|---|
| 30 | """Return a filtered Genshi event stream, or the original unfiltered |
|---|
| 31 | stream if no match. |
|---|
| 32 | |
|---|
| 33 | `req` is the current request object, `method` is the Genshi render |
|---|
| 34 | method (xml, xhtml or text), `filename` is the filename of the template |
|---|
| 35 | to be rendered, `stream` is the event stream and `data` is the data for |
|---|
| 36 | the current template. |
|---|
| 37 | |
|---|
| 38 | See the Genshi documentation for more information. |
|---|
| 39 | """ |
|---|
| 40 | |
|---|
| 41 | if filename != 'ticket.html': |
|---|
| 42 | return stream |
|---|
| 43 | |
|---|
| 44 | ticket = data['ticket'] |
|---|
| 45 | for provider in self.providers: # TODO : sorting |
|---|
| 46 | if provider.enabled(req, ticket): |
|---|
| [11697] | 47 | add_stylesheet(req, 'common/css/ticket-sidebar.css') |
|---|
| 48 | filter = Transformer('//div[@id="content"]') |
|---|
| 49 | stream |= filter.after(tag.div(provider.content(req, ticket), |
|---|
| 50 | **{'class': "sidebar" })) |
|---|
| [5529] | 51 | return stream |
|---|
| 52 | |
|---|
| 53 | def get_htdocs_dirs(self): |
|---|
| 54 | """Return a list of directories with static resources (such as style |
|---|
| 55 | sheets, images, etc.) |
|---|
| 56 | |
|---|
| 57 | Each item in the list must be a `(prefix, abspath)` tuple. The |
|---|
| 58 | `prefix` part defines the path in the URL that requests to these |
|---|
| 59 | resources are prefixed with. |
|---|
| 60 | |
|---|
| 61 | The `abspath` is the absolute path to the directory containing the |
|---|
| 62 | resources on the local file system. |
|---|
| 63 | """ |
|---|
| 64 | return [('common', resource_filename(__name__, 'htdocs'))] |
|---|
| 65 | |
|---|
| 66 | def get_templates_dirs(self): |
|---|
| 67 | """Return a list of directories containing the provided template |
|---|
| 68 | files. |
|---|
| 69 | """ |
|---|
| 70 | return [] |
|---|