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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: William Grant
  • Date: 2010-02-25 03:18:21 UTC
  • Revision ID: grantw@unimelb.edu.au-20100225031821-mi9a2tm5679fht4d
Shuffle things around so that req.user and req.store only construct when actually retrieved, and ensure they're not retrieved for media files. Saves 50ms of DB connection time per request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
import ivle.util
39
39
import ivle.database
40
40
from ivle.webapp.base.plugins import CookiePlugin
 
41
import ivle.webapp.security
41
42
 
42
43
class Request:
43
44
    """An IVLE request object. This is presented to the IVLE apps as a way of
94
95
    HTTP_NOT_FOUND                    = 404
95
96
    HTTP_INTERNAL_SERVER_ERROR        = 500
96
97
 
 
98
    _store = None
 
99
 
97
100
    def __init__(self, req, config):
98
101
        """Create an IVLE request from a mod_python one.
99
102
 
119
122
        # Split the given path into the app (top-level dir) and sub-path
120
123
        # (after first stripping away the root directory)
121
124
        (self.app, self.path) = (ivle.util.split_path(req.uri))
122
 
        self.user = None
123
125
        self.hostname = req.hostname
124
126
        self.headers_in = req.headers_in
125
127
        self.headers_out = req.headers_out
126
128
 
127
 
        # Open a database connection and transaction, keep it around for users
128
 
        # of the Request object to use
129
 
        self.store = ivle.database.get_store(config)
130
 
 
131
129
        # Default values for the output members
132
130
        self.status = Request.HTTP_OK
133
131
        self.content_type = None        # Use Apache's default
138
136
        self.got_common_vars = False
139
137
 
140
138
    def __del__(self):
141
 
        """Cleanup."""
142
 
        self.store.close()
 
139
        self.cleanup()
 
140
 
 
141
    def cleanup(self):
 
142
        """Cleanup."""
 
143
        if self._store is not None:
 
144
            self._store.close()
 
145
            self._store = None
 
146
 
 
147
    def commit(self):
 
148
        """Cleanup."""
 
149
        if self._store is not None:
 
150
            self._store.commit()
143
151
 
144
152
    def __writeheaders(self):
145
153
        """Writes out the HTTP and HTML headers before any real data is
269
277
            self.got_common_vars = True
270
278
        return self.apache_req.subprocess_env
271
279
 
 
280
    @property
 
281
    def store(self):
 
282
        # Open a database connection and transaction, keep it around for users
 
283
        # of the Request object to use.
 
284
        if self._store is None:
 
285
            self._store = ivle.database.get_store(self.config)
 
286
        return self._store
 
287
 
 
288
    @property
 
289
    def user(self):
 
290
        # Get and cache the request user, or None if it's not valid.
 
291
        # This is a property so that we don't create a store unless
 
292
        # some code actually requests the user.
 
293
        try:
 
294
            return self._user
 
295
        except AttributeError:
 
296
            if self.publicmode:
 
297
                self._user = None
 
298
            else:
 
299
                temp_user = ivle.webapp.security.get_user_details(self)
 
300
                if temp_user and temp_user.valid:
 
301
                    self._user = temp_user
 
302
                else:
 
303
                    self._user = None
 
304
            return self._user
 
305