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

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: mattgiuca
  • Date: 2008-02-29 01:18:22 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:621
Added 2 new apps: home and subjects. Both fairly incomplete, just a basic
    skeleton.
    "home" is the new default app, replacing "files".
    (It has a link to files).

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
import sys
33
33
import os
34
34
import os.path
35
 
import urllib
36
 
 
37
35
import conf
38
36
import conf.apps
39
 
import conf.conf
40
37
import apps
41
38
 
42
39
from request import Request
45
42
import login
46
43
from common import (util, forumutil)
47
44
import traceback
48
 
import plugins.console
49
 
import logging
50
 
import socket
 
45
import cStringIO
51
46
 
52
47
def handler(req):
53
48
    """Handles a request which may be to anywhere in the site except media.
84
79
    just used to catch exceptions.
85
80
    Takes both an IVLE request and an Apache req.
86
81
    """
87
 
    # Hack? Try and get the user login early just in case we throw an error
88
 
    # (most likely 404) to stop us seeing not logged in even when we are.
89
 
    if not req.publicmode:
90
 
        req.user = login.get_user_details(req)
91
 
 
92
82
    # Check req.app to see if it is valid. 404 if not.
93
83
    if req.app is not None and req.app not in conf.apps.app_url:
94
84
        # Maybe it is a special app!
98
88
            req.throw_error(Request.HTTP_NOT_FOUND,
99
89
                "There is no application called %s." % repr(req.app))
100
90
 
101
 
    # Special handling for public mode - only allow the public app, call it
102
 
    # and get out.
 
91
    # Special handling for public mode - just call public app and get out
103
92
    # NOTE: This will not behave correctly if the public app uses
104
93
    # write_html_head_foot, but "serve" does not.
105
94
    if req.publicmode:
106
 
        if req.app != conf.apps.public_app:
107
 
            req.throw_error(Request.HTTP_FORBIDDEN,
108
 
                "This application is not available on the public site.")
109
95
        app = conf.apps.app_url[conf.apps.public_app]
110
96
        apps.call_app(app.dir, req)
111
97
        return req.OK
158
144
 
159
145
    # When done, write out the HTML footer if the app has requested it
160
146
    if req.write_html_head_foot:
161
 
        # Show the console if required
162
 
        if logged_in and app.useconsole:
163
 
            plugins.console.present(req, windowpane=True)
164
147
        html.write_html_foot(req)
165
148
 
166
149
    # Note: Apache will not write custom HTML error messages here.
188
171
    the IVLE request is created.
189
172
    """
190
173
    req.content_type = "text/html"
191
 
    logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
192
 
    logfail = False
193
174
    # For some reason, some versions of mod_python have "_server" instead of
194
175
    # "main_server". So we check for both.
195
176
    try:
205
186
    except AttributeError:
206
187
        httpcode = None
207
188
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
208
 
    try:
209
 
        login = req.user.login
210
 
    except AttributeError:
211
 
        login = None
212
 
 
213
 
    # Log File
214
 
    try:
215
 
        logging.basicConfig(level=logging.INFO,
216
 
            format='%(asctime)s %(levelname)s: ' +
217
 
                '(HTTP: ' + str(req.status) +
218
 
                ', Ref: ' + str(login) + '@' +
219
 
                str(socket.gethostname()) + str(req.uri) +
220
 
                ') %(message)s',
221
 
            filename=logfile,
222
 
            filemode='a')
223
 
    except IOError:
224
 
        logfail = True
225
 
    logging.debug('Logging Unhandled Exception')
226
 
 
227
189
    # We handle 3 types of error.
228
190
    # IVLEErrors with 4xx response codes (client error).
229
191
    # IVLEErrors with 5xx response codes (handled server error).
237
199
        # IVLEErrors with 4xx response codes are client errors.
238
200
        # Therefore, these have a "nice" response (we even coat it in the IVLE
239
201
        # HTML wrappers).
240
 
        
241
202
        req.write_html_head_foot = True
242
203
        req.write('<div id="ivle_padding">\n')
243
204
        try:
256
217
            req.write("<p>%s</p>\n" % cgi.escape(msg))
257
218
        else:
258
219
            req.write("<p>An unknown error occured.</p>\n")
259
 
        
260
 
        # Logging
261
 
        logging.info(str(msg))
262
 
        
263
220
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
264
 
        if logfail:
265
 
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
266
 
                %cgi.escape(logfile))
267
221
        req.write('</div>\n')
268
222
    else:
269
223
        # A "bad" error message. We shouldn't get here unless IVLE
273
227
        # If this is a non-4xx IVLEError, get the message and httpcode and
274
228
        # make the error message a bit nicer (but still include the
275
229
        # traceback).
276
 
        # We also need to special-case IVLEJailError, as we can get another
277
 
        # almost-exception out of it.
278
 
 
279
 
        codename, msg = None, None
280
 
 
281
 
        if exc_type is util.IVLEJailError:
282
 
            msg = exc_value.type_str + ": " + exc_value.message
283
 
            tb = 'Exception information extracted from IVLEJailError:\n'
284
 
            tb += urllib.unquote(exc_value.info)
285
 
        else:
286
 
            try:
287
 
                codename, msg = req.get_http_codename(httpcode)
288
 
            except AttributeError:
289
 
                pass
290
 
            # Override the default message with the supplied one,
291
 
            # if available.
292
 
            if hasattr(exc_value, 'message') and exc_value.message is not None:
293
 
                msg = exc_value.message
294
 
                # Prepend the exception type
295
 
                if exc_type != util.IVLEError:
296
 
                    msg = exc_type.__name__ + ": " + msg
297
 
 
298
 
            tb = ''.join(traceback.format_exception(exc_type, exc_value,
299
 
                                                    exc_traceback))
300
 
 
301
 
        # Logging
302
 
        logging.error('%s\n%s'%(str(msg), tb))
 
230
        try:
 
231
            codename, msg = req.get_http_codename(httpcode)
 
232
        except AttributeError:
 
233
            codename, msg = None, None
 
234
        # Override the default message with the supplied one,
 
235
        # if available.
 
236
        if hasattr(exc_value, 'message') and exc_value.message is not None:
 
237
            msg = exc_value.message
 
238
            # Prepend the exception type
 
239
            if exc_type != util.IVLEError:
 
240
                msg = exc_type.__name__ + ": " + msg
303
241
 
304
242
        req.write("""<html>
305
243
<head><title>IVLE Internal Server Error</title></head>
321
259
administrator). Include the following information:</p>
322
260
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
323
261
 
324
 
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
325
 
        if logfail:
326
 
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
327
 
                %cgi.escape(logfile))
328
 
        req.write("</body>")
 
262
        tb_print = cStringIO.StringIO()
 
263
        traceback.print_exception(exc_type, exc_value, exc_traceback,
 
264
            file=tb_print)
 
265
        req.write("<pre>\n")
 
266
        req.write(cgi.escape(tb_print.getvalue()))
 
267
        req.write("</pre>\n</body>\n")