| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2012-2013, 2019 MATOBA Akihiro <matobaa+trac-hacks@gmail.com> |
|---|
| 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. |
|---|
| 8 | |
|---|
| 9 | from trac.core import Component, implements |
|---|
| 10 | from trac.web.api import IRequestFilter |
|---|
| 11 | from trac.web.chrome import ITemplateProvider, add_script, add_script_data |
|---|
| 12 | from pkg_resources import resource_filename |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | class TypeClassToTicket(Component): |
|---|
| 16 | """ set css-class to type on ticket. """ |
|---|
| 17 | implements(IRequestFilter, ITemplateProvider) |
|---|
| 18 | |
|---|
| 19 | def get_resource_tags(self, req, resource): |
|---|
| 20 | try: |
|---|
| 21 | from tractags.wiki import WikiTagProvider |
|---|
| 22 | tag_provider = self.compmgr[WikiTagProvider] |
|---|
| 23 | return tag_provider.get_resource_tags(req, resource) |
|---|
| 24 | except: |
|---|
| 25 | return [] |
|---|
| 26 | |
|---|
| 27 | # IRequestFilter methods |
|---|
| 28 | def pre_process_request(self, req, handler): |
|---|
| 29 | return handler # unchanged |
|---|
| 30 | |
|---|
| 31 | def post_process_request(self, req, template, data, content_type): |
|---|
| 32 | value = None |
|---|
| 33 | if template == 'ticket.html' and 'ticket' in data: |
|---|
| 34 | ticket = data['ticket'].values |
|---|
| 35 | fields = self.config.getlist('ticket', 'decorate_fields') |
|---|
| 36 | value = ' '.join(['%s_is_%s' % (field, value.rstrip(' ').rstrip(',').replace('"', '')) |
|---|
| 37 | for field in fields if field in ticket for value in ticket.get(field).split(' ')] |
|---|
| 38 | # FIXME: custom field of datetime occurs exception; it does not have 'split' attr. |
|---|
| 39 | + [ticket.get('type')] # backward compatibility |
|---|
| 40 | ) |
|---|
| 41 | if template == 'wiki_view.html': |
|---|
| 42 | value = ' '.join(['tagged_as_%s' % tag for tag in self.get_resource_tags(req, data['context'].resource)]) |
|---|
| 43 | |
|---|
| 44 | if value: |
|---|
| 45 | add_script(req, "contextchrome/js/bodyclassdecolator.js") |
|---|
| 46 | add_script_data(req, contextchrome_bodyclass=value) |
|---|
| 47 | return template, data, content_type |
|---|
| 48 | |
|---|
| 49 | # ITemplateProvider methods |
|---|
| 50 | def get_htdocs_dirs(self): |
|---|
| 51 | return [('contextchrome', resource_filename(__name__, 'htdocs'))] |
|---|
| 52 | |
|---|
| 53 | def get_templates_dirs(self): |
|---|
| 54 | return [] |
|---|