1808
by Matt Giuca
Added new base view TextView which supports Genshi text templates (as opposed to XHTML templates). |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2009 The University of Melbourne
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
# Author: Matt Giuca
|
|
19 |
||
20 |
import inspect |
|
21 |
import os.path |
|
22 |
||
23 |
import genshi.template |
|
24 |
||
25 |
from ivle.webapp.base.views import BaseView |
|
26 |
from ivle.webapp.base.xhtml import GenshiLoaderMixin |
|
27 |
||
28 |
||
29 |
class TextView(GenshiLoaderMixin, BaseView): |
|
30 |
"""
|
|
31 |
A view which provides a base class for views which need to return plain
|
|
32 |
text results using Genshi text templates.
|
|
33 |
"""
|
|
34 |
||
35 |
template = 'template.txt' |
|
36 |
content_type = 'text/plain' |
|
37 |
||
38 |
def get_context_ancestry(self, req): |
|
39 |
return req.publisher.get_ancestors(self.context) |
|
40 |
||
41 |
def filter(self, stream, ctx): |
|
42 |
return stream |
|
43 |
||
44 |
def render(self, req): |
|
45 |
req.content_type = self.content_type |
|
46 |
||
47 |
# View template
|
|
48 |
viewctx = genshi.template.Context() |
|
49 |
self.populate(req, viewctx) |
|
50 |
||
51 |
# The template is found in the directory of the module containing the
|
|
52 |
# view.
|
|
53 |
app_template = os.path.join(os.path.dirname( |
|
54 |
inspect.getmodule(self).__file__), self.template) |
|
55 |
tmpl = self._loader.load(app_template, |
|
56 |
cls=genshi.template.text.NewTextTemplate) |
|
57 |
app = self.filter(tmpl.generate(viewctx), viewctx) |
|
58 |
||
59 |
self.populate_headings(req, viewctx) |
|
60 |
req.write(tmpl.generate(viewctx).render()) |
|
61 |
||
62 |
def populate(self, req, ctx): |
|
63 |
raise NotImplementedError() |
|
64 |
||
65 |
def populate_headings(self, req, ctx): |
|
66 |
ctx['root_dir'] = req.config['urls']['root'] |
|
67 |
ctx['public_host'] = req.config['urls']['public_host'] |
|
68 |
ctx['svn_base'] = req.config['urls']['svn_addr'] |
|
69 |
ctx['write_javascript_settings'] = req.write_javascript_settings |
|
70 |
if req.user: |
|
71 |
ctx['login'] = req.user.login |
|
72 |
ctx['logged_in'] = True |
|
73 |
ctx['nick'] = req.user.nick |
|
74 |
else: |
|
75 |
ctx['login'] = None |
|
76 |
ctx['logged_in'] = False |
|
77 |
ctx['publicmode'] = req.publicmode |