root/trac-plugins/navaddplugin/navadd/navadd.py

Revision 172, 1.8 kB (checked in by cedenoj, 10 months ago)

refs #2723

Line 
1 from trac.core import *
2 from trac.web.chrome import INavigationContributor, ITemplateProvider
3 from trac.util import Markup
4 from trac.web.href import Href
5 from trac.ticket.report import ReportModule
6
7 class NavAdd(Component):
8     """ Allows to add items to main and meta navigation bar"""
9     implements(INavigationContributor)
10
11     nav_contributors = ExtensionPoint(INavigationContributor)
12
13     # INavigationContributor methods
14     def get_active_navigation_item(self, req):
15         return ''
16                
17     def get_navigation_items(self, req):
18         add = self.env.config.get('navadd', 'add_items', ''). \
19                 replace(',', ' ').split()
20
21         items = []
22         for a in add:
23             title = self.env.config.get('navadd', '%s.title' % a)
24             url = self.env.config.get('navadd', '%s.url' % a)
25             perm = self.env.config.get('navadd', '%s.perm' % a)
26             target = self.env.config.get('navadd', '%s.target' % a)
27
28             if url == '/report':
29                 url = self.get_first_report_url()
30
31             if perm and not req.perm.has_permission(perm):
32                 continue
33
34             if target not in ('mainnav', 'metanav'):
35                 target = 'mainnav'
36
37             # the conditional below was added to make sure that we got the correct links in our multi-trac setup
38             if url.startswith('/'):
39                 url = req.href.wiki().replace('/wiki', '') + url
40
41             items.append((target, a, Markup('<a href="%s">%s</a>' % (url, title))))
42
43         return items
44
45     # The following method makes sure that we always get the first report for a given trac instance
46     def get_first_report_url(self):
47         db = self.env.get_db_cnx()
48         cursor = db.cursor()
49         cursor.execute('SELECT id AS report FROM report ORDER BY report LIMIT 1')
50         row = cursor.fetchone()
51         return '/report/' + str(row[0]);
Note: See TracBrowser for help on using the browser.