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

« back to all changes in this revision

Viewing changes to ivle/dispatch/__init__.py

Add support in XHTMLView for plugin styles and scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE
2
 
# Copyright (C) 2007-2008 The University of Melbourne
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2009 The University of Melbourne
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
17
 
18
 
# Module: dispatch
19
 
# Author: Matt Giuca
20
 
# Date: 11/12/2007
21
 
 
22
 
# This is a mod_python handler program. The correct way to call it is to have
23
 
# Apache send all requests to be handled by the module 'dispatch'.
24
 
 
25
 
# Top-level handler. Handles all requests to all pages in IVLE.
26
 
# Handles authentication (not authorization).
27
 
# Then passes the request along to the appropriate ivle app.
 
18
# Author: Matt Giuca, Will Grant
 
19
 
 
20
"""
 
21
This is a mod_python handler program. The correct way to call it is to have
 
22
Apache send all requests to be handled by the module 'dispatch'.
 
23
 
 
24
Top-level handler. Handles all requests to all pages in IVLE.
 
25
Handles authentication (not authorization).
 
26
Then passes the request along to the appropriate ivle app.
 
27
"""
28
28
 
29
29
import sys
30
30
import os
37
37
import time
38
38
 
39
39
import mod_python
40
 
from mod_python import apache, Cookie
 
40
import routes
41
41
 
42
42
from ivle import util
43
43
import ivle.conf
44
44
import ivle.conf.apps
 
45
from ivle.dispatch.request import Request
 
46
from ivle.dispatch import login
45
47
import apps
46
 
import login
47
48
import html
48
 
from request import Request
49
49
import plugins.console # XXX: Relies on www/ being in the Python path.
50
50
 
 
51
# XXX List of plugins, which will eventually be read in from conf
 
52
plugins_HACK = [
 
53
    'ivle.webapp.admin.user#Plugin',
 
54
    'ivle.webapp.tutorial#Plugin',
 
55
    'ivle.webapp.admin.subject#Plugin',
 
56
    'ivle.webapp.filesystem.browser#Plugin',
 
57
    'ivle.webapp.filesystem.diff#Plugin',
 
58
    'ivle.webapp.filesystem.svnlog#Plugin',
 
59
    'ivle.webapp.groups#Plugin',
 
60
    'ivle.webapp.console#Plugin',
 
61
    'ivle.webapp.security#Plugin',
 
62
    'ivle.webapp.media#Plugin',
 
63
]
 
64
 
 
65
def generate_route_mapper(plugins):
 
66
    """
 
67
    Build a Mapper object for doing URL matching using 'routes', based on the
 
68
    given plugin registry.
 
69
    """
 
70
    m = routes.Mapper(explicit=True)
 
71
    for name in plugins:
 
72
        # Establish a URL pattern for each element of plugin.urls
 
73
        if not hasattr(plugins[name], 'urls'):
 
74
            continue
 
75
        for url in plugins[name].urls:
 
76
            routex = url[0]
 
77
            view_class = url[1]
 
78
            kwargs_dict = url[2] if len(url) >= 3 else {}
 
79
            m.connect(routex, view=view_class, **kwargs_dict)
 
80
    return m
 
81
 
 
82
def get_plugin(pluginstr):
 
83
    plugin_path, classname = pluginstr.split('#')
 
84
    # Load the plugin module from somewhere in the Python path
 
85
    # (Note that plugin_path is a fully-qualified Python module name).
 
86
    return (plugin_path,
 
87
            getattr(__import__(plugin_path, fromlist=[classname]), classname))
 
88
 
51
89
def handler(req):
52
90
    """Handles a request which may be to anywhere in the site except media.
53
91
    Intended to be called by mod_python, as a handler.
63
101
        # yet.
64
102
        handle_unknown_exception(apachereq, *sys.exc_info())
65
103
        # Tell Apache not to generate its own errors as well
66
 
        return apache.OK
 
104
        return mod_python.apache.OK
67
105
 
68
106
    # Run the main handler, and catch all exceptions
69
107
    try:
75
113
    except Exception:
76
114
        handle_unknown_exception(req, *sys.exc_info())
77
115
        # Tell Apache not to generate its own errors as well
78
 
        return apache.OK
 
116
        return mod_python.apache.OK
79
117
 
80
118
def handler_(req, apachereq):
81
119
    """
88
126
    if not req.publicmode:
89
127
        req.user = login.get_user_details(req)
90
128
 
 
129
    ### BEGIN New plugins framework ###
 
130
    # XXX This should be done ONCE per Python process, not per request.
 
