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

« back to all changes in this revision

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

  • Committer: drtomc
  • Date: 2008-02-01 04:13:23 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:369
Add stuff on installing and configuring pound.

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