| 1 | """ |
|---|
| 2 | Allow Trac to support templates for wiki-pages |
|---|
| 3 | |
|---|
| 4 | Example: |
|---|
| 5 | {{{ |
|---|
| 6 | [[ChooseTemplate]] |
|---|
| 7 | [[ChooseTemplate(templates)]] |
|---|
| 8 | }}} |
|---|
| 9 | The argument has to point to a wikipage whose subpages are the templates. |
|---|
| 10 | If no argumnent is given, a default argument of "WikiTemplates" is assumed. |
|---|
| 11 | """ |
|---|
| 12 | |
|---|
| 13 | from StringIO import StringIO |
|---|
| 14 | import trac.perm |
|---|
| 15 | from trac.util import TracError |
|---|
| 16 | from trac.wiki.model import WikiPage |
|---|
| 17 | |
|---|
| 18 | """ |
|---|
| 19 | TODO: |
|---|
| 20 | - consider removing the links in the preview -> wikipreview = hdf.getValue("args.preview", "") |
|---|
| 21 | - do some sanity checks on the arguments |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | def 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 | |
|---|
| 39 | def 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 | |
|---|
| 51 | def 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 | |
|---|
| 61 | def 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 | |
|---|
| 69 | def 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 | |
|---|
| 81 | def 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 | |
|---|
| 95 | def getTemplate(env, pageName): |
|---|
| 96 | return WikiPage(env, pageName).text |
|---|