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
23
import genshi.template
25
from ivle.webapp.media import media_url
26
from ivle.webapp.base.views import BaseView
27
from ivle.webapp.base.plugins import OverlayPlugin
31
class XHTMLView(BaseView):
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
38
template = 'template.html'
41
overlay_blacklist = []
43
def __init__(self, req, **kwargs):
45
setattr(self, key, kwargs[key])
47
def render(self, req):
48
req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
51
viewctx = genshi.template.Context()
52
self.populate(req, viewctx)
54
# The template is found in the directory of the module containing the
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)
63
for plugin in self.plugin_scripts:
64
for path in self.plugin_scripts[plugin]:
65
req.scripts.append(media_url(req, plugin, path))
67
for plugin in self.plugin_styles:
68
for path in self.plugin_styles[plugin]:
69
req.styles.append(media_url(req, plugin, path))
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'))
84
def populate(self, req, ctx):
85
raise NotImplementedError()
87
def populate_headings(self, req, ctx):
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
93
ctx['login'] = req.user.login
94
ctx['logged_in'] = True
95
ctx['nick'] = req.user.nick
98
ctx['publicmode'] = req.publicmode
99
ctx['apps_in_tabs'] = []
100
for urlname in ivle.conf.apps.apps_in_tabs:
102
app = ivle.conf.apps.app_url[urlname]
103
new_app['this_app'] = hasattr(self, 'appname') \
104
and urlname == self.appname
106
new_app['has_icon'] = True
107
icon_dir = ivle.conf.apps.app_icon_dir
108
icon_url = ivle.util.make_path(os.path.join(icon_dir, app.icon))
109
new_app['icon_url'] = icon_url
110
if new_app['this_app']:
111
ctx['favicon'] = icon_url
113
new_app['has_icon'] = False
114
new_app['path'] = ivle.util.make_path(urlname)
115
new_app['desc'] = app.desc
116
new_app['name'] = app.name
117
ctx['apps_in_tabs'].append(new_app)
119
def render_overlays(self, req):
120
"""Generate XML streams for the overlays.
122
Returns a list of streams. Populates the scripts, styles, and
126
for plugin in req.plugin_index[OverlayPlugin]:
127
for overclass in plugin.overlays:
128
if overclass in self.overlay_blacklist:
130
overlay = overclass(req)
131
#TODO: Re-factor this to look nicer
132
for mplugin in overlay.plugin_scripts:
133
for path in overlay.plugin_scripts[mplugin]:
134
req.scripts.append(media_url(req, mplugin, path))
136
for mplugin in overlay.plugin_styles:
137
for path in overlay.plugin_styles[mplugin]:
138
req.styles.append(media_url(req, mplugin, path))
140
req.scripts_init += overlay.plugin_scripts_init
142
overlays.append(overlay.render(req))