Edgewall Software

MacroBazaar: ChooseTemplate.1.py

File ChooseTemplate.1.py, 2.8 KB (added by spamfaenger@…, 7 years ago)

spelling mistake in the file

Line 
1"""
2Allow Trac to support templates for wiki-pages
3
4Example:
5{{{
6[[ChooseTemplate]]
7[[ChooseTemplate(templates)]]
8}}}
9The argument has to point to a wikipage whose subpages are the templates.
10If no argumnent is given, a default argument of "WikiTemplates" is assumed.
11"""
12
13from StringIO import StringIO
14import trac.perm
15from trac.util import TracError
16from trac.wiki.model import WikiPage
17
18"""
19TODO:
20- consider removing the links in the preview -> wikipreview = hdf.getValue("args.preview", "")
21- do some sanity checks on the arguments
22"""
23
24def execute(hdf, args, env):
25        if hdf.getValue('macro.blog.rendering', '0') == '1':
26                # I do not want to replace the blog main page!
27                return ""
28        if cannotChangeWiki(hdf, env):
29                TracError("Insufficient privileges to choose a template")
30       
31        templateToUse = hdf.getValue("args.useTemplate", "")
32        if templateToUse:
33                insertTemplate(hdf, env, templateToUse)
34                return pageReloadCode()
35       
36        templateBasePath = args or "WikiTemplates"
37        return renderTemplateChooser(env, templateBasePath)
38
39def cannotChangeWiki(hdf, env):
40        authname= hdf.getValue("trac.authname", "anonymous")
41        readonlypage= bool(hdf.getValue("wiki.readonly", "0"))
42        perm= trac.perm.PermissionCache(env, authname)
43        canChange= perm.has_permission('WIKI_CREATE') \
44                        or perm.has_permission('WIKI_EDIT')
45        if readonlypage:
46                canChange= perm.has_permission('WIKI_ADMIN') \
47                                or perm.has_permission('TRAC_ADMIN')
48        return not canChange
49
50
51def insertTemplate(hdf, env, templateToUse):
52        pagename= hdf.getValue("wiki.page_name", "")
53        authname= hdf.getValue("trac.authname", "anonymous")
54        page = WikiPage(env, pagename)
55        page.text = getTemplate(env, templateToUse)
56        page.version += 1
57        # TODO: How do we get remote_addr from a macro? (the last argument)
58        page.save(authname, 'Template '+templateToUse+' applied', None)
59
60
61def pageReloadCode():
62        # Ugly hack... is there a way to load the modified wikipage directly?
63        return "<a href='?action=edit' id='templateAppliedLink'>" \
64                        "Template applied, please start editing</a>" \
65                        "<script type='text/javascript'> window.location = " \
66                        "document.getElementById('templateAppliedLink').href </script>"
67
68
69def renderTemplateChooser(env, templateBaseSite):
70        templates = getTemplates(env, templateBaseSite)
71        out = StringIO()
72       
73        out.write("<ul>")
74        for template in templates:
75                out.write("<li> <a href='?useTemplate=" + template + "'>" + template + "</a></li>")
76        out.write("</ul>")
77       
78        return out.getvalue()
79
80
81def getTemplates(env, templateBaseSite):
82        sql = "SELECT DISTINCT name from wiki where name like '%s/%%' order by name asc" % templateBaseSite
83        cursor = env.get_db_cnx().cursor()
84        cursor.execute(sql)
85
86        result = []
87        while 1:
88                rowName = cursor.fetchone()
89                if rowName == None: # this stinks
90                        break
91                result.append(rowName[0])
92        return result
93
94
95def getTemplate(env, pageName):
96        return WikiPage(env, pageName).text