Home | History | Annotate | Download | only in guide
      1 webapp2_extras
      2 ==============
      3 webapp2_extras is a package with common utilities that work well with
      4 webapp2. It includes:
      5 
      6 - Localization and internationalization support
      7 - Sessions using secure cookies, memcache or datastore
      8 - Extra route classes -- to match subdomains and other conveniences
      9 - Support for third party libraries: Jinja2 and Mako
     10 - Support for threaded environments, so that you can use webapp2 outside of
     11   App Engine or in the upcoming App Engine Python 2.7 runtime
     12 
     13 Some of these modules (:ref:`api.webapp2_extras.i18n`, :ref:`api.webapp2_extras.jinja2`,
     14 :ref:`api.webapp2_extras.mako` and :ref:`api.webapp2_extras.sessions`) use configuration
     15 values that can be set in the WSGI application. When a config key is not set,
     16 the modules will use the default values they define.
     17 
     18 All configuration keys are optional, except ``secret_key`` that must be set
     19 for :ref:`api.webapp2_extras.sessions`. Here is an example that sets the ``secret_key``
     20 configuration and tests that the session is working::
     21 
     22     import webapp2
     23     from webapp2_extras import sessions
     24 
     25     class BaseHandler(webapp2.RequestHandler):
     26         def dispatch(self):
     27             # Get a session store for this request.
     28             self.session_store = sessions.get_store(request=self.request)
     29 
     30             try:
     31                 # Dispatch the request.
     32                 webapp2.RequestHandler.dispatch(self)
     33             finally:
     34                 # Save all sessions.
     35                 self.session_store.save_sessions(self.response)
     36 
     37         @webapp2.cached_property
     38         def session(self):
     39             # Returns a session using the default cookie key.
     40             return self.session_store.get_session()
     41 
     42     class HomeHandler(BaseHandler):
     43         def get(self):
     44             test_value = self.session.get('test-value')
     45             if test_value:
     46                 self.response.write('Session has this value: %r.' % test_value)
     47             else:
     48                 self.session['test-value'] = 'Hello, session world!'
     49                 self.response.write('Session is empty.')
     50 
     51     config = {}
     52     config['webapp2_extras.sessions'] = {
     53         'secret_key': 'some-secret-key',
     54     }
     55 
     56     app = webapp2.WSGIAppplication([
     57         ('/', HomeHandler),
     58     ], debug=True, config=config)
     59 
     60     def main():
     61         app.run()
     62 
     63     if __name__ == '__main__':
     64         main()
     65