From 6a8214ebb93fa6f03ad2bf110c3d807c39702ae8 Mon Sep 17 00:00:00 2001 From: Wim De Clercq Date: Thu, 18 Jul 2024 10:21:13 +0200 Subject: [PATCH] Guarantee a session factory to be set. Issue #891 --- atramhasis/__init__.py | 77 ++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/atramhasis/__init__.py b/atramhasis/__init__.py index 09026f24..920514b1 100644 --- a/atramhasis/__init__.py +++ b/atramhasis/__init__.py @@ -1,10 +1,16 @@ +import logging import os from pyramid.config import Configurator +from pyramid.config import PHASE2_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", @@ -29,30 +35,64 @@ def includeme(config): if key not in settings: settings[key] = value - config.include('pyramid_jinja2') - config.include('pyramid_tm') - config.add_static_view('static', 'static', cache_max_age=3600) - config.add_renderer('csv', 'atramhasis.renderers.CSVRenderer') - config.add_renderer('skosrenderer_verbose', json_renderer_verbose) + configure_session(config) + config.include("pyramid_jinja2") + config.include("pyramid_tm") + config.add_static_view("static", "static", cache_max_age=3600) + config.add_renderer("csv", "atramhasis.renderers.CSVRenderer") + config.add_renderer("skosrenderer_verbose", json_renderer_verbose) # Rewrite urls with trailing slash - config.include('pyramid_rewrite') + config.include("pyramid_rewrite") config.include("pyramid_openapi3") - config.include('atramhasis.routes') - config.include('pyramid_skosprovider') - config.include('atramhasis.cache') - config.scan('pyramid_skosprovider') + config.include("atramhasis.routes") + config.include("pyramid_skosprovider") + config.include("atramhasis.cache") + config.scan("pyramid_skosprovider") - config.add_translation_dirs('atramhasis:locale/') + config.add_translation_dirs("atramhasis:locale/") config.scan() -def main(global_config, **settings): - """ This function returns a Pyramid WSGI application. +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. """ - settings['layout.focus_conceptschemes'] = aslist(settings['layout.focus_conceptschemes'], flatten=False) - dump_location = settings['atramhasis.dump_location'] + 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.set_session_factory(default_session_factory) + + config.action( + "check_session_factory_set", check_session_factory_set, order=PHASE2_CONFIG + ) + + +def main(global_config, **settings): + """This function returns a Pyramid WSGI application.""" + settings["layout.focus_conceptschemes"] = aslist( + settings["layout.focus_conceptschemes"], flatten=False + ) + + dump_location = settings["atramhasis.dump_location"] if not os.path.exists(dump_location): os.makedirs(dump_location) @@ -63,11 +103,14 @@ def main(global_config, **settings): def load_app(config, settings): from pyramid.session import SignedCookieSessionFactory - atramhasis_session_factory = SignedCookieSessionFactory(settings['atramhasis.session_factory.secret']) + + atramhasis_session_factory = SignedCookieSessionFactory( + settings["atramhasis.session_factory.secret"] + ) config.set_session_factory(atramhasis_session_factory) includeme(config) - config.include('atramhasis.data:db') + config.include("atramhasis.data:db") return config.make_wsgi_app()