31
31
from ivle.webapp.publisher import NoPath
32
32
from ivle.webapp.breadcrumbs import Breadcrumber
34
class XHTMLView(BaseView):
35
class GenshiLoaderMixin(object):
36
"""Mixin for classes which need to render Genshi templates.
38
A TemplateLoader is shared between all instances, so templates are
39
cached across multiple instances and therefore also requests.
43
def __init__(self, *args, **kwargs):
44
super(GenshiLoaderMixin, self).__init__(*args, **kwargs)
46
# We use a single loader for all views, so we can cache the
47
# parsed templates. auto_reload is convenient and has a minimal
48
# performance penalty, so we'll leave it on.
49
if GenshiLoaderMixin._loader is None:
50
GenshiLoaderMixin._loader = genshi.template.TemplateLoader(
51
".", auto_reload=True,
55
class XHTMLView(GenshiLoaderMixin, BaseView):
36
57
A view which provides a base class for views which need to return XHTML
37
58
It is expected that apps which use this view will be written using Genshi
72
93
app_template = os.path.join(os.path.dirname(
73
94
inspect.getmodule(self).__file__), self.template)
74
loader = genshi.template.TemplateLoader(".", auto_reload=True)
75
tmpl = loader.load(app_template)
95
tmpl = self._loader.load(app_template)
76
96
app = self.filter(tmpl.generate(viewctx), viewctx)
106
126
ctx['title_img'] = media_url(req, CorePlugin,
107
127
"images/chrome/root-breadcrumb.png")
109
ctx['ancestry'] = self.get_context_ancestry(req)
129
ancestry = self.get_context_ancestry(req)
113
# If the view has specified text for a breadcrumb, add one.
114
if self.breadcrumb_text:
115
ctx['extra_breadcrumbs'] = [ViewBreadcrumb(req, self)]
117
ctx['extra_breadcrumbs'] = []
119
# Allow the view to add its own fake breadcrumbs.
120
ctx['extra_breadcrumbs'] += self.extra_breadcrumbs
122
ctx['crumb'] = Breadcrumber(req).crumb
133
crumber = Breadcrumber(req)
135
ctx['breadcrumbs'] = []
136
if not req.publicmode:
137
for ancestor in ancestry:
138
crumb = crumber.crumb(ancestor)
142
if hasattr(crumb, 'extra_breadcrumbs_before'):
143
ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_before)
144
ctx['breadcrumbs'].append(crumb)
145
if hasattr(crumb, 'extra_breadcrumbs_after'):
146
ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_after)
148
# If the view has specified text for a breadcrumb, add one.
149
if self.breadcrumb_text:
150
ctx['breadcrumbs'].append(ViewBreadcrumb(req, self))
152
# Allow the view to add its own fake breadcrumbs.
153
ctx['breadcrumbs'].extend(self.extra_breadcrumbs)
123
155
self.populate_headings(req, ctx)
124
tmpl = loader.load(os.path.join(os.path.dirname(__file__),
156
tmpl = self._loader.load(os.path.join(os.path.dirname(__file__),
125
157
'ivle-headings.html'))
126
158
req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
153
185
for tab in plugin.tabs:
154
# tab is a tuple: name, title, desc, icon, path
186
# tab is a tuple: name, title, desc, icon, path, weight, admin
187
# (Admin is optional, defaults to false)
156
189
new_app['this_app'] = hasattr(self, 'tab') \
157
190
and tab[0] == self.tab
165
198
ctx['favicon'] = icon_url
167
200
new_app['has_icon'] = False
201
# The following check is here, so it is AFTER setting the
202
# icon, but BEFORE actually installing the tab in the menu
203
if len(tab) > 6 and tab[6]:
205
if not (req.user and req.user.admin):
168
207
new_app['path'] = req.make_path(tab[4])
169
208
new_app['desc'] = tab[2]
170
209
new_app['name'] = tab[1]
226
265
def populate(self, req, ctx):
228
267
ctx['exception'] = self.context
268
req.headers_out['X-IVLE-Error'] = self.context.message
230
270
class XHTMLUnauthorizedView(XHTMLErrorView):
231
271
template = 'xhtmlunauthorized.html'