~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/base/xhtml.py

ivle.webapp.testing: Add, with fake request and user.
ivle.webapp.base.test: Add! Test the JSONRESTView, using the new mocks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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: Nick Chadwick
19
 
 
20
 
import inspect
21
 
import os.path
22
 
import urllib
23
 
 
24
 
import genshi.template
25
 
 
26
 
from ivle.webapp.media import media_url
27
 
from ivle.webapp.core import Plugin as CorePlugin
28
 
from ivle.webapp.base.views import BaseView
29
 
from ivle.webapp.base.plugins import ViewPlugin, OverlayPlugin
30
 
from ivle.webapp.errors import HTTPError, Unauthorized
31
 
import ivle.conf
32
 
import ivle.util
33
 
 
34
 
class XHTMLView(BaseView):
35
 
    """
36
 
    A view which provides a base class for views which need to return XHTML
37
 
    It is expected that apps which use this view will be written using Genshi
38
 
    templates.
39
 
    """
40
 
 
41
 
    template = 'template.html'
42
 
    plugin_scripts = {}
43
 
    plugin_styles = {}
44
 
    allow_overlays = True
45
 
    overlay_blacklist = []
46
 
 
47
 
    def __init__(self, req, **kwargs):
48
 
        for key in kwargs:
49
 
            setattr(self, key, kwargs[key])
50
 
 
51
 
    def filter(self, stream, ctx):
52
 
        return stream
53
 
 
54
 
    def render(self, req):
55
 
        req.content_type = 'text/html' # TODO: Detect application/xhtml+xml
56
 
 
57
 
        # View template
58
 
        viewctx = genshi.template.Context()
59
 
        self.populate(req, viewctx)
60
 
 
61
 
        # The template is found in the directory of the module containing the
62
 
        # view.
63
 
        app_template = os.path.join(os.path.dirname(
64
 
                        inspect.getmodule(self).__file__), self.template) 
65
 
        loader = genshi.template.TemplateLoader(".", auto_reload=True)
66
 
        tmpl = loader.load(app_template)
67
 
        app = self.filter(tmpl.generate(viewctx), viewctx)
68
 
 
69
 
        for plugin in self.plugin_scripts:
70
 
            for path in self.plugin_scripts[plugin]:
71
 
                req.scripts.append(media_url(req, plugin, path))
72
 
 
73
 
        for plugin in self.plugin_styles:
74
 
            for path in self.plugin_styles[plugin]:
75
 
                req.styles.append(media_url(req, plugin, path))
76
 
 
77
 
        # Global template
78
 
        ctx = genshi.template.Context()
79
 
        # XXX: Leave this here!! (Before req.styles is read)
80
 
        ctx['overlays'] = self.render_overlays(req) if req.user else []
81
 
 
82
 
        ctx['styles'] = [media_url(req, CorePlugin, 'ivle.css')]
83
 
        ctx['styles'] += req.styles
84
 
 
85
 
        ctx['scripts'] = [media_url(req, CorePlugin, path) for path in
86
 
                           ('util.js', 'json2.js', 'md5.js')]
87
 
        ctx['scripts'].append(media_url(req, '+external/jquery', 'jquery.js'))
88
 
        ctx['scripts'] += req.scripts
89
 
 
90
 
        ctx['scripts_init'] = req.scripts_init
91
 
        ctx['app_template'] = app
92
 
        ctx['title_img'] = media_url(req, CorePlugin,
93
 
                                     "images/chrome/title.png")
94
 
        self.populate_headings(req, ctx)
95
 
        tmpl = loader.load(os.path.join(os.path.dirname(__file__), 
96
 
                                                        'ivle-headings.html'))
97
 
        req.write(tmpl.generate(ctx).render('xhtml', doctype='xhtml'))
98
 
        
99
 
    def populate(self, req, ctx):
100
 
        raise NotImplementedError()
101
 
 
102
 
    def populate_headings(self, req, ctx):
103
 
        ctx['favicon'] = None
104
 
        ctx['root_dir'] = ivle.conf.root_dir
105
 
        ctx['public_host'] = ivle.conf.public_host
106
 
        ctx['write_javascript_settings'] = req.write_javascript_settings
107
 
        if req.user:
108
 
            ctx['login'] = req.user.login
