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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: matt.giuca
  • Date: 2009-01-19 10:34:45 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1169
Changed svn:ignore on trunk - no longer need to ignore bin or svn directories
    (bin is in repo, svn is gone).
Added .bzrignore, for Bazaar branches of this repository.

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
 
from ivle.webapp.base.plugins import CookiePlugin
 
31
import plugins.console # XXX: Relies on www/ being in the Python path.
40
32
 
41
33
class Request:
42
34
    """An IVLE request object. This is presented to the IVLE apps as a way of
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."""
236
217
        except KeyError:
237
218
            app = None
238
219
 
 
220
        # Write any final modifications to header content
 
221
        if app and app.useconsole and self.user:
 
222
            plugins.console.insert_scripts_styles(self.scripts, self.styles, \
 
223
                self.scripts_init)
 
224
 
239
225
        # Prepare the HTTP and HTML headers before the first write is made
240
226
        if self.content_type != None:
241
227
            self.apache_req.content_type = self.content_type
270
256
    def logout(self):
271
257
        """Log out the current user by destroying the session state.
272
258
        Then redirect to the top-level IVLE page."""
 
259
        # List of cookies that IVLE uses (to be removed at logout)
 
260
        ivle_cookies = ["ivleforumcookie", "clipboard"]
 
261
        
273
262
        if hasattr(self, 'session'):
274
263
            self.session.invalidate()
275
264
            self.session.delete()
276
265
            # Invalidates all IVLE cookies
277
 
            all_cookies = mod_python.Cookie.get_cookies(self)
278
 
 
279
 
            # Create cookies for plugins that might request them.
280
 
            for plugin in self.plugin_index[CookiePlugin]:
281
 
                for cookie in plugin.cookies:
282
 
                    self.add_cookie(mod_python.Cookie.Cookie(cookie, '',
283
 
                                                    expires=1, path='/'))
 
266
            all_cookies = Cookie.get_cookies(self)
 
267
            for cookie in all_cookies:
 
268
                if cookie in ivle_cookies:
 
269
                    self.add_cookie(Cookie.Cookie(cookie,'',expires=1,path='/'))
284
270
        self.throw_redirect(ivle.util.make_path('')) 
285
271
 
286
272
 
320
306
        httpcode: An HTTP response status code. Pass a constant from the
321
307
        Request class.
322
308
        """
323
 
        # Note: location may be a unicode, but it MUST only have ASCII
324
 
        # characters (non-ascii characters should be URL-encoded).
325
 
        mod_python.util.redirect(self.apache_req, location.encode("ascii"))
 
309
        mod_python.util.redirect(self.apache_req, location)
326
310
 
327
311
    def add_cookie(self, cookie, value=None, **attributes):
328
312
        """Inserts a cookie into this request object's headers."""
329
313
        if value is None:
330
 
            mod_python.Cookie.add_cookie(self.apache_req, cookie)
 
314
            Cookie.add_cookie(self.apache_req, cookie)
331
315
        else:
332
 
            mod_python.Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
 
316
            Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
333
317
 
334
318
    def get_session(self):
335
319
        """Returns a mod_python Session object for this request.
337
321
        interface if porting away from mod_python."""
338
322
        # Cache the session object and set the timeout to 24 hours.
339
323
        if not hasattr(self, 'session'):
340
 
            self.session = mod_python.Session.FileSession(self.apache_req,
 
324
            self.session = Session.FileSession(self.apache_req,
341
325
                                               timeout = 60 * 60 * 24)
342
326
        return self.session
343
327
 
347
331
        interface if porting away from mod_python."""
348
332
        # Cache the fieldstorage object
349
333
        if not hasattr(self, 'fields'):
350
 
            self.fields = mod_python.util.FieldStorage(self.apache_req)
 
334
            self.fields = util.FieldStorage(self.apache_req)
351
335
        return self.fields
352
336
 
353
337
    def get_cgi_environ(self):