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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: me at id
  • Date: 2009-01-14 05:24:16 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1125
doc/setup/ivle(-both).conf: Set PythonHandler to ivle.dispatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2009 The University of Melbourne
 
2
# Copyright (C) 2007-2008 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.request
18
19
# Author: Matt Giuca
19
 
 
20
 
"""
21
 
IVLE Request Object
22
 
 
23
 
Builds an IVLE request object from a mod_python request object.
24
 
See design notes/apps/dispatch.txt for a full specification of this request
25
 
object.
26
 
"""
27
 
 
28
 
try:
29
 
    import mod_python.Session
30
 
    import mod_python.Cookie
31
 
    import mod_python.util
32
 
except ImportError:
33
 
    # This needs to be importable from outside Apache.
34
 
    pass
 
20
# Date:   12/12/2007
 
21
 
 
22
# Builds an IVLE request object from a mod_python request object.
 
23
# See design notes/apps/dispatch.txt for a full specification of this request
 
24
# object.
 
25
 
 
26
import mod_python
 
27
from mod_python import (util, Session, Cookie)
35
28
 
36
29
import ivle.util
37
30
import ivle.conf
38
 
import ivle.database
39
31
import plugins.console # XXX: Relies on www/ being in the Python path.
40
32
 
41
33
class Request:
56
48
        user (read)
57
49
            User object. Details of the user who is currently logged in, or
58
50
            None.
59
 
        store (read)
60
 
            storm.store.Store instance. Holds a database transaction open,
61
 
            which is available for the entire lifetime of the request.
62
51
        hostname (read)
63
52
            String. Hostname the server is running on.
64
53
        headers_in (read)
203
192
        self.headers_in = req.headers_in
204
193
        self.headers_out = req.headers_out
205
194
 
206
 
        # Open a database connection and transaction, keep it around for users
207
 
        # of the Request object to use
208
 
        self.store = ivle.database.get_store()
209
 
 
210
195
        # Default values for the output members
211
196
        self.status = Request.HTTP_OK
212
197
        self.content_type = None        # Use Apache's default
221
206
        self.write_javascript_settings = True
222
207
        self.got_common_vars = False
223
208
 
224
 
    def __del__(self):
225
 
        """Cleanup."""
226
 
        self.store.close()
227
 
 
228
209
    def __writeheaders(self):
229
210
        """Writes out the HTTP and HTML headers before any real data is
230
211
        written."""
272
253
            # This includes binary strings.
273
254
            self.apache_req.write(string, flush)
274
255
 
275
 
    def logout(self):
276
 
        """Log out the current user by destroying the session state.
277
 
        Then redirect to the top-level IVLE page."""
278
 
        # List of cookies that IVLE uses (to be removed at logout)
279
 
        ivle_cookies = ["ivleforumcookie", "clipboard"]
280
 
        
281
 
        if hasattr(self, 'session'):
282
 
            self.session.invalidate()
283
 
            self.session.delete()
284
 
            # Invalidates all IVLE cookies
285
 
            all_cookies = mod_python.Cookie.get_cookies(self)
286
 
            for cookie in all_cookies:
287
 
                if cookie in ivle_cookies:
288
 
                    self.add_cookie(mod_python.Cookie.Cookie(cookie,'',expires=1,path='/'))
289
 
        self.throw_redirect(ivle.util.make_path('')) 
290
 
 
291
 
 
292
256
    def flush(self):
293
257
        """Flushes the output buffer."""
294
258
        self.apache_req.flush()
325
289
        httpcode: An HTTP response status code. Pass a constant from the
326
290
        Request class.
327
291
        """
328
 
        # Note: location may be a unicode, but it MUST only have ASCII
329
 
        # characters (non-ascii characters should be URL-encoded).
330
 
        mod_python.util.redirect(self.apache_req, location.encode("ascii"))
 
292
        mod_python.util.redirect(self.apache_req, location)
331
293
 
332
294
    def add_cookie(self, cookie, value=None, **attributes):
333
295
        """Inserts a cookie into this request object's headers."""
334
296
        if value is None:
335
 
            mod_python.Cookie.add_cookie(self.apache_req, cookie)
 
297
            Cookie.add_cookie(self.apache_req, cookie)
336
298
        else:
337
 
            mod_python.Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
 
299
            Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
338
300
 
339
301
    def get_session(self):
340
302
        """Returns a mod_python Session object for this request.
342
304
        interface if porting away from mod_python."""
343
305
        # Cache the session object and set the timeout to 24 hours.
344
306
        if not hasattr(self, 'session'):
345
 
            self.session = mod_python.Session.FileSession(self.apache_req,
 
307
            self.session = Session.FileSession(self.apache_req,
346
308
                                               timeout = 60 * 60 * 24)
347
309
        return self.session
348
310
 
352
314
        interface if porting away from mod_python."""
353
315
        # Cache the fieldstorage object
354
316
        if not hasattr(self, 'fields'):
355
 
            self.fields = mod_python.util.FieldStorage(self.apache_req)
 
317
            self.fields = util.FieldStorage(self.apache_req)
356
318
        return self.fields
357
319
 
358
320
    def get_cgi_environ(self):