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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: Matt Giuca
  • Date: 2010-03-22 06:05:32 UTC
  • Revision ID: matt.giuca@gmail.com-20100322060532-5365361xrx9mh32v
Changed database.py get_svn_url to take a req; include the req.user.login in the Subversion URL. This allows you to check out repositories without separately supplying the IVLE URL (as Subversion won't ask for a username by default). Also removed --username= from the lecturer project view, as it's redundant now. This fixes Launchpad bug #543936.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    import mod_python.Session
30
30
    import mod_python.Cookie
31
31
    import mod_python.util
 
32
    import mod_python.apache
 
33
 
 
34
    class PotentiallySecureFileSession(mod_python.Session.FileSession):
 
35
        """A mod_python FileSession that sets secure cookie when appropriate.
 
36
 
 
37
        A secure cookie will be set if the request itself is over HTTPS, or if
 
38
        a proxy in front has set X-Forwarded-Proto: https. Otherwise the cookie
 
39
        will be insecure.
 
40
        """
 
41
        def make_cookie(self):
 
42
            cookie = super(PotentiallySecureFileSession, self).make_cookie()
 
43
            if (self._req.is_https() or
 
44
                self._req.headers_in.get('X-Forwarded-Proto') == 'https'):
 
45
                cookie.secure = True
 
46
            return cookie
32
47
except ImportError:
33
48
    # This needs to be importable from outside Apache.
34
49
    pass
38
53
import ivle.util
39
54
import ivle.database
40
55
from ivle.webapp.base.plugins import CookiePlugin
 
56
import ivle.webapp.security
 
57
 
41
58
 
42
59
class Request:
43
60
    """An IVLE request object. This is presented to the IVLE apps as a way of
94
111
    HTTP_NOT_FOUND                    = 404
95
112
    HTTP_INTERNAL_SERVER_ERROR        = 500
96
113
 
 
114
    _store = None
 
115
 
97
116
    def __init__(self, req, config):
98
117
        """Create an IVLE request from a mod_python one.
99
118
 
119
138
        # Split the given path into the app (top-level dir) and sub-path
120
139
        # (after first stripping away the root directory)
121
140
        (self.app, self.path) = (ivle.util.split_path(req.uri))
122
 
        self.user = None
123
141
        self.hostname = req.hostname
124
142
        self.headers_in = req.headers_in
125
143
        self.headers_out = req.headers_out
126
144
 
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
145
        # Default values for the output members
132
146
        self.status = Request.HTTP_OK
133
147
        self.content_type = None        # Use Apache's default
138
152
        self.got_common_vars = False
139
153
 
140
154
    def __del__(self):
141
 
        """Cleanup."""
142
 
        self.store.close()
 
155
        self.cleanup()
 
156
 
 
157
    def cleanup(self):
 
158
        """Cleanup."""
 
159
        if self._store is not None:
 
160
            self._store.close()
 
161
            self._store = None
 
162
 
 
163
    def commit(self):
 
164
        """Cleanup."""
 
165
        if self._store is not None:
 
166
            self._store.commit()
143
167
 
144
168
    def __writeheaders(self):
145
169
        """Writes out the HTTP and HTML headers before any real data is
247
271
        """
248
272
        # Cache the session object and set the timeout to 24 hours.
249
273
        if not hasattr(self, 'session'):
250
 
            self.session = mod_python.Session.FileSession(self.apache_req,
251
 
                                               timeout = 60 * 60 * 24)
 
274
            self.session = PotentiallySecureFileSession(
 
275
                self.apache_req, timeout = 60 * 60 * 24)
252
276
        return self.session
253
277
 
254
278
    def get_fieldstorage(self):
269
293
            self.got_common_vars = True
270
294
        return self.apache_req.subprocess_env
271
295
 
 
296
    @property
 
297
    def store(self):
 
298
        # Open a database connection and transaction, keep it around for users
 
299
        # of the Request object to use.
 
300
        if self._store is None:
 
301
            self._store = ivle.database.get_store(self.config)
 
302
        return self._store
 
303
 
 
304
    @property
 
305
    def user(self):
 
306
        # Get and cache the request user, or None if it's not valid.
 
307
        # This is a property so that we don't create a store unless
 
308
        # some code actually requests the user.
 
309
        try:
 
310
            return self._user
 
311
        except AttributeError:
 
312
            if self.publicmode:
 
313
                self._user = None
 
314
            else:
 
315
                temp_user = ivle.webapp.security.get_user_details(self)
 
316
                if temp_user and temp_user.valid:
 
317
                    self._user = temp_user
 
318
                else:
 
319
                    self._user = None
 
320
            return self._user
 
321