Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request[session_name]: error type 'Request' is not iterable on sanic 21 #6

Closed
yurenchen000 opened this issue Jan 27, 2022 · 1 comment · Fixed by #8
Closed

request[session_name]: error type 'Request' is not iterable on sanic 21 #6

yurenchen000 opened this issue Jan 27, 2022 · 1 comment · Fixed by #8

Comments

@yurenchen000
Copy link
Contributor

yurenchen000 commented Jan 27, 2022

description

seems request[session_name] can't store session object anymore.
// as metioned in #4

tested on

  • python 3.9
  • sanic 21.6.2

BTW, ecrypt/signed session status into client cookie is a good idea
for applications without stringent security requirements.

err detail

⚠️ 500 — Internal Server Error
argument of type 'Request' is not iterable

Traceback of test_app (most recent call last):
TypeError: argument of type 'Request' is not iterable
File /home/chen/.local/lib/python3.9/site-packages/sanic/app.py, line 749, in handle_request

response = await self._run_request_middleware(

File /home/chen/.local/lib/python3.9/site-packages/sanic/app.py, line 1116, in _run_request_middleware

response = await response

File /home/chen/.local/lib/python3.9/site-packages/sanic_cookiesession/__init__.py, line 34, in load_session

if session_name in request:

TypeError: argument of type 'Request' is not iterable while handling path /

a workaround

just for sanic21, py3.9
// I don't know much about other versions, compatibility may not considered

diff with 3cc1535

--- a/examples/counter.py
+++ b/examples/counter.py
@@ -18,24 +18,24 @@ def render(session):
 
 @app.get('/')
 async def index(request):
-    return render(request['session'])
+    return render(request.ctx.session)
 
 
 @app.get('/inc')
 async def inc(request):
-    request['session']['counter'] += 1
+    request.ctx.session['counter'] += 1
     return response.redirect('/')
 
 
 @app.get('/dec')
 async def dec(request):
-    request['session']['counter'] -= 1
+    request.ctx.session['counter'] -= 1
     return response.redirect('/')
 
 
 @app.middleware('request')
 async def create_counter(request):
-    request['session'].setdefault('counter', 0)
+    request.ctx.session.setdefault('counter', 0)
 
 
 if __name__ == '__main__':



--- a/sanic_cookiesession/__init__.py
+++ b/sanic_cookiesession/__init__.py
@@ -31,7 +31,7 @@ def setup(app, session_type=dict, serializer_type=URLSafeTimedSerializer):
 
     @app.middleware('request')
     async def load_session(request):
-        if session_name in request:
+        if hasattr(request.ctx, session_name):
             return
         session_cookie = request.cookies.get(cookie_name)
         if session_cookie:
@@ -42,13 +42,13 @@ def setup(app, session_type=dict, serializer_type=URLSafeTimedSerializer):
                 session = session_type()
         else:
             session = session_type()
-        request[session_name] = session
+        setattr(request.ctx, session_name, session)
 
     @app.middleware('response')
     async def save_session(request, response):
-        session = request.get(session_name)
+        session = getattr(request.ctx, session_name, None)
         if session is None:
-            session = request[session_name] = session_type()
+            setattr(request.ctx, session_name, session := session_type())
         response.cookies[cookie_name] = serializer.dumps(session)
         if domain:
             response.cookies[cookie_name]['domain'] = domain
@yurenchen000 yurenchen000 changed the title error type 'Request' is not iterable on sanic 21 error type 'Request' is not iterable on sanic 21 // about request[session_name] Jan 27, 2022
@yurenchen000 yurenchen000 changed the title error type 'Request' is not iterable on sanic 21 // about request[session_name] request[session_name]: error type 'Request' is not iterable on sanic 21 Jan 27, 2022
@pyx
Copy link
Owner

pyx commented Mar 24, 2022

Thank you for the information, could you please make a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants