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

« back to all changes in this revision

Viewing changes to ivle/webapp/base/xhtml.py

Fix the XHTML base template to not crash when not logged in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2009 The University of Melbourne
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
# Author: Nick Chadwick
 
19
 
 
20
import inspect
 
21
import os.path
 
22
 
 
23
import genshi.template
 
24
 
 
25
from ivle.webapp.media import media_url
 
26
from ivle.webapp.base.views import BaseView
 
27
from ivle.webapp.base.plugins import OverlayPlugin
 
28
import ivle.conf
 
29
import ivle.util
 
30
 
 
31
class XHTMLView(BaseView):
 
32
    """
 
33
    A view which provides a base class for views which need to return XHTML
 
34
    It is expected that apps which use this view will be written using Genshi
 
35
    templates.
 
36
    """
 
37
 
 
38
    template = 'template.html'
 
39
    plugin_scripts = {}
 
40
    plugin_styles = {}
 
41
    overlay_blacklist = []
 
42
 
 
43
    def __init__(self, req, **kwargs):
 
44
        for key in kwargs:
 
45
            setattr(self, key, kwargs[key])
 
46
 
 
47
    def render(self, req):
 
48
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
 
49
 
 
50
        # View template
 
51
        viewctx = genshi.template.Context()
 
52
        self.populate(req, viewctx)
 
53
 
 
54
        # The template is found in the directory of the module containing the
 
55
        # view.
 
56
        app_template = os.path.join(os.path.dirname(
 
57
                        inspect.getmodule(self).__file__), self.template) 
 
58
        req.write_html_head_foot = False
 
59
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
 
60
        tmpl = loader.load(app_template)
 
61
        app = tmpl.generate(viewctx)
 
62
 
 
63
        for plugin in self.plugin_scripts:
 
64
            for path in self.plugin_scripts[plugin]:
 
65
                req.scripts.append(media_url(req, plugin, path))
 
66
 
 
67
        for plugin in self.plugin_styles:
 
68
            for path in self.plugin_styles[plugin]:
 
69
                req.styles.append(media_url(req, plugin, path))
 
70
 
 
71
        # Global template
 
72
        ctx = genshi.template.Context()
 
73
        # XXX: Leave this here!! (Before req.styles is read)
 
74
        ctx['overlays'] = self.render_overlays(req)
 
75
        ctx['app_styles'] = req.styles
 
76
        ctx['scripts'] = req.scripts
 
77
        ctx['scripts_init'] = req.scripts_init
 
78
        ctx['app_template'] = app
 
79
        self.populate_headings(req, ctx)
 
80
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
 
81
                                                        'ivle-headings.html'))
 
82
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
 
83
        
 
84
    def populate(self, req, ctx):
 
85
        raise NotImplementedError()
 
86
 
 
87
    def populate_headings(self, req, ctx):
 
88
        ctx['favicon'] = None
 
89
        ctx['root_dir'] = ivle.conf.root_dir
 
90
        ctx['public_host'] = ivle.conf.public_host
 
91
        ctx['write_javascript_settings'] = req.write_javascript_settings
 
92
        if req.user:
 
93
            ctx['login'] = req.user.login
 
94
            ctx['logged_in'] = True
 
95
            ctx['nick'] = req.user.nick
 
96
        else:
 
97
            ctx['login'] = None
 
98
            ctx['logged_in'] = False
 
99
        ctx['publicmode'] = req.publicmode
 
100
        ctx['apps_in_tabs'] = []
 
101
        for urlname in ivle.conf.apps.apps_in_tabs:
 
102
            new_app = {}
 
103
            app = ivle.conf.apps.app_url[urlname]
 
104
            new_app['this_app'] = hasattr(self, 'appname') \
 
105
                                  and urlname == self.appname
 
106
            if app.icon:
 
107
                new_app['has_icon'] = True
 
108
                icon_dir = ivle.conf.apps.app_icon_dir
 
109
                icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
 
110
                new_app['icon_url'] = icon_url
 
111
                if new_app['this_app']:
 
112
                    ctx['favicon'] = icon_url
 
113
            else:
 
114
                new_app['has_icon'] = False
 
115
            new_app['path'] = ivle.util.make_path(urlname)
 
116
            new_app['desc'] = app.desc
 
117
            new_app['name'] = app.name
 
118
            ctx['apps_in_tabs'].append(new_app)
 
119
            
 
120
    def render_overlays(self, req):
 
121
        """Generate XML streams for the overlays.
 
122
        
 
123
        Returns a list of streams. Populates the scripts, styles, and 
 
124
        scripts_init.
 
125
        """
 
126
        overlays = []
 
127
        for plugin in req.plugin_index[OverlayPlugin]:
 
128
            for overclass in plugin.overlays:
 
129
                if overclass in self.overlay_blacklist:
 
130
                    continue
 
131
                overlay = overclass(req)
 
132
                #TODO: Re-factor this to look nicer
 
133
                for mplugin in overlay.plugin_scripts:
 
134
                    for path in overlay.plugin_scripts[mplugin]:
 
135
                        req.scripts.append(media_url(req, mplugin, path))
 
136
 
 
137
                for mplugin in overlay.plugin_styles:
 
138
                    for path in overlay.plugin_styles[mplugin]:
 
139
                        req.styles.append(media_url(req, mplugin, path))
 
140
 
 
141
                req.scripts_init += overlay.plugin_scripts_init
 
142
 
 
143
                overlays.append(overlay.render(req))
 
144
        return overlays
 
145
 
 
146
class XHTMLErrorView(XHTMLView):
 
147
    template = 'xhtmlerror.html'
 
148
 
 
149
    def __init__(self, req, exception):
 
150
        self.context = exception
 
151
 
 
152
    def populate(self, req, ctx):
 
153
        ctx['exception'] = self.context