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

« back to all changes in this revision

Viewing changes to ivle/dispatch/__init__.py

mip() the config dir when installing.

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
 
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):
 
47
from ivle.webapp.errors import HTTPError, Unauthorized
 
48
 
 
49
def generate_route_mapper(view_plugins, attr):
53
50
    """
54
51
    Build a Mapper object for doing URL matching using 'routes', based on the
55
52
    given plugin registry.
73
70
    @param apachereq: An Apache request object.
74
71
    """
75
72
    # Make the request object into an IVLE request which can be given to views
76
 
    req = Request(apachereq, config)
 
73
    req = Request(apachereq)
77
74
 
78
75
    # Hack? Try and get the user login early just in case we throw an error
79
76
    # (most likely 404) to stop us seeing not logged in even when we are.
84
81
        if user and user.valid:
85
82
            req.user = user
86
83
 
 
84
    conf = ivle.config.Config()
 
85
    req.config = conf
 
86
 
87
87
    if req.publicmode:
88
 
        req.mapper = generate_router(config.plugin_index[PublicViewPlugin],
89
 
                                    'public_urls')
 
88
        req.mapper = generate_route_mapper(conf.plugin_index[PublicViewPlugin],
 
89
                                           'public_urls')
90
90
    else:
91
 
        req.mapper = generate_router(config.plugin_index[ViewPlugin], 'urls')
 
91
        req.mapper = generate_route_mapper(conf.plugin_index[ViewPlugin],
 
92
                                           'urls')
92
93
 
93
94
    matchdict = req.mapper.match(req.uri)
94
95
    if matchdict is not None:
115
116
            if hasattr(viewcls, 'get_error_view'):
116
117
                errviewcls = viewcls.get_error_view(e)
117
118
            else:
118
 
                errviewcls = XHTMLView.get_error_view(e)
 
119
                errviewcls = None
119
120
 
120
121
            if errviewcls:
121
122
                errview = errviewcls(req, e)
126
127
                return req.OK
127
128
            else:
128
129
                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
134
130
        except Exception, e:
135
131
            # A non-HTTPError appeared. We have an unknown exception. Panic.
136
132
            handle_unknown_exception(req, *sys.exc_info())
139
135
            req.store.commit()
140
136
            return req.OK
141
137
    else:
142
 
        XHTMLErrorView(req, NotFound()).render(req)
143
 
        return req.OK
 
138
        return req.HTTP_NOT_FOUND # TODO: Prettify.
144
139
 
145
140
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
146
141
    """
153
148
    the IVLE request is created.
154
149
    """
155
150
    req.content_type = "text/html"
156
 
    logfile = os.path.join(config['paths']['logs'], 'ivle_error.log')
 
151
    logfile = os.path.join(ivle.conf.log_path, 'ivle_error.log')
157
152
    logfail = False
158
153
    try:
159
154
        httpcode = exc_value.httpcode
169
164
        login = req.user.login
170
165
    except AttributeError:
171
166
        login = None
 
167
    try:
 
168
        role = req.user.role
 
169
    except AttributeError:
 
170
        role = None
172
171
 
173
172
    # Log File
174
173
    try:
214
213
 
215
214
    # Error messages are only displayed is the user is NOT a student,
216
215
    # or if there has been a problem logging the error message
217
 
    show_errors = (not publicmode) and ((login and req.user.admin) or logfail)
 
216
    show_errors = (not publicmode) and ((login and \
 
217
                        str(role) != "student") or logfail)
218
218
    req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
219
219
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
220
220
<html xmlns="http://www.w3.org/1999/xhtml">
228
228
 
229
229
    req.write("""</h1>
230
230
<p>An error has occured which is the fault of the IVLE developers or
231
 
administrators. """)
232
 
 
233
 
    if logfail:
234
 
        req.write("Please report this issue to the server administrators, "
235
 
                  "along with the following information.")
236
 
    else:
237
 
        req.write("Details have been logged for further examination.")
238
 
    req.write("</p>")
239
 
 
 
231
administrators. Details have been logged for further examination.</p>
 
232
""")
240
233
    if show_errors:
241
234
        if msg is not None:
242
235
            req.write("<p>%s</p>\n" % cgi.escape(msg))
245
238
        req.write("<h2>Debugging information</h2>")
246
239
 
247
240
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
241
        if logfail:
 
242
            req.write("<p>Warning: Could not open error log.</p>\n"
 
243
                %cgi.escape(logfile))
248
244
    req.write("</body></html>")