~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/help/__init__.py

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os, inspect
 
2
 
 
3
import genshi
 
4
import genshi.template
 
5
 
 
6
import ivle.conf
 
7
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
 
8
from ivle.webapp.base.xhtml import XHTMLView
 
9
from ivle.webapp.errors import NotFound, Forbidden
 
10
 
 
11
def generate_toc(plugins, req):
 
12
    toc = {}
 
13
    for plugin in plugins:
 
14
        if hasattr(plugin, 'help'):
 
15
            # Get the dir the plugin resides in
 
16
            plugindir = os.path.dirname(inspect.getmodule(plugin).__file__)
 
17
            add_dict(toc, plugin.help, plugindir)
 
18
    return toc
 
19
 
 
20
def add_dict(newdict, curdict, plugin):
 
21
    for key in curdict:
 
22
        if key not in newdict:
 
23
            newdict[key] = {}
 
24
        if isinstance(curdict[key], dict):
 
25
            add_dict(newdict[key], curdict[key], plugin)
 
26
        else:
 
27
            newdict[key] = os.path.join(plugin, curdict[key])
 
28
    return newdict
 
29
 
 
30
class HelpView(XHTMLView):
 
31
    """Shows the help file for the specified path."""
 
32
 
 
33
    template = 'helpview.html'
 
34
 
 
35
    def __init__(self, req, path):
 
36
        self.paths = path.split('/')
 
37
 
 
38
    def authorize(self, req):
 
39
        return req.user is not None
 
40
 
 
41
    def populate(self, req, ctx):
 
42
        self.plugin_styles[Plugin] = ['help.css']
 
43
 
 
44
        helpfile = generate_toc(req.config.plugin_index[ViewPlugin], req)
 
45
        try:
 
46
            for path in self.paths:
 
47
                if len(path) > 0:
 
48
                    helpfile = helpfile[path]
 
49
        except (KeyError, TypeError):
 
50
            # Traversal failed. We 404.
 
51
            raise NotFound()
 
52
 
 
53
        if not isinstance(helpfile, basestring):
 
54
            # It's a virtual directory.
 
55
            raise Forbidden()
 
56
 
 
57
        ctx['helpfile'] = helpfile
 
58
 
 
59
 
 
60
class HelpToCView(XHTMLView):
 
61
    """Displays the help Table of Contents."""
 
62
    appname = 'help'
 
63
    template = 'toc.html'
 
64
 
 
65
    def authorize(self, req):
 
66
        return req.user is not None
 
67
 
 
68
    def populate(self, req, ctx):
 
69
        ctx['toc'] = generate_toc(req.config.plugin_index[ViewPlugin], req)
 
70
 
 
71
 
 
72
class Plugin(ViewPlugin, MediaPlugin):
 
73
    """The plugin for viewing help files."""
 
74
 
 
75
    urls = [
 
76
        ('+help', HelpToCView),
 
77
        ('+help/*path', HelpView)
 
78
    ]
 
79
 
 
80
    tabs = [
 
81
        ('help', 'Help', 'IVLE help pages', 'help.png', '+help', 100)
 
82
    ]
 
83
 
 
84
    media = 'media'