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

« back to all changes in this revision

Viewing changes to ivle/dispatch/__init__.py

  • Committer: David Coles
  • Date: 2009-07-21 02:19:56 UTC
  • mto: (1281.1.8 aufsless)
  • mto: This revision was merged to the branch mainline in revision 1300.
  • Revision ID: coles.david@gmail.com-20090721021956-c1jiwu7fhi2dna1g
Updated to work on bind mounts

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.conf
 
43
import ivle.config
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
 
from ivle.webapp.errors import HTTPError, Unauthorized
48
 
 
49
 
def generate_route_mapper(view_plugins, attr):
 
47
from ivle.webapp.base.xhtml import XHTMLView, XHTMLErrorView
 
48
from ivle.webapp.errors import HTTPError, Unauthorized, NotFound
 
49
 
 
50
config = ivle.config.Config()
 
51
 
 
52
def generate_router(view_plugins, attr):
50
53
    """
51
54
    Build a Mapper object for doing URL matching using 'routes', based on the
52
55
    given plugin registry.
70
73
    @param apachereq: An Apache request object.
71
74
    """
72
75
    # Make the request object into an IVLE request which can be given to views
73
 
    req = Request(apachereq)
 
76
    req = Request(apachereq, config)
74
77
 
75
78
    # Hack? Try and get the user login early just in case we throw an error
76
79
    # (most likely 404) to stop us seeing not logged in even when we are.
81
84
        if user and user.valid:
82
85
            req.user = user
83
86
 
84
 
    conf = ivle.config.Config()
85
 
    req.config = conf
86
 
 
87
87
    if req.publicmode:
88
 
        req.mapper = generate_route_mapper(conf.plugin_index[PublicViewPlugin],
89
 
                                           'public_urls')
 
88
        req.mapper = generate_router(config.plugin_index[PublicViewPlugin],
 
89
                                    'public_urls')
90
90
    else:
91
 
        req.mapper = generate_route_mapper(conf.plugin_index[ViewPlugin],
92
 
                                           'urls')
 
91
        req.mapper = generate_router(config.plugin_index[ViewPlugin], 'urls')
93
92
 
94
93
    matchdict = req.mapper.match(req.uri)
95
94
    if matchdict is not None:
116
115
            if hasattr(viewcls, 'get_error_view'):
117
116
                errviewcls = viewcls.get_error_view(e)
118
117
            else:
119
 
                errviewcls = None
 
118
                errviewcls = XHTMLView.get_error_view(e)
120
119
 
121
120
            if errviewcls:
122
121
                errview = errviewcls(req, e)
127
126
                return req.OK
128
127
            else:
129
128
                return e.code
 
129
        except mod_python.apache.SERVER_RETURN:
 
130
            # A mod_python-specific Apache error.
 
131
            # XXX: We need to raise these because req.throw_error() uses them.
 
132
            # Remove this after Google Code issue 117 is fixed.
 
133
            raise
130
134
        except Exception, e:
131
135
            # A non-HTTPError appeared. We have an unknown exception. Panic.
132
136
            handle_unknown_exception(req, *sys.exc_info())
135
139
            req.store.commit()
136
140
            return req.OK
137
141
    else:
138
 
        return req.HTTP_NOT_FOUND # TODO: Prettify.
 
142
        XHTMLErrorView(req, NotFound()).render(req)
 
143
        return req.OK
139
144
 
140
145
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
141
146
    """
148
153
    the IVLE request is created.
149
154
    """
150
155
    req.content_type = "text/html"
151
 
    logfile = os.path.join(ivle.conf.log_path, 'ivle_error.log')
 
156
    logfile = os.path.join(config['paths']['logs'], 'ivle_error.log')
152
157
    logfail = False
153
 
    try:
154
 
        httpcode = exc_value.httpcode
155
 
        req.status = httpcode
156
 
    except AttributeError:
157
 
        httpcode = None
158
 
        req.status = mod_python.apache.HTTP_INTERNAL_SERVER_ERROR
 
158
    req.status = mod_python.apache.HTTP_INTERNAL_SERVER_ERROR
 
159
 
159
160
    try:
160
161
        publicmode = req.publicmode
161
162
    except AttributeError:
164
165
        login = req.user.login
165
166
    except AttributeError:
166
167
        login = None
167
 
    try:
168
 
        role = req.user.role
169
 
    except AttributeError:
170
 
        role = None
171
168
 
172
169
    # Log File
173
170
    try:
188
185
    # misbehaves (which is currently very easy, if things aren't set up
189
186
    # correctly).
190
187
    # Write the traceback.
191
 
    # If this is a non-4xx IVLEError, get the message and httpcode and
192
 
    # make the error message a bit nicer (but still include the
193
 
    # traceback).
194
 
    # We also need to special-case IVLEJailError, as we can get another
 
188
 
 
189
    # We need to special-case IVLEJailError, as we can get another
195
190
    # almost-exception out of it.
196
 
 
197
 
    codename, msg = None, None
198
 
 
199
191
    if exc_type is util.IVLEJailError:
200
 
        msg = exc_value.type_str + ": " + exc_value.message
201
192
        tb = 'Exception information extracted from IVLEJailError:\n'
202
193
        tb += urllib.unquote(exc_value.info)
203
194
    else:
204
 
        try:
205
 
            codename, msg = req.get_http_codename(httpcode)
206
 
        except AttributeError:
207
 
            pass
208
 
 
209
195
        tb = ''.join(traceback.format_exception(exc_type, exc_value,
210
196
                                                exc_traceback))
211
197
 
212
 
    logging.error('%s\n%s'%(str(msg), tb))
 
198
    logging.error('\n' + tb)
213
199
 
214
200
    # Error messages are only displayed is the user is NOT a student,
215
201
    # or if there has been a problem logging the error message
216
 
    show_errors = (not publicmode) and ((login and \
217
 
                        str(role) != "student") or logfail)
 
202
    show_errors = (not publicmode) and ((login and req.user.admin) or logfail)
218
203
    req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
219
204
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
220
205
<html xmlns="http://www.w3.org/1999/xhtml">
221
206
<head><title>IVLE Internal Server Error</title></head>
222
207
<body>
223
 
<h1>IVLE Internal Server Error""")
224
 
    if show_errors:
225
 
        if codename is not None and \
226
 
           httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR:
227
 
            req.write(": %s" % cgi.escape(codename))
228
 
 
229
 
    req.write("""</h1>
 
208
<h1>IVLE Internal Server Error</h1>
230
209
<p>An error has occured which is the fault of the IVLE developers or
231
210
administrators. """)
232
211
 
238
217
    req.write("</p>")
239
218
 
240
219
    if show_errors:
241
 
        if msg is not None:
242
 
            req.write("<p>%s</p>\n" % cgi.escape(msg))
243
 
        if httpcode is not None:
244
 
            req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
245
220
        req.write("<h2>Debugging information</h2>")
246
 
 
247
221
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
248
222
    req.write("</body></html>")