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

« back to all changes in this revision

Viewing changes to ivle/dispatch/__init__.py

  • Committer: William Grant
  • Date: 2009-04-02 06:38:57 UTC
  • mto: (1165.3.1 submissions)
  • mto: This revision was merged to the branch mainline in revision 1174.
  • Revision ID: grantw@unimelb.edu.au-20090402063857-tka8rl8n3gnlo3rf
Expose SVN revision and URL information through fileservice.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
import routes
41
41
 
42
42
from ivle import util
43
 
import ivle.config
 
43
import ivle.conf
44
44
from ivle.dispatch.request import Request
45
45
import ivle.webapp.security
46
46
from ivle.webapp.base.plugins import ViewPlugin, PublicViewPlugin
47
47
from ivle.webapp.base.xhtml import XHTMLView, XHTMLErrorView
48
48
from ivle.webapp.errors import HTTPError, Unauthorized, NotFound
49
49
 
50
 
config = ivle.config.Config()
51
 
 
52
 
def generate_router(view_plugins, attr):
 
50
def generate_route_mapper(view_plugins, attr):
53
51
    """
54
52
    Build a Mapper object for doing URL matching using 'routes', based on the
55
53
    given plugin registry.
73
71
    @param apachereq: An Apache request object.
74
72
    """
75
73
    # Make the request object into an IVLE request which can be given to views
76
 
    req = Request(apachereq, config)
 
74
    req = Request(apachereq)
77
75
 
78
76
    # Hack? Try and get the user login early just in case we throw an error
79
77
    # (most likely 404) to stop us seeing not logged in even when we are.
84
82
        if user and user.valid:
85
83
            req.user = user
86
84
 
 
85
    conf = ivle.config.Config()
 
86
    req.config = conf
 
87
 
87
88
    if req.publicmode:
88
 
        req.mapper = generate_router(config.plugin_index[PublicViewPlugin],
89
 
                                    'public_urls')
 
89
        req.mapper = generate_route_mapper(conf.plugin_index[PublicViewPlugin],
 
90
                                           'public_urls')
90
91
    else:
91
 
        req.mapper = generate_router(config.plugin_index[ViewPlugin], 'urls')
 
92
        req.mapper = generate_route_mapper(conf.plugin_index[ViewPlugin],
 
93
                                           'urls')
92
94
 
93
95
    matchdict = req.mapper.match(req.uri)
94
96
    if matchdict is not None:
139
141
            req.store.commit()
140
142
            return req.OK
141
143
    else:
142
 
        req.status = 404
143
144
        XHTMLErrorView(req, NotFound()).render(req)
144
145
        return req.OK
145
146
 
154
155
    the IVLE request is created.
155
156
    """
156
157
    req.content_type = "text/html"
157
 
    logfile = os.path.join(config['paths']['logs'], 'ivle_error.log')
 
158
    logfile = os.path.join(ivle.conf.log_path, 'ivle_error.log')
158
159
    logfail = False
159
 
 
160
 
    # XXX: This remains here for ivle.interpret's IVLEErrors. Once we rewrite
161
 
    #      fileservice, req.status should always be 500 (ISE) here.
162
160
    try:
163
161
        httpcode = exc_value.httpcode
164
162
        req.status = httpcode
165
163
    except AttributeError:
166
164
        httpcode = None
167
165
        req.status = mod_python.apache.HTTP_INTERNAL_SERVER_ERROR
168
 
 
169
166
    try:
170
167
        publicmode = req.publicmode
171
168
    except AttributeError:
194
191
    # misbehaves (which is currently very easy, if things aren't set up
195
192
    # correctly).
196
193
    # Write the traceback.
197
 
 
198
 
    # We need to special-case IVLEJailError, as we can get another
 
194
    # If this is a non-4xx IVLEError, get the message and httpcode and
 
195
    # make the error message a bit nicer (but still include the
 
196
    # traceback).
 
197
    # We also need to special-case IVLEJailError, as we can get another
199
198
    # almost-exception out of it.
 
199
 
 
200
    codename, msg = None, None
 
201
 
200
202
    if exc_type is util.IVLEJailError:
 
203
        msg = exc_value.type_str + ": " + exc_value.message
201
204
        tb = 'Exception information extracted from IVLEJailError:\n'
202
205
        tb += urllib.unquote(exc_value.info)
203
206
    else:
 
207
        try:
 
208
            codename, msg = req.get_http_codename(httpcode)
 
209
        except AttributeError:
 
210
            pass
 
211
 
204
212
        tb = ''.join(traceback.format_exception(exc_type, exc_value,
205
213
                                                exc_traceback))
206
214
 
207
 
    logging.error('\n' + tb)
 
215
    logging.error('%s\n%s'%(str(msg), tb))
208
216
 
209
217
    # Error messages are only displayed is the user is NOT a student,
210
218
    # or if there has been a problem logging the error message
214
222
<html xmlns="http://www.w3.org/1999/xhtml">
215
223
<head><title>IVLE Internal Server Error</title></head>
216
224
<body>
217
 
<h1>IVLE Internal Server Error</h1>
 
225
<h1>IVLE Internal Server Error""")
 
226
    if show_errors:
 
227
        if codename is not None and \
 
228
           httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR:
 
229
            req.write(": %s" % cgi.escape(codename))
 
230
 
 
231
    req.write("""</h1>
218
232
<p>An error has occured which is the fault of the IVLE developers or
219
233
administrators. """)
220
234
 
226
240
    req.write("</p>")
227
241
 
228
242
    if show_errors:
 
243
        if msg is not None:
 
244
            req.write("<p>%s</p>\n" % cgi.escape(msg))
 
245
        if httpcode is not None:
 
246
            req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
229
247
        req.write("<h2>Debugging information</h2>")
 
248
 
230
249
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
231
250
    req.write("</body></html>")