paulb@1 | 1 | #!/usr/bin/env python |
paulb@1 | 2 | |
paulb@1 | 3 | """ |
paulb@1 | 4 | Twisted classes. |
paulb@403 | 5 | |
paulb@623 | 6 | Copyright (C) 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@1 | 21 | """ |
paulb@1 | 22 | |
paulb@463 | 23 | import WebStack.Generic |
paulb@463 | 24 | from WebStack.Helpers.Auth import UserInfo |
paulb@463 | 25 | from WebStack.Helpers.Request import Cookie, decode_value, filter_fields |
paulb@463 | 26 | from WebStack.Helpers.Response import ConvertingStream |
paulb@463 | 27 | from WebStack.Helpers.Session import SessionStore |
paulb@71 | 28 | from cgi import parse_qs |
paulb@1 | 29 | |
paulb@463 | 30 | class Transaction(WebStack.Generic.Transaction): |
paulb@1 | 31 | |
paulb@1 | 32 | """ |
paulb@1 | 33 | Twisted transaction interface. |
paulb@1 | 34 | """ |
paulb@1 | 35 | |
paulb@1 | 36 | def __init__(self, trans): |
paulb@1 | 37 | |
paulb@1 | 38 | "Initialise the transaction using the Twisted transaction 'trans'." |
paulb@1 | 39 | |
paulb@1 | 40 | self.trans = trans |
paulb@311 | 41 | self.content_type = None |
paulb@1 | 42 | |
paulb@239 | 43 | # Special objects retained throughout the transaction. |
paulb@239 | 44 | |
paulb@239 | 45 | self.session_store = None |
paulb@239 | 46 | |
paulb@239 | 47 | def commit(self): |
paulb@239 | 48 | |
paulb@239 | 49 | """ |
paulb@239 | 50 | A special method, synchronising the transaction with framework-specific |
paulb@239 | 51 | objects. |
paulb@239 | 52 | """ |
paulb@239 | 53 | |
paulb@239 | 54 | # Close the session store. |
paulb@239 | 55 | |
paulb@239 | 56 | if self.session_store is not None: |
paulb@239 | 57 | self.session_store.close() |
paulb@239 | 58 | |
paulb@338 | 59 | # Server-related methods. |
paulb@338 | 60 | |
paulb@338 | 61 | def get_server_name(self): |
paulb@338 | 62 | |
paulb@338 | 63 | "Returns the server name." |
paulb@338 | 64 | |
paulb@338 | 65 | return self.trans.getRequestHostname() |
paulb@338 | 66 | |
paulb@338 | 67 | def get_server_port(self): |
paulb@338 | 68 | |
paulb@338 | 69 | "Returns the server port as a string." |
paulb@338 | 70 | |
paulb@651 | 71 | host = self.trans.getHost() |
paulb@651 | 72 | |
paulb@651 | 73 | # Use the attribute for Twisted 2.0 or greater. |
paulb@651 | 74 | |
paulb@651 | 75 | if hasattr(host, "port"): |
paulb@651 | 76 | return str(host.port) |
paulb@651 | 77 | |
paulb@651 | 78 | # Use the element for earlier versions. |
paulb@651 | 79 | |
paulb@651 | 80 | else: |
paulb@651 | 81 | return str(host[2]) |
paulb@338 | 82 | |
paulb@1 | 83 | # Request-related methods. |
paulb@1 | 84 | |
paulb@1 | 85 | def get_request_stream(self): |
paulb@1 | 86 | |
paulb@1 | 87 | """ |
paulb@186 | 88 | Returns the request stream for the transaction. |
paulb@1 | 89 | """ |
paulb@1 | 90 | |
paulb@651 | 91 | #self.trans.content.seek(0) |
paulb@5 | 92 | return self.trans.content |
paulb@1 | 93 | |
paulb@1 | 94 | def get_request_method(self): |
paulb@1 | 95 | |
paulb@1 | 96 | """ |
paulb@186 | 97 | Returns the request method. |
paulb@1 | 98 | """ |
paulb@1 | 99 | |
paulb@1 | 100 | return self.trans.method |
paulb@1 | 101 | |
paulb@1 | 102 | def get_headers(self): |
paulb@1 | 103 | |
paulb@1 | 104 | """ |
paulb@186 | 105 | Returns all request headers as a dictionary-like object mapping header |
paulb@186 | 106 | names to values. |
paulb@186 | 107 | |
paulb@91 | 108 | NOTE: If duplicate header names are permitted, then this interface will |
paulb@91 | 109 | NOTE: need to change. |
paulb@1 | 110 | """ |
paulb@1 | 111 | |
paulb@65 | 112 | return self.trans.received_headers |
paulb@65 | 113 | |
paulb@65 | 114 | def get_header_values(self, key): |
paulb@1 | 115 | |
paulb@65 | 116 | """ |
paulb@186 | 117 | Returns a list of all request header values associated with the given |
paulb@186 | 118 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@186 | 119 | case-insensitive string. |
paulb@65 | 120 | """ |
paulb@65 | 121 | |
paulb@65 | 122 | # Twisted does not convert the header key to lower case (which is the |
paulb@65 | 123 | # stored representation). |
paulb@65 | 124 | |
paulb@65 | 125 | return self.convert_to_list(self.trans.received_headers.get(key.lower())) |
paulb@1 | 126 | |
paulb@1 | 127 | def get_content_type(self): |
paulb@1 | 128 | |
paulb@1 | 129 | """ |
paulb@186 | 130 | Returns the content type specified on the request, along with the |
paulb@186 | 131 | charset employed. |
paulb@1 | 132 | """ |
paulb@1 | 133 | |
paulb@1 | 134 | return self.parse_content_type(self.trans.getHeader("Content-Type")) |
paulb@1 | 135 | |
paulb@1 | 136 | def get_content_charsets(self): |
paulb@1 | 137 | |
paulb@1 | 138 | """ |
paulb@1 | 139 | Returns the character set preferences. |
paulb@1 | 140 | """ |
paulb@1 | 141 | |
paulb@1 | 142 | return self.parse_content_preferences(self.trans.getHeader("Accept-Language")) |
paulb@1 | 143 | |
paulb@1 | 144 | def get_content_languages(self): |
paulb@1 | 145 | |
paulb@1 | 146 | """ |
paulb@186 | 147 | Returns extracted language information from the transaction. |
paulb@1 | 148 | """ |
paulb@1 | 149 | |
paulb@1 | 150 | return self.parse_content_preferences(self.trans.getHeader("Accept-Charset")) |
paulb@1 | 151 | |
paulb@434 | 152 | def get_path(self, encoding=None): |
paulb@5 | 153 | |
paulb@5 | 154 | """ |
paulb@434 | 155 | Returns the entire path from the request as a Unicode object. Any "URL |
paulb@434 | 156 | encoded" character values in the part of the path before the query |
paulb@434 | 157 | string will be decoded and presented as genuine characters; the query |
paulb@434 | 158 | string will remain "URL encoded", however. |
paulb@434 | 159 | |
paulb@434 | 160 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 161 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 162 | character values. |
paulb@5 | 163 | """ |
paulb@5 | 164 | |
paulb@434 | 165 | path = self.get_path_without_query(encoding) |
paulb@434 | 166 | qs = self.get_query_string() |
paulb@434 | 167 | if qs: |
paulb@434 | 168 | return path + "?" + qs |
paulb@434 | 169 | else: |
paulb@434 | 170 | return path |
paulb@5 | 171 | |
paulb@434 | 172 | def get_path_without_query(self, encoding=None): |
paulb@162 | 173 | |
paulb@162 | 174 | """ |
paulb@434 | 175 | Returns the entire path from the request minus the query string as a |
paulb@434 | 176 | Unicode object containing genuine characters (as opposed to "URL |
paulb@434 | 177 | encoded" character values). |
paulb@434 | 178 | |
paulb@434 | 179 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 180 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 181 | character values. |
paulb@162 | 182 | """ |
paulb@162 | 183 | |
paulb@434 | 184 | return self.decode_path(self.trans.uri.split("?")[0], encoding) |
paulb@162 | 185 | |
paulb@434 | 186 | def get_path_info(self, encoding=None): |
paulb@1 | 187 | |
paulb@1 | 188 | """ |
paulb@186 | 189 | Returns the "path info" (the part of the URL after the resource name |
paulb@434 | 190 | handling the current request) from the request as a Unicode object |
paulb@434 | 191 | containing genuine characters (as opposed to "URL encoded" character |
paulb@434 | 192 | values). |
paulb@434 | 193 | |
paulb@434 | 194 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 195 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 196 | character values. |
paulb@1 | 197 | """ |
paulb@1 | 198 | |
paulb@623 | 199 | encoding = encoding or self.default_charset |
paulb@623 | 200 | |
paulb@439 | 201 | return decode_value("/%s" % "/".join(self.trans.postpath), encoding) |
paulb@1 | 202 | |
paulb@69 | 203 | def get_query_string(self): |
paulb@69 | 204 | |
paulb@69 | 205 | """ |
paulb@186 | 206 | Returns the query string from the path in the request. |
paulb@69 | 207 | """ |
paulb@69 | 208 | |
paulb@434 | 209 | t = self.trans.uri.split("?") |
paulb@69 | 210 | if len(t) == 1: |
paulb@69 | 211 | return "" |
paulb@69 | 212 | else: |
paulb@69 | 213 | |
paulb@69 | 214 | # NOTE: Overlook erroneous usage of "?" characters in the path. |
paulb@69 | 215 | |
paulb@69 | 216 | return "?".join(t[1:]) |
paulb@69 | 217 | |
paulb@1 | 218 | # Higher level request-related methods. |
paulb@1 | 219 | |
paulb@430 | 220 | def get_fields_from_path(self, encoding=None): |
paulb@69 | 221 | |
paulb@69 | 222 | """ |
paulb@249 | 223 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 224 | transaction. The underlying framework may refuse to supply fields from |
paulb@430 | 225 | the path if handling a POST transaction. The optional 'encoding' |
paulb@430 | 226 | parameter specifies the character encoding of the query string for cases |
paulb@430 | 227 | where the default encoding is to be overridden. |
paulb@69 | 228 | |
paulb@71 | 229 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@71 | 230 | single value is associated with any given field name). |
paulb@69 | 231 | """ |
paulb@69 | 232 | |
paulb@623 | 233 | encoding = encoding or self.default_charset |
paulb@623 | 234 | |
paulb@249 | 235 | fields = {} |
paulb@249 | 236 | for name, values in parse_qs(self.get_query_string(), keep_blank_values=1).items(): |
paulb@439 | 237 | name = decode_value(name, encoding) |
paulb@249 | 238 | fields[name] = [] |
paulb@249 | 239 | for value in values: |
paulb@439 | 240 | value = decode_value(value, encoding) |
paulb@430 | 241 | fields[name].append(value) |
paulb@249 | 242 | return fields |
paulb@69 | 243 | |
paulb@167 | 244 | def get_fields_from_body(self, encoding=None): |
paulb@69 | 245 | |
paulb@69 | 246 | """ |
paulb@249 | 247 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 248 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 249 | encoding of the message body for cases where no such information is |
paulb@249 | 250 | available, but where the default encoding is to be overridden. |
paulb@69 | 251 | |
paulb@73 | 252 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 253 | single value is associated with any given field name). Each value is |
paulb@193 | 254 | either a Unicode object (representing a simple form field, for example) |
paulb@543 | 255 | or a WebStack.Helpers.Request.FileContent object (representing a file |
paulb@543 | 256 | upload form field). |
paulb@543 | 257 | |
paulb@543 | 258 | NOTE: Twisted does not currently support file uploads correctly and a |
paulb@543 | 259 | NOTE: Unicode object will be returned for such fields instead. |
paulb@69 | 260 | """ |
paulb@69 | 261 | |
paulb@249 | 262 | # There may not be a reliable means of extracting only the fields |
paulb@249 | 263 | # the message body using the API. Remove fields originating from the |
paulb@249 | 264 | # path in the mixture provided by the API. |
paulb@228 | 265 | |
paulb@249 | 266 | all_fields = self._get_fields(encoding) |
paulb@228 | 267 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 268 | return filter_fields(all_fields, fields_from_path) |
paulb@224 | 269 | |
paulb@249 | 270 | def _get_fields(self, encoding=None): |
paulb@224 | 271 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@167 | 272 | fields = {} |
paulb@167 | 273 | for field_name, field_values in self.trans.args.items(): |
paulb@439 | 274 | field_name = decode_value(field_name, encoding) |
paulb@434 | 275 | |
paulb@228 | 276 | # Find the body values. |
paulb@228 | 277 | |
paulb@198 | 278 | if type(field_values) == type([]): |
paulb@198 | 279 | fields[field_name] = [] |
paulb@198 | 280 | |
paulb@198 | 281 | # Twisted stores plain strings. |
paulb@198 | 282 | |
paulb@198 | 283 | for field_str in field_values: |
paulb@439 | 284 | fields[field_name].append(decode_value(field_str, encoding)) |
paulb@198 | 285 | else: |
paulb@439 | 286 | fields[field_name] = decode_value(field_values, encoding) |
paulb@228 | 287 | |
paulb@167 | 288 | return fields |
paulb@1 | 289 | |
paulb@249 | 290 | def get_fields(self, encoding=None): |
paulb@249 | 291 | |
paulb@249 | 292 | """ |
paulb@249 | 293 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 294 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 295 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 296 | where no such information is available, but where the default encoding |
paulb@249 | 297 | is to be overridden. |
paulb@249 | 298 | |
paulb@249 | 299 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 300 | single value is associated with any given field name). Each value is |
paulb@249 | 301 | either a Unicode object (representing a simple form field, for example) |
paulb@543 | 302 | or a WebStack.Helpers.Request.FileContent object (representing a file |
paulb@543 | 303 | upload form field). |
paulb@543 | 304 | |
paulb@543 | 305 | NOTE: Twisted does not currently support file uploads correctly and a |
paulb@543 | 306 | NOTE: Unicode object will be returned for such fields instead. |
paulb@249 | 307 | |
paulb@249 | 308 | Where a given field name is used in both the path and message body to |
paulb@249 | 309 | specify values, the values from both sources will be combined into a |
paulb@249 | 310 | single list associated with that field name. |
paulb@249 | 311 | """ |
paulb@249 | 312 | |
paulb@249 | 313 | return self._get_fields(encoding) |
paulb@249 | 314 | |
paulb@45 | 315 | def get_user(self): |
paulb@1 | 316 | |
paulb@1 | 317 | """ |
paulb@186 | 318 | Extracts user information from the transaction. |
paulb@108 | 319 | |
paulb@108 | 320 | Returns a username as a string or None if no user is defined. |
paulb@1 | 321 | """ |
paulb@1 | 322 | |
paulb@128 | 323 | # Twisted makes headers lower case. |
paulb@128 | 324 | |
paulb@128 | 325 | if self.user is not None: |
paulb@128 | 326 | return self.user |
paulb@45 | 327 | |
paulb@49 | 328 | auth_header = self.get_headers().get("authorization") |
paulb@49 | 329 | if auth_header: |
paulb@49 | 330 | return UserInfo(auth_header).username |
paulb@49 | 331 | else: |
paulb@49 | 332 | return None |
paulb@1 | 333 | |
paulb@104 | 334 | def get_cookies(self): |
paulb@104 | 335 | |
paulb@104 | 336 | """ |
paulb@186 | 337 | Obtains cookie information from the request. |
paulb@104 | 338 | |
paulb@104 | 339 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@104 | 340 | NOTE: Twisted does not seem to support this operation via methods. Thus, |
paulb@104 | 341 | NOTE: direct access has been employed to get the dictionary. |
paulb@104 | 342 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 343 | NOTE: introduced. |
paulb@104 | 344 | """ |
paulb@104 | 345 | |
paulb@258 | 346 | return self.process_cookies(self.trans.received_cookies, using_strings=1) |
paulb@104 | 347 | |
paulb@104 | 348 | def get_cookie(self, cookie_name): |
paulb@104 | 349 | |
paulb@104 | 350 | """ |
paulb@186 | 351 | Obtains cookie information from the request. |
paulb@104 | 352 | |
paulb@104 | 353 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@104 | 354 | cookie exists. |
paulb@104 | 355 | NOTE: Twisted also returns a plain string - a Cookie object is therefore |
paulb@104 | 356 | NOTE: introduced. |
paulb@104 | 357 | """ |
paulb@104 | 358 | |
paulb@258 | 359 | value = self.trans.getCookie(self.encode_cookie_value(cookie_name)) |
paulb@258 | 360 | if value is not None: |
paulb@258 | 361 | return Cookie(cookie_name, self.decode_cookie_value(value)) |
paulb@258 | 362 | else: |
paulb@258 | 363 | return None |
paulb@104 | 364 | |
paulb@1 | 365 | # Response-related methods. |
paulb@1 | 366 | |
paulb@1 | 367 | def get_response_stream(self): |
paulb@1 | 368 | |
paulb@1 | 369 | """ |
paulb@186 | 370 | Returns the response stream for the transaction. |
paulb@1 | 371 | """ |
paulb@1 | 372 | |
paulb@224 | 373 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 374 | # or a default encoding. |
paulb@167 | 375 | |
paulb@252 | 376 | encoding = self.get_response_stream_encoding() |
paulb@252 | 377 | return ConvertingStream(self.trans, encoding) |
paulb@252 | 378 | |
paulb@252 | 379 | def get_response_stream_encoding(self): |
paulb@252 | 380 | |
paulb@252 | 381 | """ |
paulb@252 | 382 | Returns the response stream encoding. |
paulb@252 | 383 | """ |
paulb@252 | 384 | |
paulb@167 | 385 | if self.content_type: |
paulb@224 | 386 | encoding = self.content_type.charset |
paulb@252 | 387 | else: |
paulb@252 | 388 | encoding = None |
paulb@252 | 389 | return encoding or self.default_charset |
paulb@1 | 390 | |
paulb@42 | 391 | def get_response_code(self): |
paulb@42 | 392 | |
paulb@42 | 393 | """ |
paulb@42 | 394 | Get the response code associated with the transaction. If no response |
paulb@42 | 395 | code is defined, None is returned. |
paulb@42 | 396 | """ |
paulb@42 | 397 | |
paulb@42 | 398 | # NOTE: Accessing the request attribute directly. |
paulb@42 | 399 | |
paulb@42 | 400 | return self.trans.code |
paulb@42 | 401 | |
paulb@42 | 402 | def set_response_code(self, response_code): |
paulb@42 | 403 | |
paulb@42 | 404 | """ |
paulb@42 | 405 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@42 | 406 | specification. |
paulb@42 | 407 | """ |
paulb@42 | 408 | |
paulb@42 | 409 | self.trans.setResponseCode(response_code) |
paulb@42 | 410 | |
paulb@67 | 411 | def set_header_value(self, header, value): |
paulb@49 | 412 | |
paulb@49 | 413 | """ |
paulb@49 | 414 | Set the HTTP 'header' with the given 'value'. |
paulb@49 | 415 | """ |
paulb@49 | 416 | |
paulb@49 | 417 | self.trans.setHeader(self.format_header_value(header), self.format_header_value(value)) |
paulb@49 | 418 | |
paulb@1 | 419 | def set_content_type(self, content_type): |
paulb@1 | 420 | |
paulb@1 | 421 | """ |
paulb@186 | 422 | Sets the 'content_type' for the response. |
paulb@1 | 423 | """ |
paulb@1 | 424 | |
paulb@167 | 425 | # Remember the content type for encoding purposes later. |
paulb@167 | 426 | |
paulb@167 | 427 | self.content_type = content_type |
paulb@224 | 428 | self.trans.setHeader("Content-Type", str(content_type)) |
paulb@1 | 429 | |
paulb@104 | 430 | # Higher level response-related methods. |
paulb@104 | 431 | |
paulb@104 | 432 | def set_cookie(self, cookie): |
paulb@104 | 433 | |
paulb@104 | 434 | """ |
paulb@186 | 435 | Stores the given 'cookie' object in the response. |
paulb@104 | 436 | """ |
paulb@104 | 437 | |
paulb@258 | 438 | self.set_cookie_value(cookie.name, cookie.value, path=cookie.path, expires=cookie.expires) |
paulb@104 | 439 | |
paulb@104 | 440 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@104 | 441 | |
paulb@104 | 442 | """ |
paulb@186 | 443 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@104 | 444 | |
paulb@104 | 445 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@104 | 446 | and the optional 'expires' parameter is a value compatible with the |
paulb@104 | 447 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@104 | 448 | """ |
paulb@104 | 449 | |
paulb@258 | 450 | self.trans.addCookie(self.encode_cookie_value(name), |
paulb@258 | 451 | self.encode_cookie_value(value), expires=expires, path=path) |
paulb@104 | 452 | |
paulb@104 | 453 | def delete_cookie(self, cookie_name): |
paulb@104 | 454 | |
paulb@104 | 455 | """ |
paulb@186 | 456 | Adds to the response a request that the cookie with the given |
paulb@186 | 457 | 'cookie_name' be deleted/discarded by the client. |
paulb@104 | 458 | """ |
paulb@104 | 459 | |
paulb@108 | 460 | # Create a special cookie, given that we do not know whether the browser |
paulb@108 | 461 | # has been sent the cookie or not. |
paulb@108 | 462 | # NOTE: Magic discovered in Webware. |
paulb@108 | 463 | |
paulb@258 | 464 | self.trans.addCookie(self.encode_cookie_value(cookie_name), "", expires=0, path="/", max_age=0) |
paulb@104 | 465 | |
paulb@239 | 466 | # Session-related methods. |
paulb@239 | 467 | |
paulb@239 | 468 | def get_session(self, create=1): |
paulb@239 | 469 | |
paulb@239 | 470 | """ |
paulb@239 | 471 | Gets a session corresponding to an identifier supplied in the |
paulb@239 | 472 | transaction. |
paulb@239 | 473 | |
paulb@239 | 474 | If no session has yet been established according to information |
paulb@239 | 475 | provided in the transaction then the optional 'create' parameter |
paulb@239 | 476 | determines whether a new session will be established. |
paulb@239 | 477 | |
paulb@239 | 478 | Where no session has been established and where 'create' is set to 0 |
paulb@239 | 479 | then None is returned. In all other cases, a session object is created |
paulb@239 | 480 | (where appropriate) and returned. |
paulb@239 | 481 | """ |
paulb@239 | 482 | |
paulb@239 | 483 | # NOTE: Requires configuration. |
paulb@239 | 484 | |
paulb@239 | 485 | if self.session_store is None: |
paulb@239 | 486 | self.session_store = SessionStore(self, "WebStack-sessions") |
paulb@239 | 487 | return self.session_store.get_session(create) |
paulb@239 | 488 | |
paulb@239 | 489 | def expire_session(self): |
paulb@239 | 490 | |
paulb@239 | 491 | """ |
paulb@239 | 492 | Expires any session established according to information provided in the |
paulb@239 | 493 | transaction. |
paulb@239 | 494 | """ |
paulb@239 | 495 | |
paulb@239 | 496 | # NOTE: Requires configuration. |
paulb@239 | 497 | |
paulb@239 | 498 | if self.session_store is None: |
paulb@239 | 499 | self.session_store = SessionStore(self, "WebStack-sessions") |
paulb@239 | 500 | self.session_store.expire_session() |
paulb@239 | 501 | |
paulb@1 | 502 | # vim: tabstop=4 expandtab shiftwidth=4 |