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