Search code examples
pythongoogle-app-enginevariablespython-2.7webapp2

Organizing my config variable for webapp2


For simplicity I think I need to rewrite this to just one statement

config = {'webapp2_extras.jinja2': {'template_path': 'templates',
          'filters': {
          'timesince': filters.timesince,
          'datetimeformat': filters.datetimeformat},
          'environment_args': {'extensions': ['jinja2.ext.i18n']}}}

config['webapp2_extras.sessions'] = \
    {'secret_key': 'my-secret-key'}

Then I want to know where to put it if I use multiple files with multiple request handlers. Should I just put it in one file and import it to the others? Since the session code is secret, what are your recommendation for handling it via source control? To always change the secret before or after committing to source control?

Thank you


Solution

  • Just add 'webapp2_extras.sessions' to your dict initializer:

    config = {'webapp2_extras.jinja2': {'template_path': 'templates',
              'filters': {
              'timesince': filters.timesince,
              'datetimeformat': filters.datetimeformat},
              'environment_args': {'extensions': ['jinja2.ext.i18n']}},
              'webapp2_extras.sessions': {'secret_key': 'my-secret-key'}}
    

    This would be clearer if the nesting were explicit, though:

    config = {
      'webapp2_extras.jinja2': {
        'template_path': 'templates',
        'filters': {
          'timesince': filters.timesince,
          'datetimeformat': filters.datetimeformat
        },
        'environment_args': {'extensions': ['jinja2.ext.i18n']},
      },
      'webapp2_extras.sessions': {'secret_key': 'my-secret-key'}
    }