31
31
from ivle.webapp.publisher import NoPath
32
32
from ivle.webapp.breadcrumbs import Breadcrumber
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):
34
class XHTMLView(BaseView):
57
36
A view which provides a base class for views which need to return XHTML
58
37
It is expected that apps which use this view will be written using Genshi
93
72
app_template = os.path.join(os.path.dirname(
94
73
inspect.getmodule(self).__file__), self.template)
95
tmpl = self._loader.load(app_template)
74
loader = genshi.template.TemplateLoader(".", auto_reload=True)
75
tmpl = loader.load(app_template)
96
76
app = self.filter(tmpl.generate(viewctx), viewctx)
133
113
crumber = Breadcrumber(req)
135
115
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)
116
for ancestor in ancestry:
117
crumb = crumber.crumb(ancestor)
121
if hasattr(crumb, 'extra_breadcrumbs_before'):
122
ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_before)
123
ctx['breadcrumbs'].append(crumb)
124
if hasattr(crumb, 'extra_breadcrumbs_after'):
125
ctx['breadcrumbs'].extend(crumb.extra_breadcrumbs_after)
127
# If the view has specified text for a breadcrumb, add one.
128
if self.breadcrumb_text:
129
ctx['breadcrumbs'].append(ViewBreadcrumb(req, self))
131
# Allow the view to add its own fake breadcrumbs.
132
ctx['breadcrumbs'].extend(self.extra_breadcrumbs)
155
134
self.populate_headings(req, ctx)
156
tmpl = self._loader.load(os.path.join(os.path.dirname(__file__),
135
tmpl = loader.load(os.path.join(os.path.dirname(__file__),
157
136
'ivle-headings.html'))
158
137
req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
265
244
def populate(self, req, ctx):
267
246
ctx['exception'] = self.context
268
req.headers_out['X-IVLE-Error'] = self.context.message
270
248
class XHTMLUnauthorizedView(XHTMLErrorView):
271
249
template = 'xhtmlunauthorized.html'