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

« back to all changes in this revision

Viewing changes to ivle/dispatch/__init__.py

Began implementing new dispatch framework (with Will Grant and Nick Chadwick).
Added package: ivle.webapp. This contains 'base', with some base class
    implementations for the new Plugins and Views, and 'admin', with 'user'
    (part of 'userservice') re-implemented for the new framework in a RESTful
    way.
dispatch: Added code to partly handle the new plugin system, then fall back to
    the old one.

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
38
38
 
39
39
import mod_python
40
40
from mod_python import apache, Cookie
 
41
import routes
41
42
 
42
43
from ivle import util
43
44
import ivle.conf
48
49
from request import Request
49
50
import plugins.console # XXX: Relies on www/ being in the Python path.
50
51
 
 
52
# XXX List of plugins, which will eventually be read in from conf
 
53
# Elements are (module, classname) pairs.
 
54
plugins_HACK = [
 
55
    ('ivle.webapp.admin.user', 'Plugin'),
 
56
]
 
57
 
 
58
def get_routes_mapper():
 
59
    """
 
60
    Build a Mapper object for doing URL matching using 'routes', based on the
 
61
    plugins config.
 
62
    """
 
63
    m = routes.Mapper()
 
64
    for plugin_path, classname in plugins_HACK:
 
65
        # Load the plugin module from somewhere in the Python path
 
66
        # (Note that plugin_path is a fully-qualified Python module name).
 
67
        plugin = getattr(__import__(plugin_path, fromlist=[classname]),
 
68
            classname)
 
69
        # Establish a URL pattern for each element of plugin.urls
 
70
        for url in plugin.urls:
 
71
            routex = url[0]
 
72
            view_class = url[1]
 
73
            kwargs_dict = url[2] if len(url) >= 3 else {}
 
74
            m.connect(routex, view=view_class, **kwargs_dict)
 
75
    return m
 
76
 
51
77
def handler(req):
52
78
    """Handles a request which may be to anywhere in the site except media.
53
79
    Intended to be called by mod_python, as a handler.
88
114
    if not req.publicmode:
89
115
        req.user = login.get_user_details(req)
90
116
 
 
117
    ### BEGIN New plugins framework ###
 
118
    # XXX This should be done ONCE per Python process, not per request.
 
119
    # (Wait till WSGI)
 
120
    # XXX No authentication is done here
 
121
    mapper = get_routes_mapper()
 
122
    matchdict = mapper.match(req.uri)
 
123
    if matchdict is not None:
 
124
        viewcls = matchdict['view']
 
125
        # Get the remaining arguments, less 'view', 'action' and 'controller'
 
126
        # (The latter two seem to be built-in, and we don't want them).
 
127
        kwargs = matchdict.copy()
 
128
        del kwargs['view']
 
129
        del kwargs['action']
 
130
        del kwargs['controller']
 
131
        # Instantiate the view, which should be a BaseView class
 
132
        view = viewcls(req, **kwargs)
 
133
        # Render the output
 
134
        view.render(req)
 
135
        req.store.commit()
 
136
        return req.OK
 
137
    ### END New plugins framework ###
 
138
 
91
139
    # Check req.app to see if it is valid. 404 if not.
92
140
    if req.app is not None and req.app not in ivle.conf.apps.app_url:
93
141
        req.throw_error(Request.HTTP_NOT_FOUND,
193
241
        httpcode = None
194
242
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
195
243
    try:
 
244
        publicmode = req.publicmode
 
245
    except AttributeError:
 
246
        publicmode = True
 
247
    try:
196
248
        login = req.user.login
197
249
    except AttributeError:
198
250
        login = None
 
251
    try:
 
252
        role = req.user.role
 
253
    except AttributeError:
 
254
        role = None
199
255
 
200
256
    # Log File
201
257
    try:
291
347
 
292
348
        # Logging
293
349
        logging.error('%s\n%s'%(str(msg), tb))
294
 
 
 
350
        # Error messages are only displayed is the user is NOT a student,
 
351
        # or if there has been a problem logging the error message
 
352
        show_errors = (not publicmode) and ((login and \
 
353
                            str(role) != "student") or logfail)
295
354
        req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
296
355
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
297
356
<html xmlns="http://www.w3.org/1999/xhtml">
298
357
<head><title>IVLE Internal Server Error</title></head>
299
358
<body>
300
359
<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))
 
360
        if (show_errors):
 
361
            if (codename is not None
 
362
                        and httpcode != apache.HTTP_INTERNAL_SERVER_ERROR):
 
363
                req.write(": %s" % cgi.escape(codename))
 
364
        
304
365
        req.write("""</h1>
305
366
<p>An error has occured which is the fault of the IVLE developers or
306
 
administration.</p>
 
367
administration. The developers have been notified.</p>
307
368
""")
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)))
 
369
        if (show_errors):
 
370
            if msg is not None:
 
371
                req.write("<p>%s</p>\n" % cgi.escape(msg))
 
372
            if httpcode is not None:
 
373
                req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
 
374
            req.write("""
 
375
    <p>Please report this to <a href="mailto:%s">%s</a> (the system
 
376
    administrator). Include the following information:</p>
 
377
    """ % (cgi.escape(admin_email), cgi.escape(admin_email)))
316
378
 
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))
 
379
            req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
380
            if logfail:
 
381
                req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
382
                    %cgi.escape(logfile))
321
383
        req.write("</body></html>")