Opened 14 years ago

Last modified 12 years ago

#8060 new defect

Improved version for trac 0.11 and 0.12 — at Initial Version

Reported by: anonymous Owned by: mathomas
Priority: normal Component: ListTracProjectsMacro
Severity: normal Keywords:
Cc: Trac Release: 0.12

Description

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.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from trac import wiki
from trac import web
from trac import perm


def get_projects(req):
    """
    Gives the projects to display.
    """

    # Assume that the root is one level up.
    href = web.href.Href(req.base_path + '/..')
    for env_name, env_path in web.main.get_environments(req.environ).items():
        try:
            env = web.main.open_environment(env_path, use_cache=True)
        except IOError, e:
            # Some directories might not be correct projects.
            # Catch the 'File not found' error for VERSION file.
            if e.errno == 2:
                continue
            raise
        proj = {
            'env': env,
            'env_name': env_name,
            'name': env.project_name,
            'description': env.project_description,
            'href': href(env_name),
        }
        # Don't display projects you don't have permission to see.
        if 'WIKI_VIEW' in perm.PermissionCache(env, req.authname):
            yield proj


class ProjectList(wiki.macros.WikiMacroBase):
    """
    Display a list of projects on the wiki.
    """

    def render_macro(self, req, name, content):
        html_lines = [u'<ul class="project-list">']
        for proj in sorted(get_projects(req), key=lambda d:d['env_name']):
            html_lines.append(u"""<li>
    <a title="%(description)s" href="%(href)s">%(name)s</a>
</li>""" % proj)
        html_lines.append('</ul>')
        return u''.join(html_lines)

Change History (1)

Changed 14 years ago by anonymous

Attachment: ProjectList.py added
Note: See TracTickets for help on using tickets.