Ticket #10899: easy_session_manipulation.diff

File easy_session_manipulation.diff, 3.9 KB (added by tallfred, 6 years ago)
  • test/client.py

    class Client(object): 
    166166        self.cookies = SimpleCookie()
    167167        self.exc_info = None
    168168        self.errors = StringIO()
     169        self._session_cache = None
    169170
    170171    def store_exc_info(self, **kwargs):
    171172        """
    class Client(object): 
    177178        """
    178179        Obtains 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 {}
     181        if self._session_cache == None:
     182            if 'django.contrib.sessions' in settings.INSTALLED_APPS:
     183                engine = import_module(settings.SESSION_ENGINE)
     184                cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
     185                if cookie:
     186                    self._session_cache = engine.SessionStore(cookie.value)
     187                else:
     188                    self._session_cache = engine.SessionStore()
     189                    self._session_cache.save()  # make load() work so the cookie isn't worthless
     190                    self.cookies[settings.SESSION_COOKIE_NAME] = self._session_cache.session_key
     191            else:
     192                self._session_cache = {}
     193        return self._session_cache
     194    def _session_save(self):
     195        if self._session_cache:
     196            self._session_cache.save()
     197            self._session_cache = None
     198
    186199    session = property(_session)
    187200
    188201    def request(self, **request):
    class Client(object): 
    268281        """
    269282        Requests a response from the server using GET.
    270283        """
     284        self._session_save()
    271285        parsed = urlparse(path)
    272286        r = {
    273287            'CONTENT_TYPE':    'text/html; charset=utf-8',
    class Client(object): 
    288302        """
    289303        Requests a response from the server using POST.
    290304        """
     305        self._session_save()
    291306        if content_type is MULTIPART_CONTENT:
    292307            post_data = encode_multipart(BOUNDARY, data)
    293308        else:
    class Client(object): 
    319334        """
    320335        Request a response from the server using HEAD.
    321336        """
     337        self._session_save()
    322338        parsed = urlparse(path)
    323339        r = {
    324340            'CONTENT_TYPE':    'text/html; charset=utf-8',
    class Client(object): 
    338354        """
    339355        Request a response from the server using OPTIONS.
    340356        """
     357        self._session_save()
    341358        parsed = urlparse(path)
    342359        r = {
    343360            'PATH_INFO':       urllib.unquote(parsed[2]),
    class Client(object): 
    357374        """
    358375        Send a resource to the server using PUT.
    359376        """
     377        self._session_save()
    360378        if content_type is MULTIPART_CONTENT:
    361379            post_data = encode_multipart(BOUNDARY, data)
    362380        else:
    class Client(object): 
    382400        """
    383401        Send a DELETE request to the server.
    384402        """
     403        self._session_save()
    385404        parsed = urlparse(path)
    386405        r = {
    387406            'PATH_INFO':       urllib.unquote(parsed[2]),
    class Client(object): 
    404423        are incorrect, or the user is inactive, or if the sessions framework is
    405424        not available.
    406425        """
     426        self._session_save()
    407427        user = authenticate(**credentials)
    408428        if user and user.is_active \
    409429                and 'django.contrib.sessions' in settings.INSTALLED_APPS:
    class Client(object): 
    442462
    443463        Causes the authenticated user to be logged out.
    444464        """
     465        self._session_cache = None
    445466        session = import_module(settings.SESSION_ENGINE).SessionStore()
    446467        session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
    447468        if session_cookie:
Back to Top