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

« back to all changes in this revision

Viewing changes to www/dispatch/request.py

  • Committer: mattgiuca
  • Date: 2008-02-19 00:54:28 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:500
db: get_user and get_users now return User objects instead of dictionaries.
    This is the major part of replacing dicts with User objects, as it
    propagates upwards.

Propagated User objects up through the following modules:
    listusers.py, dispatch.login, authenticate, userservice, forumutil
All of these now treat users as an object rather than a dict.

To save on the size of the changes so far, login still individually copies
fields over to the session (so the session does not yet store a user object;
that is the second part of this refactor).

WOO!! Revision 500 :)

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import common.util
27
27
import mod_python
28
 
from mod_python import (util, Session)
 
28
from mod_python import (util, Session, Cookie)
 
29
import conf
29
30
 
30
31
class Request:
31
32
    """An IVLE request object. This is presented to the IVLE apps as a way of
45
46
        username (read)
46
47
            String. Login name of the user who is currently logged in, or
47
48
            None.
 
49
        hostname (read)
 
50
            String. Hostname the server is running on.
48
51
        headers_in (read)
49
52
            Table object representing headers sent by the client.
50
53
        headers_out (read, can be written to)
51
54
            Table object representing headers to be sent to the client.
 
55
        publicmode (read)
 
56
            Bool. True if the request came for the "public host" as
 
57
            configured in conf.py. Note that public mode requests do not
 
58
            have an app (app is set to None).
52
59
 
53
60
        status (write)
54
61
            Int. Response status number. Use one of the status codes defined
158
165
        self.func_write_html_head = write_html_head
159
166
        self.headers_written = False
160
167
 
 
168
        # Determine if the browser used the public host name to make the
 
169
        # request (in which case we are in "public mode")
 
170
        if req.hostname == conf.public_host:
 
171
            self.publicmode = True
 
172
        else:
 
173
            self.publicmode = False
 
174
 
161
175
        # Inherit values for the input members
162
176
        self.method = req.method
163
177
        self.uri = req.uri
164
178
        # Split the given path into the app (top-level dir) and sub-path
165
179
        # (after first stripping away the root directory)
166
 
        (self.app, self.path) = (
167
 
            common.util.split_path(common.util.unmake_path(req.uri)))
 
180
        path = common.util.unmake_path(req.uri)
 
181
        if self.publicmode:
 
182
            self.app = None
 
183
            self.path = path
 
184
        else:
 
185
            (self.app, self.path) = (common.util.split_path(path))
168
186
        self.username = None
 
187
        self.hostname = req.hostname
169
188
        self.headers_in = req.headers_in
170
189
        self.headers_out = req.headers_out
171
190
 
205
224
 
206
225
        if not self.headers_written:
207
226
            self.__writeheaders()
208
 
        self.apache_req.write(string, flush)
 
227
        if isinstance(string, unicode):
 
228
            # Encode unicode strings as UTF-8
 
229
            # (Otherwise cannot handle being written to a bytestream)
 
230
            self.apache_req.write(string.encode('utf8'), flush)
 
231
        else:
 
232
            # 8-bit clean strings just get written directly.
 
233
            # This includes binary strings.
 
234
            self.apache_req.write(string, flush)
209
235
 
210
236
    def flush(self):
211
237
        """Flushes the output buffer."""
245
271
        """
246
272
        mod_python.util.redirect(self.apache_req, location)
247
273
 
 
274
    def add_cookie(self, cookie, value=None, **attributes):
 
275
        """Inserts a cookie into this request object's headers."""
 
276
        if value is None:
 
277
            Cookie.add_cookie(self.apache_req, cookie)
 
278
        else:
 
279
            Cookie.add_cookie(self.apache_req, cookie, value, **attributes)
 
280
 
248
281
    def get_session(self):
249
282
        """Returns a mod_python Session object for this request.
250
283
        Note that this is dependent on mod_python and may need to change
251
284
        interface if porting away from mod_python."""
252
 
        # Cache the session object
 
285
        # Cache the session object and set the timeout to 24 hours.
253
286
        if not hasattr(self, 'session'):
254
 
            self.session = Session.Session(self.apache_req)
 
287
            self.session = Session.FileSession(self.apache_req,
 
288
                                               timeout = 60 * 60 * 24)
255
289
        return self.session
256
290
 
257
291
    def get_fieldstorage(self):