~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-12-08 03:50:24 UTC
  • mfrom: (1294.2.143 ui-the-third)
  • Revision ID: grantw@unimelb.edu.au-20091208035024-wjx8zp54gth15ph8
Merge ui-the-third. This is another UI revamp.

The header is now thin! Thin! The yellow bar is gone. The tabs are gone.
Breadcrumbs are here. Routes is replaced (with an object publishing
mechanism). Views are less repetitive. etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
7
7
from ivle.webapp.base.xhtml import XHTMLView
8
8
from ivle.webapp.errors import NotFound, Forbidden
 
9
from ivle.webapp.publisher.decorators import forward_route, reverse_route
 
10
from ivle.webapp.publisher import ROOT
 
11
from ivle.webapp import ApplicationRoot
9
12
 
10
 
def generate_toc(plugins, req):
11
 
    toc = {}
 
13
def generate_toc(plugins):
 
14
    """Create a root HelpTree with content from the plugins."""
 
15
    toc = HelpTree(None, 'Help', {})
12
16
    for plugin in plugins:
13
17
        if hasattr(plugin, 'help'):
14
18
            # Get the dir the plugin resides in
17
21
    return toc
18
22
 
19
23
def add_dict(newdict, curdict, plugin):
 
24
    """Deeply merge curdict into newdict."""
20
25
    for key in curdict:
21
 
        if key not in newdict:
22
 
            newdict[key] = {}
23
26
        if isinstance(curdict[key], dict):
 
27
            if key not in newdict:
 
28
                newdict[key] = HelpTree(newdict, key, {})
24
29
            add_dict(newdict[key], curdict[key], plugin)
25
30
        else:
26
 
            newdict[key] = os.path.join(plugin, curdict[key])
 
31
            newdict[key] = HelpEntry(newdict, key, os.path.join(plugin, curdict[key]))
27
32
    return newdict
28
33
 
29
 
class HelpView(XHTMLView):
 
34
class HelpTreeView(XHTMLView):
 
35
    tab = 'help'
 
36
    template = 'toc.html'
 
37
    
30
38
    """Shows the help file for the specified path."""
 
39
    def authorize(self, req):
 
40
        return req.user is not None
 
41
 
 
42
    def populate(self, req, ctx):
 
43
        self.plugin_styles[Plugin] = ['help.css']
 
44
 
 
45
        ctx['req'] = req
 
46
        ctx['context'] = self.context
 
47
        ctx['HelpTree'] = HelpTree
 
48
        ctx['HelpEntry'] = HelpEntry
 
49
 
 
50
class HelpEntryView(XHTMLView):
31
51
    tab = 'help'
32
52
    template = 'helpview.html'
33
 
 
34
 
    def __init__(self, req, path):
35
 
        self.paths = path.split('/')
36
 
 
 
53
    
 
54
    """Shows the help file for the specified path."""
37
55
    def authorize(self, req):
38
56
        return req.user is not None
39
57
 
40
58
    def populate(self, req, ctx):
41
59
        self.plugin_styles[Plugin] = ['help.css']
42
 
 
43
 
        helpfile = generate_toc(req.config.plugin_index[ViewPlugin], req)
44
 
        try:
45
 
            for path in self.paths:
46
 
                if len(path) > 0:
47
 
                    helpfile = helpfile[path]
48
 
        except (KeyError, TypeError):
49
 
            # Traversal failed. We 404.
50
 
            raise NotFound()
51
 
 
52
 
        if not isinstance(helpfile, basestring):
53
 
            # It's a virtual directory.
54
 
            raise Forbidden()
55
 
 
56
 
        ctx['helpfile'] = helpfile
57
 
 
58
 
 
59
 
class HelpToCView(XHTMLView):
60
 
    """Displays the help Table of Contents."""
61
 
    tab = 'help'
62
 
    template = 'toc.html'
63
 
 
64
 
    def authorize(self, req):
65
 
        return req.user is not None
66
 
 
67
 
    def populate(self, req, ctx):
68
 
        ctx['toc'] = generate_toc(req.config.plugin_index[ViewPlugin], req)
69
 
 
 
60
        
 
61
        ctx['helpfile'] = self.context.file
 
62
 
 
63
class HelpTree(dict):
 
64
    def __init__(self, parent, name, tree):
 
65
        super(HelpTree, self).__init__(tree)
 
66
        self.parent = parent
 
67
        self.name = name
 
68
 
 
69
class HelpEntry(object):
 
70
    def __init__(self, parent, name, file):
 
71
        self.parent = parent
 
72
        self.name = name
 
73
        self.file = file
 
74
 
 
75
@forward_route(ApplicationRoot, '+help')
 
76
def root_to_helptree(root):
 
77
    return generate_toc(root.config.plugin_index[ViewPlugin])
 
78
 
 
79
@forward_route(HelpTree, argc=1)
 
80
def helptree_to_help(help, subhelp_name):
 
81
    try:
 
82
        return help[subhelp_name]
 
83
    except KeyError:
 
84
        # No help entry of that name
 
85
        return None
 
86
 
 
87
@reverse_route(HelpTree)
 
88
def helptree_url(helptree):
 
89
    if helptree.parent is None:
 
90
        return (ROOT, '+help')
 
91
    return (helptree.parent, helptree.name)
 
92
 
 
93
@reverse_route(HelpEntry)
 
94
def helpentry_url(helpentry):
 
95
    return (helpentry.parent, helpentry.name)
 
96
 
 
97
class HelpBreadcrumb(object):
 
98
    def __init__(self, req, context):
 
99
        self.req = req
 
100
        self.context = context
 
101
 
 
102
    @property
 
103
    def url(self):
 
104
        return self.req.publisher.generate(self.context)
 
105
 
 
106
    @property
 
107
    def text(self):
 
108
        return self.context.name
 
109
 
 
110
class HelpTreeBreadcrumb(HelpBreadcrumb):
 
111
    @property
 
112
    def menu(self):
 
113
        return dict((item.name, self.req.publisher.generate(item))
 
114
                    for item in self.context.values())
70
115
 
71
116
class Plugin(ViewPlugin, MediaPlugin):
72
117
    """The plugin for viewing help files."""
73
 
 
74
 
    urls = [
75
 
        ('+help', HelpToCView),
76
 
        ('+help/*path', HelpView)
77
 
    ]
 
118
    forward_routes = (root_to_helptree, helptree_to_help)
 
119
    reverse_routes = (helptree_url, helpentry_url)
 
120
 
 
121
    views = [(HelpTree, '+index', HelpTreeView),
 
122
             (HelpEntry, '+index', HelpEntryView)]
 
123
 
 
124
    breadcrumbs = {HelpEntry: HelpBreadcrumb,
 
125
                   HelpTree: HelpTreeBreadcrumb,
 
126
                  }
78
127
 
79
128
    tabs = [
80
129
        ('help', 'Help', 'Get help with IVLE', 'help.png', '+help', 100)