1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
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.
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.
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
18
# Author: Nick Chadwick
24
import genshi.template
26
from ivle.webapp.media import media_url
27
from ivle.webapp.core import Plugin as CorePlugin
28
from ivle.webapp.base.views import BaseView
29
from ivle.webapp.base.plugins import ViewPlugin, OverlayPlugin
30
from ivle.webapp.errors import HTTPError, Unauthorized
34
class XHTMLView(BaseView):
36
A view which provides a base class for views which need to return XHTML
37
It is expected that apps which use this view will be written using Genshi
41
template = 'template.html'
45
overlay_blacklist = []
47
def __init__(self, req, **kwargs):
49
setattr(self, key, kwargs[key])
51
def render(self, req):
52
req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
55
viewctx = genshi.template.Context()
56
self.populate(req, viewctx)
58
# The template is found in the directory of the module containing the
60
app_template = os.path.join(os.path.dirname(
61
inspect.getmodule(self).__file__), self.template)
62
req.write_html_head_foot = False
63
loader = genshi.template.TemplateLoader(".", auto_reload=True)
64
tmpl = loader.load(app_template)
65
app = tmpl.generate(viewctx)
67
for plugin in self.plugin_scripts:
68
for path in self.plugin_scripts[plugin]:
69
req.scripts.append(media_url(req, plugin, path))
71
for plugin in self.plugin_styles:
72
for path in self.plugin_styles[plugin]:
73
req.styles.append(media_url(req, plugin, path))
76
ctx = genshi.template.Context()
77
# XXX: Leave this here!! (Before req.styles is read)
78
ctx['overlays'] = self.render_overlays(req)
80
ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
81
ctx['styles'] += req.styles
83
ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
84
('util.js', 'json2.js', 'md5.js')]
85
ctx['scripts'] += req.scripts
87
ctx['scripts_init'] = req.scripts_init
88
ctx['app_template'] = app
89
self.populate_headings(req, ctx)
90
tmpl = loader.load(os.path.join(os.path.dirname(__file__),
91
'ivle-headings.html'))
92
req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
94
def populate(self, req, ctx):
95
raise NotImplementedError()
97
def populate_headings(self, req, ctx):
99
ctx['root_dir'] = ivle.conf.root_dir
100
ctx['public_host'] = ivle.conf.public_host
101
ctx['write_javascript_settings'] = req.write_javascript_settings
103
ctx['login'] = req.user.login
104
ctx['logged_in'] = True
105
ctx['nick'] = req.user.nick
108
ctx['logged_in'] = False
109
ctx['publicmode'] = req.publicmode
110
if hasattr(self, 'help'):
111
ctx['help_path'] = self.help
113
ctx['apps_in_tabs'] = []
114
for plugin in req.config.plugin_index[ViewPlugin]:
115
if not hasattr(plugin, 'tabs'):
118
for tab in plugin.tabs:
119
# tab is a tuple: name, title, desc, icon, path
121
new_app['this_app'] = hasattr(self, 'appname') \
122
and tab[0] == self.appname
125
if tab[3] is not None:
126
new_app['has_icon'] = True
127
icon_url = media_url(req, plugin, tab[3])
128
new_app['icon_url'] = icon_url
129
if new_app['this_app']:
130
ctx['favicon'] = icon_url
132
new_app['has_icon'] = False
133
new_app['path'] = ivle.util.make_path(tab[4])
134
new_app['desc'] = tab[2]
135
new_app['name'] = tab[1]
136
new_app['weight'] = tab[5]
137
ctx['apps_in_tabs'].append(new_app)
139
ctx['apps_in_tabs'].sort(key=lambda tab: tab['weight'])
141
def render_overlays(self, req):
142
"""Generate XML streams for the overlays.
144
Returns a list of streams. Populates the scripts, styles, and
148
if not self.allow_overlays:
151
for plugin in req.config.plugin_index[OverlayPlugin]:
152
for overclass in plugin.overlays:
153
if overclass in self.overlay_blacklist:
155
overlay = overclass(req)
156
#TODO: Re-factor this to look nicer
157
for mplugin in overlay.plugin_scripts:
158
for path in overlay.plugin_scripts[mplugin]:
159
req.scripts.append(media_url(req, mplugin, path))
161
for mplugin in overlay.plugin_styles:
162
for path in overlay.plugin_styles[mplugin]:
163
req.styles.append(media_url(req, mplugin, path))
165
req.scripts_init += overlay.plugin_scripts_init
167
overlays.append(overlay.render(req))
171
def get_error_view(cls, e):
172
view_map = {HTTPError: XHTMLErrorView,
173
Unauthorized: XHTMLUnauthorizedView}
174
for exccls in inspect.getmro(type(e)):
175
if exccls in view_map:
176
return view_map[exccls]
178
class XHTMLErrorView(XHTMLView):
179
template = 'xhtmlerror.html'
181
def __init__(self, req, exception):
182
self.context = exception
184
def populate(self, req, ctx):
185
ctx['exception'] = self.context
187
class XHTMLUnauthorizedView(XHTMLErrorView):
188
template = 'xhtmlunauthorized.html'
190
def __init__(self, req, exception):
191
super(XHTMLUnauthorizedView, self).__init__(req, exception)
194
# Not logged in. Redirect to login page.
195
req.throw_redirect('/+login?' +
196
urllib.urlencode([('url', req.uri)]))