diff options
| author | Jon Janzen <jon@jonjanzen.com> | 2023-10-16 18:50:20 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-03-13 17:55:15 +0100 |
| commit | f5c340684be3f27a145ec86ba55b24eb88d2780c (patch) | |
| tree | 910a61644c1bdd368e20b14977ee7a20aeff7688 /django/test | |
| parent | 33c06ca0da6c4f151b84e5d8c305faff9ca30d98 (diff) | |
Fixed #34901 -- Added async-compatible interface to session engines.
Thanks Andrew-Chen-Wang for the initial implementation which was posted
to the Django forum thread about asyncifying contrib modules.
Diffstat (limited to 'django/test')
| -rw-r--r-- | django/test/client.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/django/test/client.py b/django/test/client.py index d1fd428ea8..0964f87866 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -817,7 +817,14 @@ class ClientMixin: return session async def asession(self): - return await sync_to_async(lambda: self.session)() + engine = import_module(settings.SESSION_ENGINE) + cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) + if cookie: + return engine.SessionStore(cookie.value) + session = engine.SessionStore() + await session.asave() + self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key + return session def login(self, **credentials): """ @@ -893,7 +900,7 @@ class ClientMixin: await alogin(request, user, backend) # Save the session values. - await sync_to_async(request.session.save)() + await request.session.asave() self._set_login_cookies(request) def _set_login_cookies(self, request): |