131
    # (Wait till WSGI)
 
132
    # XXX No authentication is done here
 
133
    req.plugins = dict([get_plugin(pluginstr) for pluginstr in plugins_HACK])
 
134
    req.reverse_plugins = dict([(v, k) for (k, v) in req.plugins.items()])
 
135
    req.mapper = generate_route_mapper(req.plugins)
 
136
 
 
137
    matchdict = req.mapper.match(req.uri)
 
138
    if matchdict is not None:
 
139
        viewcls = matchdict['view']
 
140
        # Get the remaining arguments, less 'view', 'action' and 'controller'
 
141
        # (The latter two seem to be built-in, and we don't want them).
 
142
        kwargs = matchdict.copy()
 
143
        del kwargs['view']
 
144
        # Instantiate the view, which should be a BaseView class
 
145
        view = viewcls(req, **kwargs)
 
146
        # Render the output
 
147
        view.render(req)
 
148
        req.store.commit()
 
149
        return req.OK
 
150
    ### END New plugins framework ###
 
151
 
91
152
    # Check req.app to see if it is valid. 404 if not.
92
153
    if req.app is not None and req.app not in ivle.conf.apps.app_url:
93
154
        req.throw_error(Request.HTTP_NOT_FOUND,
180
241
    # For some reason, some versions of mod_python have "_server" instead of
181
242
    # "main_server". So we check for both.
182
243
    try:
183
 
        admin_email = apache.main_server.server_admin
 
244
        admin_email = mod_python.apache.main_server.server_admin
184
245
    except AttributeError:
185
246
        try:
186
 
            admin_email = apache._server.server_admin
 
247
            admin_email = mod_python.apache._server.server_admin
187
248
        except AttributeError:
188
249
            admin_email = ""
189
250
    try:
191
252
        req.status = httpcode
192
253
    except AttributeError:
193
254
        httpcode = None
194
 
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
 
255
        req.status = mod_python.apache.HTTP_INTERNAL_SERVER_ERROR
 
256
    try:
 
257
        publicmode = req.publicmode
 
258
    except AttributeError:
 
259
        publicmode = True
195
260
    try:
196
261
        login = req.user.login
197
262
    except AttributeError:
198
263
        login = None
 
264
    try:
 
265
        role = req.user.role
 
266
    except AttributeError:
 
267
        role = None
199
268
 
200
269
    # Log File
201
270
    try:
291
360
 
292
361
        # Logging
293
362
        logging.error('%s\n%s'%(str(msg), tb))
294
 
 
 
363
        # Error messages are only displayed is the user is NOT a student,
 
364
        # or if there has been a problem logging the error message
 
365
        show_errors = (not publicmode) and ((login and \
 
366
                            str(role) != "student") or logfail)
295
367
        req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
296
368
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
297
369
<html xmlns="http://www.w3.org/1999/xhtml">
298
370
<head><title>IVLE Internal Server Error</title></head>
299
371
<body>
300
372
<h1>IVLE Internal Server Error""")
301
 
        if (codename is not None
302
 
            and httpcode != apache.HTTP_INTERNAL_SERVER_ERROR):
303
 
            req.write(": %s" % cgi.escape(codename))
 
373
        if (show_errors):
 
374
            if (codename is not None
 
375
                        and httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR):
 
376
                req.write(": %s" % cgi.escape(codename))
 
377
        
304
378
        req.write("""</h1>
305
379
<p>An error has occured which is the fault of the IVLE developers or
306
 
administration.</p>
 
380
administration. The developers have been notified.</p>
307
381
""")
308
 
        if msg is not None:
309
 
            req.write("<p>%s</p>\n" % cgi.escape(msg))
310
 
        if httpcode is not None:
311
 
            req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
312
 
        req.write("""
313
 
<p>Please report this to <a href="mailto:%s">%s</a> (the system
314
 
administrator). Include the following information:</p>
315
 
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
 
382
        if (show_errors):
 
383
            if msg is not None:
 
384
                req.write("<p>%s</p>\n" % cgi.escape(msg))
 
385
            if httpcode is not None:
 
386
                req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
 
387
            req.write("""
 
388
    <p>Please report this to <a href="mailto:%s">%s</a> (the system
 
389
    administrator). Include the following information:</p>
 
390
    """ % (cgi.escape(admin_email), cgi.escape(admin_email)))
316
391
 
317
 
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
318
 
        if logfail:
319
 
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
320
 
                %cgi.escape(logfile))
 
392
            req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
393
            if logfail:
 
394
                req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
395
                    %cgi.escape(logfile))
321
396
        req.write("</body></html>")