2
2
import time
3
3
from importlib import import_module
4
4
5
+ import django
5
6
from django .conf import settings
6
7
from django .contrib .sessions .backends .base import UpdateError
7
8
from django .core .exceptions import SuspiciousOperation
@@ -167,9 +168,7 @@ def __init__(self, scope, send):
167
168
168
169
async def resolve_session (self ):
169
170
session_key = self .scope ["cookies" ].get (self .cookie_name )
170
- self .scope ["session" ]._wrapped = await database_sync_to_async (
171
- self .session_store
172
- )(session_key )
171
+ self .scope ["session" ]._wrapped = self .session_store (session_key )
173
172
174
173
async def send (self , message ):
175
174
"""
@@ -187,7 +186,7 @@ async def send(self, message):
187
186
and message .get ("status" , 200 ) != 500
188
187
and (modified or settings .SESSION_SAVE_EVERY_REQUEST )
189
188
):
190
- await database_sync_to_async ( self .save_session ) ()
189
+ await self .save_session ()
191
190
# If this is a message type that can transport cookies back to the
192
191
# client, then do so.
193
192
if message ["type" ] in self .cookie_response_message_types :
@@ -225,12 +224,15 @@ async def send(self, message):
225
224
# Pass up the send
226
225
return await self .real_send (message )
227
226
228
- def save_session (self ):
227
+ async def save_session (self ):
229
228
"""
230
229
Saves the current session.
231
230
"""
232
231
try :
233
- self .scope ["session" ].save ()
232
+ if django .VERSION >= (5 , 1 ):
233
+ await self .scope ["session" ].asave ()
234
+ else :
235
+ database_sync_to_async (self .scope ["session" ].save )()
234
236
except UpdateError :
235
237
raise SuspiciousOperation (
236
238
"The request's session was deleted before the "
0 commit comments