1 | | Since the version for 0.11 turned out to be extremely hacky and simplistic, I made an improved version. It works with 0.12 too! Feel free to use it however you like. |
2 | | |
3 | | {{{#!python |
4 | | #!/usr/bin/env python |
5 | | # -*- coding: utf-8 -*- |
6 | | |
7 | | from trac import wiki |
8 | | from trac import web |
9 | | from trac import perm |
10 | | |
11 | | |
12 | | def get_projects(req): |
13 | | """ |
14 | | Gives the projects to display. |
15 | | """ |
16 | | |
17 | | # Assume that the root is one level up. |
18 | | href = web.href.Href(req.base_path + '/..') |
19 | | for env_name, env_path in web.main.get_environments(req.environ).items(): |
20 | | try: |
21 | | env = web.main.open_environment(env_path, use_cache=True) |
22 | | except IOError, e: |
23 | | # Some directories might not be correct projects. |
24 | | # Catch the 'File not found' error for VERSION file. |
25 | | if e.errno == 2: |
26 | | continue |
27 | | raise |
28 | | proj = { |
29 | | 'env': env, |
30 | | 'env_name': env_name, |
31 | | 'name': env.project_name, |
32 | | 'description': env.project_description, |
33 | | 'href': href(env_name), |
34 | | } |
35 | | # Don't display projects you don't have permission to see. |
36 | | if 'WIKI_VIEW' in perm.PermissionCache(env, req.authname): |
37 | | yield proj |
38 | | |
39 | | |
40 | | class ProjectList(wiki.macros.WikiMacroBase): |
41 | | """ |
42 | | Display a list of projects on the wiki. |
43 | | """ |
44 | | |
45 | | def render_macro(self, req, name, content): |
46 | | html_lines = [u'<ul class="project-list">'] |
47 | | for proj in sorted(get_projects(req), key=lambda d:d['env_name']): |
48 | | html_lines.append(u"""<li> |
49 | | <a title="%(description)s" href="%(href)s">%(name)s</a> |
50 | | </li>""" % proj) |
51 | | html_lines.append('</ul>') |
52 | | return u''.join(html_lines) |
53 | | }}} |
| 1 | Since the version for 0.11 turned out to be extremely hacky and simplistic, I made an [attachment:ProjectList.py] improved version. It works with 0.12 too! Feel free to use it however you like. |