How to handle generator code inside a decorator?
<p>I'm attempting to write a login_required decorator for authenticating requests to a protected view in Django using a WSGI middleware.</p>

<p>Here's my code:</p>

<pre><code>def login_required(f, request_class=HTTPRequest):
    def _wrapper(*args, **kwargs):
        if not isinstance(args[0], request_class):
            req = request_class(environ=args[0])
        else:
            req = args[0]
        wsgi_app = WSGIController()
        settings = wsgi_app.settings                
        google_client = client.GoogleClient(
            settings.OAUTH2_CLIENT_ID,
            access_token=settings.OAUTH2_ACCESS_TOKEN,
            scope='email', 
            redirect_url=settings.OAUTH2_REDIRECT_URL,
            login_path="/session_login/")

         wsgi_app = google_client.wsgi_middleware(wsgi_app, \
            secret=settings.SECRET_KEY)
         def process_middleware(environ, start_response):
             return wsgi_app(environ, start_response)
         response = process_middleware(req.environ, f)
         return f(*args, **kwargs)
     return _wrapper
</code></pre>

<p>How can I handle the response object (generator) to redirect the user to the oauth login page ?</p>

<p>Update: If I add the following code:</p>

<blockquote>
  <p>redirect = [item for item in response.next()]</p>
</blockquote>

<p>Then I get the following error:</p>

<blockquote>
  <p>TypeError: unhashable type: 'list'</p>
</blockquote>

<p>You can reference to the login_required decorator logic in <code>django/contrib/auth/decoractors.py</code></p>

<p>It make use of <code>redirect_to_login</code> function to redirect to login page when login check is failed.</p>

<pre><code>from django.contrib.auth.views import redirect_to_login
return redirect_to_login(path, resolved_login_url, redirect_field_name)
</code></pre>

python django wsgi python-decorators