Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Last active December 18, 2015 18:39
Show Gist options
  • Save jvanasco/5827488 to your computer and use it in GitHub Desktop.
Save jvanasco/5827488 to your computer and use it in GitHub Desktop.
configurable sessions with pyramid
import logging
log = logging.getLogger(__name__)
import pyramid_beaker
from pyramid.session import UnencryptedCookieSessionFactoryConfig
import types
valid_args_pyramid_UnencryptedCookieSessionFactoryConfig = [
'secret',
'timeout',
'cookie_name',
'cookie_max_age',
'cookie_path',
'cookie_domain',
'cookie_secure',
'cookie_httponly',
'cookie_on_exception',
'signed_serialize',
'signed_deserialize',
]
def CustomSessionFactoryWrapper( settings , prefixes , def_session_prefix_lookup ):
if not isinstance( prefixes , (types.ListType,types.TupleType) ):
raise ValueError( "prefixes not list/tuple")
if not len(prefixes):
raise ValueError( "no len(prefixes)" )
# manage sets of options
options = {}
session_factories = {}
for prefix in prefixes :
options[prefix] = {}
session_factories[prefix] = {}
# Pull out any config args meant for our sessions. if there are any
for k, v in settings.items():
for prefix in prefixes:
if k.startswith(prefix):
option_name = k[len(prefix):]
options[prefix][option_name] = v
# coerce the options
for prefix in prefixes :
if 'framework' not in options[prefix] :
raise ValueError( "Missing from .ini -- %sframework" % prefix )
framework = options[prefix]['framework']
if framework == 'beaker' :
## pre-cleanup some args:
for option_name in options[prefix].keys() :
if option_name in ('cookie_on_exception',):
options[prefix][option_name] = pyramid_beaker.asbool(v)
## official cleanup
options[prefix] = pyramid_beaker.coerce_session_params( options[prefix] )
session_factories[prefix] = pyramid_beaker.BeakerSessionFactoryConfig( **options[prefix] )
elif framework == 'pyramid' :
_factory = options[prefix]['factory']
if _factory == 'UnencryptedCookieSessionFactoryConfig' :
## pre-cleanup some args:
for option_name in options[prefix].keys() :
if option_name in ('cookie_secure','cookie_httponly','cookie_on_exception'):
options[prefix][option_name] = pyramid_beaker.asbool(v)
# clean the args
_clean = {}
for option_name in options[prefix].keys() :
if option_name in valid_args_pyramid_UnencryptedCookieSessionFactoryConfig :
_clean[option_name] = options[prefix][option_name]
options[prefix] = _clean
_args = [ options[prefix]['secret'] , ]
_kwargs = options[prefix].copy()
del _kwargs['secret']
session_factories[prefix] = UnencryptedCookieSessionFactoryConfig( *_args , **_kwargs )
else:
raise ValueError( "Invalid .ini -- %stype = %s" % ( prefix , session_type ) )
else:
raise ValueError('i dont support `%s` yet' % framework )
# create a lookup
def session_from_request(request):
prefix = def_session_prefix_lookup(request)
return session_factories[prefix](request)
## return the lookup
return session_from_request
session_a.framework = pyramid
session_a.factory = UnencryptedCookieSessionFactoryConfig
session_a.cookie_name = SessionA
session_a.secret = hello.world
session_a.cookie_expires = False
session_b.framework = beaker
session_b.key = SessionB
session_b.type = cookie
session_b.timeout = 2592000
session_b.cookie_expires = False
session_b.validate_key = foo.bar
from custom_sessions import CustomSessionFactoryWrapper
from pyramid_subscribers_beaker_https_session import initialize_https_session_set_request_property
def initialize_sessions( config , settings ):
def session_prefix_lookup( request ):
if True :
return 'session_a.'
return 'session_b.'
prefixes = ( 'session_a.' , 'session_b.' , )
custom_session_factory = CustomSessionFactoryWrapper( settings , prefixes , session_prefix_lookup )
config.set_session_factory(custom_session_factory)
# add the https session
if True:
## actually initialize https support
initialize_https_session_set_request_property( config , settings )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment