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

« back to all changes in this revision

Viewing changes to ivle/dispatch/request.py

  • Committer: William Grant
  • Date: 2009-02-24 13:29:44 UTC
  • Revision ID: grantw@unimelb.edu.au-20090224132944-lm29zd1li1rjg77p
Privileges (apart from admin) are now offering-local, not global.

Offering privileges are granted by Enrolment.role, and global admin
by User.admin. ivle.caps is dead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    # This needs to be importable from outside Apache.
34
34
    pass
35
35
 
36
 
import os.path
37
 
 
38
36
import ivle.util
 
37
import ivle.conf
39
38
import ivle.database
40
39
from ivle.webapp.base.plugins import CookiePlugin
41
40
 
79
78
        location (write)
80
79
            String. Response "Location" header value. Used with HTTP redirect
81
80
            responses.
 
81
        styles (write)
 
82
            List of strings. Write a list of URLs to CSS files here, and they
 
83
            will be incorporated as <link rel="stylesheet" type="text/css">
 
84
            elements in the head, if write_html_head_foot is True.
 
85
            URLs should be relative to the IVLE root; they will be fixed up
 
86
            to be site-relative.
 
87
        scripts (write)
 
88
            List of strings. Write a list of URLs to JS files here, and they
 
89
            will be incorporated as <script type="text/javascript"> elements
 
90
            in the head, if write_html_head_foot is True.
 
91
            URLs should be relative to the IVLE root; they will be fixed up
 
92
            to be site-relative.
 
93
        scripts_init (write)
 
94
            List of strings. Write a list of JS function names, and they
 
95
            will be added as window.addListener('load', ..., false); calls
 
96
            in the head, if write_html_head_foot is True.
 
97
            This is the propper way to specify functions that need to run at 
 
98
            page load time.
82
99
    """
83
100
 
84
101
    # Special code for an OK response.
137
154
    HTTP_INSUFFICIENT_STORAGE         = 507
138
155
    HTTP_NOT_EXTENDED                 = 510
139
156
 
140
 
    def __init__(self, req, config):
141
 
        """Create an IVLE request from a mod_python one.
 
157
    def __init__(self, req):
 
158
        """Builds an IVLE request object from a mod_python request object.
 
159
        This results in an object with all of the necessary methods and
 
160
        additional fields.
142
161
 
143
 
        @param req: A mod_python request.
144
 
        @param config: An IVLE configuration.
 
162
        req: A mod_python request object.
145
163
        """
146
164
 
147
165
        # Methods are mostly wrappers around the Apache request object
148
166
        self.apache_req = req
149
 
        self.config = config
150
167
        self.headers_written = False
151
168
 
152
169
        # Determine if the browser used the public host name to make the
153
170
        # request (in which case we are in "public mode")
154
 
        if req.hostname == config['urls']['public_host']:
 
171
        if req.hostname == ivle.conf.public_host:
155
172
            self.publicmode = True
156
173
        else:
157
174
            self.publicmode = False
161
178
        self.uri = req.uri
162
179
        # Split the given path into the app (top-level dir) and sub-path
163
180
        # (after first stripping away the root directory)
164
 
        path = self.unmake_path(req.uri)
 
181
        path = ivle.util.unmake_path(req.uri)
165
182
        (self.app, self.path) = (ivle.util.split_path(path))
166
183
        self.user = None
167
184
        self.hostname = req.hostname
170
187
 
171
188
        # Open a database connection and transaction, keep it around for users
172
189
        # of the Request object to use
173
 
        self.store = ivle.database.get_store(config)
 
190
        self.store = ivle.database.get_store()
174
191
 
175
192
        # Default values for the output members
176
193
        self.status = Request.HTTP_OK
177
194
        self.content_type = None        # Use Apache's default
178
195
        self.location = None
 
196
        self.styles = []
 
197
        self.scripts = []
 
198
        self.scripts_init = []
179
199
        # In some cases we don't want the template JS (such as the username
180
200
        # and public FQDN) in the output HTML. In that case, set this to 0.
181
201
        self.write_javascript_settings = True
232
252
                for cookie in plugin.cookies:
233
253
                    self.add_cookie(mod_python.Cookie.Cookie(cookie, '',
234
254
                                                    expires=1, path='/'))
235
 
        self.throw_redirect(self.make_path(''))
 
255
        self.throw_redirect(ivle.util.make_path('')) 
236
256
 
237
257
 
238
258
    def flush(self):
272
292
        else:
273
293
            mod_python.Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
274
294
 
275
 
    def make_path(self, path):
276
 
        """Prepend the IVLE URL prefix to the given path.
277
 
 
278
 
        This is used when generating URLs to send to the client.
279
 
 
280
 
        This method is DEPRECATED. We no longer support use of a prefix.
281
 
        """
282
 
        return os.path.join(self.config['urls']['root'], path)
283
 
 
284
 
    def unmake_path(self, path):
285
 
        """Strip the IVLE URL prefix from the given path, if present.
286
 
 
287
 
        Also normalises the path.
288
 
 
289
 
        This method is DEPRECATED. We no longer support use of a prefix.
290
 
        """
291
 
        path = os.path.normpath(path)
292
 
        root = os.path.normpath(self.config['urls']['root'])
293
 
 
294
 
        if path.startswith(root):
295
 
            path = path[len(root):]
296
 
            # Take out the slash as well
297
 
            if len(path) > 0 and path[0] == os.sep:
298
 
                path = path[1:]
299
 
 
300
 
        return path
301
 
 
302
295
    def get_session(self):
303
296
        """Returns a mod_python Session object for this request.
304
297
        Note that this is dependent on mod_python and may need to change
305
 
        interface if porting away from mod_python.
306
 
 
307
 
        IMPORTANT: Call unlock() on the session as soon as you are done with
308
 
                   it! If you don't, all other requests will block!
309
 
        """
 
298
        interface if porting away from mod_python."""
310
299
        # Cache the session object and set the timeout to 24 hours.
311
300
        if not hasattr(self, 'session'):
312
301
            self.session = mod_python.Session.FileSession(self.apache_req,