Ticket #10899: easy_session_manipulation6.diff

File easy_session_manipulation6.diff, 5.5 KB (added by Rick Dean, 16 years ago)
  • test/client.py

    class Client(object):  
    166166        self.cookies = SimpleCookie()
    167167        self.exc_info = None
    168168        self.errors = StringIO()
     169        self._session_store = None
    169170
    170171    def store_exc_info(self, **kwargs):
    171172        """
    class Client(object):  
    175176
    176177    def _session(self):
    177178        """
    178         Obtains the current session variables.
     179        Obtains a SessionStore containing all the current session variables.
    179180        """
    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
    187212
    188213    def request(self, **request):
    189214        """
    class Client(object):  
    268293        """
    269294        Requests a response from the server using GET.
    270295        """
     296        self._session_save()
    271297        parsed = urlparse(path)
    272298        r = {
    273299            'CONTENT_TYPE':    'text/html; charset=utf-8',
    class Client(object):  
    288314        """
    289315        Requests a response from the server using POST.
    290316        """
     317        self._session_save()
    291318        if content_type is MULTIPART_CONTENT:
    292319            post_data = encode_multipart(BOUNDARY, data)
    293320        else:
    class Client(object):  
    319346        """
    320347        Request a response from the server using HEAD.
    321348        """
     349        self._session_save()
    322350        parsed = urlparse(path)
    323351        r = {
    324352            'CONTENT_TYPE':    'text/html; charset=utf-8',
    class Client(object):  
    338366        """
    339367        Request a response from the server using OPTIONS.
    340368        """
     369        self._session_save()
    341370        parsed = urlparse(path)
    342371        r = {
    343372            'PATH_INFO':       urllib.unquote(parsed[2]),
    class Client(object):  
    357386        """
    358387        Send a resource to the server using PUT.
    359388        """
     389        self._session_save()
    360390        if content_type is MULTIPART_CONTENT:
    361391            post_data = encode_multipart(BOUNDARY, data)
    362392        else:
    class Client(object):  
    382412        """
    383413        Send a DELETE request to the server.
    384414        """
     415        self._session_save()
    385416        parsed = urlparse(path)
    386417        r = {
    387418            'PATH_INFO':       urllib.unquote(parsed[2]),
    class Client(object):  
    407438        user = authenticate(**credentials)
    408439        if user and user.is_active \
    409440                and 'django.contrib.sessions' in settings.INSTALLED_APPS:
    410             engine = import_module(settings.SESSION_ENGINE)
    411 
    412441            # Create a fake request to store login details.
    413442            request = HttpRequest()
    414             if self.session:
    415                 request.session = self.session
    416             else:
    417                 request.session = engine.SessionStore()
     443            request.session = self.session
    418444            login(request, user)
    419445
    420             # Set the cookie to represent the session.
    421             session_cookie = settings.SESSION_COOKIE_NAME
    422             self.cookies[session_cookie] = request.session.session_key
    423             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 
    435446            return True
    436447        else:
    437448            return False
    class Client(object):  
    442453
    443454        Causes the authenticated user to be logged out.
    444455        """
     456        self._session_store = None
    445457        session = import_module(settings.SESSION_ENGINE).SessionStore()
    446458        session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
    447459        if session_cookie:
Back to Top