Ticket #10899: easy_session_manipulation6.diff
File easy_session_manipulation6.diff, 5.5 KB (added by , 16 years ago) |
---|
-
test/client.py
class Client(object): 166 166 self.cookies = SimpleCookie() 167 167 self.exc_info = None 168 168 self.errors = StringIO() 169 self._session_store = None 169 170 170 171 def store_exc_info(self, **kwargs): 171 172 """ … … class Client(object): 175 176 176 177 def _session(self): 177 178 """ 178 Obtains the current session variables.179 Obtains a SessionStore containing all the current session variables. 179 180 """ 180 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 181 engine = import_module(settings.SESSION_ENGINE) 182 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 183 if cookie: 184 return engine.SessionStore(cookie.value) 185 return {} 186 session = property(_session) 181 if self._session_store == None: 182 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 183 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 184 engine = import_module(settings.SESSION_ENGINE) 185 if cookie: 186 self._session_store = engine.SessionStore(cookie.value) 187 else: 188 self._session_store = engine.SessionStore() 189 # if load() fails the cookie changes, so save() first 190 self._session_store.save() 191 self.cookies[settings.SESSION_COOKIE_NAME] = \ 192 self._session_store.session_key 193 else: 194 self._session_store = {} 195 return self._session_store 196 def _failure(self, *extra): 197 assert False, "Don't replace the session store." 198 session = property(_session, _failure, _failure) 199 200 def _session_save(self): 201 """ 202 Push the session store to the database then delete the 203 session store. It's needed before calling a view. 204 """ 205 if hasattr(self._session_store, 'modified'): 206 if self._session_store.modified: 207 self._session_store.save() 208 # update the cookie in case cycle_key() was called 209 self.cookies[settings.SESSION_COOKIE_NAME] = \ 210 self._session_store.session_key 211 self._session_store = None 187 212 188 213 def request(self, **request): 189 214 """ … … class Client(object): 268 293 """ 269 294 Requests a response from the server using GET. 270 295 """ 296 self._session_save() 271 297 parsed = urlparse(path) 272 298 r = { 273 299 'CONTENT_TYPE': 'text/html; charset=utf-8', … … class Client(object): 288 314 """ 289 315 Requests a response from the server using POST. 290 316 """ 317 self._session_save() 291 318 if content_type is MULTIPART_CONTENT: 292 319 post_data = encode_multipart(BOUNDARY, data) 293 320 else: … … class Client(object): 319 346 """ 320 347 Request a response from the server using HEAD. 321 348 """ 349 self._session_save() 322 350 parsed = urlparse(path) 323 351 r = { 324 352 'CONTENT_TYPE': 'text/html; charset=utf-8', … … class Client(object): 338 366 """ 339 367 Request a response from the server using OPTIONS. 340 368 """ 369 self._session_save() 341 370 parsed = urlparse(path) 342 371 r = { 343 372 'PATH_INFO': urllib.unquote(parsed[2]), … … class Client(object): 357 386 """ 358 387 Send a resource to the server using PUT. 359 388 """ 389 self._session_save() 360 390 if content_type is MULTIPART_CONTENT: 361 391 post_data = encode_multipart(BOUNDARY, data) 362 392 else: … … class Client(object): 382 412 """ 383 413 Send a DELETE request to the server. 384 414 """ 415 self._session_save() 385 416 parsed = urlparse(path) 386 417 r = { 387 418 'PATH_INFO': urllib.unquote(parsed[2]), … … class Client(object): 407 438 user = authenticate(**credentials) 408 439 if user and user.is_active \ 409 440 and 'django.contrib.sessions' in settings.INSTALLED_APPS: 410 engine = import_module(settings.SESSION_ENGINE)411 412 441 # Create a fake request to store login details. 413 442 request = HttpRequest() 414 if self.session: 415 request.session = self.session 416 else: 417 request.session = engine.SessionStore() 443 request.session = self.session 418 444 login(request, user) 419 445 420 # Set the cookie to represent the session.421 session_cookie = settings.SESSION_COOKIE_NAME422 self.cookies[session_cookie] = request.session.session_key423 cookie_data = {424 'max-age': None,425 'path': '/',426 'domain': settings.SESSION_COOKIE_DOMAIN,427 'secure': settings.SESSION_COOKIE_SECURE or None,428 'expires': None,429 }430 self.cookies[session_cookie].update(cookie_data)431 432 # Save the session values.433 request.session.save()434 435 446 return True 436 447 else: 437 448 return False … … class Client(object): 442 453 443 454 Causes the authenticated user to be logged out. 444 455 """ 456 self._session_store = None 445 457 session = import_module(settings.SESSION_ENGINE).SessionStore() 446 458 session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) 447 459 if session_cookie: