anmnsg / webapp-improved

Automatically exported from code.google.com/p/webapp-improved
Other
0 stars 0 forks source link

Broken webapp2 #5

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Download and unpack webapp2-1.6.3.zip 176kB
2. Create a new GAE project and copy webapp2.py and webapp2_extras folder from 
step 1 into project root
3. Sample code trying to test sessions (put this in a file main.py):
import webapp2
from google.appengine.ext.webapp import util
from webapp2_extras import sessions

class BaseHandler(webapp2.RequestHandler):
    def dispatch(self):
        # Get a session store for this request.
        self.session_store = sessions.get_store(request=self.request)

        try:
            # Dispatch the request.
            webapp2.RequestHandler.dispatch(self)
        finally:
            # Save all sessions.
            self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def session(self):
        # Returns a session using the default cookie key.
        return self.session_store.get_session()

class MainHandler(BaseHandler):
    def get(self):
        self.session['foo'] = 'bar'
        self.response.out.write('Hello world!')

def main():
    application = webapp2.WSGIApplication([('/', 'main.MainHandler')],
                                         debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

What is the expected output? What do you see instead?
expected: hello world
observed:
Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3858, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3792, in _Dispatch
    base_env_dict=env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 580, in Dispatch
    base_env_dict=base_env_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2918, in Dispatch
    self._module_dict)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2822, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2702, in ExecuteOrImportScript
    exec module_code in script_module.__dict__
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\main.py", line 51, in <module>
    main()
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\main.py", line 47, in main
    util.run_wsgi_app(application)
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 98, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 116, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2.py", line 1075, in __call__
    return self._internal_error(e, environ, start_response)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2.py", line 1067, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2.py", line 1061, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2.py", line 875, in default_dispatcher
    return factory(request, response)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2.py", line 327, in factory
    return handler.dispatch()
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\main.py", line 24, in dispatch
    self.session_store = sessions.get_store(request=self.request)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2_extras\sessions.py", line 397, in get_store
    store = request.registry[key] = factory(request)
  File "C:\Users\siddjain\PycharmProjects\webapp2_test\webapp2_extras\sessions.py", line 272, in __init__
    self.config = request.app.config[self.config_key]
AttributeError: 'WSGIApplication' object has no attribute 'config'

What version of the product are you using? On what operating system?
webapp2-1.6.3.zip 176kB, Windows Vista

Please provide any additional information below.
this is not the first time I have tried to use webapp2 or tipfy, and found that 
its broken out-of-the-box

Original issue reported on code.google.com by fd97...@gmail.com on 3 Jun 2011 at 5:31

GoogleCodeExporter commented 8 years ago
This is not documented, but webapp2 doesn't include a config object by default.

You must do:

import webapp2
from webapp2_extras import config as extras_config

app_config = {}
app_config['webapp2_extras.sessions'] = {
    'secret_key': 'some-secret-key',
}

app = webapp2.WSGIAppplication([...routes...])
app.config = extras_config.Config(app_config)

I'm sorry for the trouble, and I'll add a page to the docs explaining how to 
make the config used by some of the extras modules available.

Original comment by rodrigo.moraes on 3 Jun 2011 at 6:12

GoogleCodeExporter commented 8 years ago
This issue was closed by revision 76b7b45014a2.

Original comment by rodrigo.moraes on 3 Jun 2011 at 6:35

GoogleCodeExporter commented 8 years ago
As a side note, you must leave the application object outside of main():

application = webapp2.WSGIApplication([('/', 'main.MainHandler')],
                                     debug=True)

def main():
    util.run_wsgi_app(application)

This way the app is created once and not on every request.

Original comment by rodrigo.moraes on 3 Jun 2011 at 6:38

GoogleCodeExporter commented 8 years ago
After this issue, I decided to add a simple configuration object to the WSGI 
app, which now can be initialized with a config dictionary:

app = webapp2.WSGIApplication(routes, debug=True, config={})

A full explanation and example are here:

http://webapp-improved.appspot.com/guide/extras.html

The error "'WSGIApplication' object has no attribute 'config'" will no longer 
occur, as a separate config doesn't need to be set. The sessions module, 
however, requires a secret_key to be set.

Thanks for the feedback. :)

Original comment by rodrigo.moraes on 4 Jun 2011 at 11:28