Skip to content

Commit

Permalink
Guarantee a session factory to be set.
Browse files Browse the repository at this point in the history
Issue #891
  • Loading branch information
Wim-De-Clercq committed Jul 18, 2024
1 parent 23d1604 commit 354798a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions atramhasis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import logging
import os

from pyramid.config import Configurator
from pyramid.config import PHASE3_CONFIG
from pyramid.interfaces import ISessionFactory
from pyramid.session import SignedCookieSessionFactory
from pyramid.settings import aslist

from atramhasis.renderers import json_renderer_verbose

LOG = logging.getLogger(__name__)


DEFAULT_SETTINGS = {
"cache.tree.backend": "dogpile.cache.memory",
Expand Down Expand Up @@ -47,6 +53,38 @@ def includeme(config):
config.scan()


def configure_session(config):
"""
Configure pyramid's session factory.
People can configure their own session factory, but if no factory is registered
atramhasis will try configuring its own.
"""

def check_session_factory_set():
session_factory = config.registry.queryUtility(ISessionFactory)
if session_factory:
return

settings = config.registry.settings
if 'atramhasis.session_factory.secret' not in settings:
msg = (
'No session factory is configured, and '
'atramhasis.session_factory.secret setting is missing.'
)
raise ValueError(msg)

LOG.info('Using default SignedCookieSessionFactory.')
default_session_factory = SignedCookieSessionFactory(
settings['atramhasis.session_factory.secret']
)
config.registry.registerUtility(default_session_factory, ISessionFactory)

config.action(
'check_session_factory_set', check_session_factory_set, order=PHASE3_CONFIG + 1
)


def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def __init__(self):
def setUp(self):
pyramid_settings = Settings(settings)
config = testing.setUp(settings=pyramid_settings)
atramhasis.load_app(config, pyramid_settings)
atramhasis.load_app(config)
self.request = DummyRequest()
self.request.application_url = "http://localhost:6543"
apply_request_extensions(self.request)
Expand Down

0 comments on commit 354798a

Please sign in to comment.