109
 
            ctx['logged_in'] = True
110
 
            ctx['nick'] = req.user.nick
111
 
        else:
112
 
            ctx['login'] = None
113
 
            ctx['logged_in'] = False
114
 
        ctx['publicmode'] = req.publicmode
115
 
        if hasattr(self, 'help'):
116
 
            ctx['help_path'] = self.help
117
 
 
118
 
        ctx['apps_in_tabs'] = []
119
 
        for plugin in req.config.plugin_index[ViewPlugin]:
120
 
            if not hasattr(plugin, 'tabs'):
121
 
                continue
122
 
 
123
 
            for tab in plugin.tabs:
124
 
                # tab is a tuple: name, title, desc, icon, path
125
 
                new_app = {}
126
 
                new_app['this_app'] = hasattr(self, 'tab') \
127
 
                                      and tab[0] == self.tab
128
 
 
129
 
                # Icon name
130
 
                if tab[3] is not None:
131
 
                    new_app['has_icon'] = True
132
 
                    icon_url = media_url(req, plugin, tab[3])
133
 
                    new_app['icon_url'] = icon_url
134
 
                    if new_app['this_app']:
135
 
                        ctx['favicon'] = icon_url
136
 
                else:
137
 
                    new_app['has_icon'] = False
138
 
                new_app['path'] = ivle.util.make_path(tab[4])
139
 
                new_app['desc'] = tab[2]
140
 
                new_app['name'] = tab[1]
141
 
                new_app['weight'] = tab[5]
142
 
                ctx['apps_in_tabs'].append(new_app)
143
 
 
144
 
        ctx['apps_in_tabs'].sort(key=lambda tab: tab['weight'])
145
 
 
146
 
    def render_overlays(self, req):
147
 
        """Generate XML streams for the overlays.
148
 
        
149
 
        Returns a list of streams. Populates the scripts, styles, and 
150
 
        scripts_init.
151
 
        """
152
 
        overlays = []
153
 
        if not self.allow_overlays:
154
 
            return overlays
155
 
 
156
 
        for plugin in req.config.plugin_index[OverlayPlugin]:
157
 
            for overclass in plugin.overlays:
158
 
                if overclass in self.overlay_blacklist:
159
 
                    continue
160
 
                overlay = overclass(req)
161
 
                #TODO: Re-factor this to look nicer
162
 
                for mplugin in overlay.plugin_scripts:
163
 
                    for path in overlay.plugin_scripts[mplugin]:
164
 
                        req.scripts.append(media_url(req, mplugin, path))
165
 
 
166
 
                for mplugin in overlay.plugin_styles:
167
 
                    for path in overlay.plugin_styles[mplugin]:
168
 
                        req.styles.append(media_url(req, mplugin, path))
169
 
 
170
 
                req.scripts_init += overlay.plugin_scripts_init
171
 
 
172
 
                overlays.append(overlay.render(req))
173
 
        return overlays
174
 
 
175
 
    @classmethod
176
 
    def get_error_view(cls, e):
177
 
        view_map = {HTTPError:    XHTMLErrorView,
178
 
                    Unauthorized: XHTMLUnauthorizedView}
179
 
        for exccls in inspect.getmro(type(e)):
180
 
            if exccls in view_map:
181
 
                return view_map[exccls]
182
 
 
183
 
class XHTMLErrorView(XHTMLView):
184
 
    template = 'xhtmlerror.html'
185
 
 
186
 
    def __init__(self, req, exception):
187
 
        self.context = exception
188
 
 
189
 
    def populate(self, req, ctx):
190
 
        ctx['exception'] = self.context
191
 
 
192
 
class XHTMLUnauthorizedView(XHTMLErrorView):
193
 
    template = 'xhtmlunauthorized.html'
194
 
 
195
 
    def __init__(self, req, exception):
196
 
        super(XHTMLUnauthorizedView, self).__init__(req, exception)
197
 
 
198
 
        if req.user is None:
199
 
            # Not logged in. Redirect to login page.
200
 
            if req.uri == '/':
201
 
                query_string = ''
202
 
            else:
203
 
                query_string = '?url=' + urllib.quote(req.uri, safe="/~")
204
 
            req.throw_redirect('/+login' + query_string)
205
 
 
206
 
        req.status = 403