From c4e872c94c395ec4a08fb18304582df598a28f85 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 6 Mar 2022 18:49:09 -0600 Subject: [PATCH] Add the "provider" concept, let them configure db sessions more to come... --- tailbone/app.py | 11 +++++++-- tailbone/providers.py | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tailbone/providers.py diff --git a/tailbone/app.py b/tailbone/app.py index 6896ea4d..9874e51a 100644 --- a/tailbone/app.py +++ b/tailbone/app.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2020 Lance Edgar +# Copyright © 2010-2022 Lance Edgar # # This file is part of Rattail. # @@ -45,6 +45,7 @@ import tailbone.db from tailbone.auth import TailboneAuthorizationPolicy from tailbone.config import csrf_token_name, csrf_header_name from tailbone.util import get_effective_theme, get_theme_template_path +from tailbone.providers import get_all_providers def make_rattail_config(settings): @@ -115,6 +116,8 @@ def make_pyramid_config(settings, configure_csrf=True): """ Make a Pyramid config object from the given settings. """ + rattail_config = settings['rattail_config'] + config = settings.pop('pyramid_config', None) if config: config.set_root_factory(Root) @@ -132,7 +135,6 @@ def make_pyramid_config(settings, configure_csrf=True): # maybe require CSRF token protection if configure_csrf: - rattail_config = settings['rattail_config'] config.set_default_csrf_options(require_csrf=True, token=csrf_token_name(rattail_config), header=csrf_header_name(rattail_config)) @@ -152,6 +154,11 @@ def make_pyramid_config(settings, configure_csrf=True): else: config.include('pyramid_retry') + # configure DB sessions associated with transaction manager + providers = get_all_providers(rattail_config) + for provider in six.itervalues(providers): + provider.configure_db_sessions(rattail_config, config) + # Add some permissions magic. config.add_directive('add_tailbone_permission_group', 'tailbone.auth.add_permission_group') config.add_directive('add_tailbone_permission', 'tailbone.auth.add_permission') diff --git a/tailbone/providers.py b/tailbone/providers.py new file mode 100644 index 00000000..6752a447 --- /dev/null +++ b/tailbone/providers.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2022 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# Rattail. If not, see . +# +################################################################################ +""" +Providers for Tailbone features +""" + +from __future__ import unicode_literals, absolute_import + +from rattail.util import load_entry_points + + +class TailboneProvider(object): + """ + Base class for Tailbone providers. These are responsible for + declaring which things a given project makes available to the app. + (Or at least the things which should be easily configurable.) + """ + + def __init__(self, config): + self.config = config + + def configure_db_sessions(self, rattail_config, pyramid_config): + pass + + def get_provided_views(self): + return {} + + +def get_all_providers(config): + """ + Returns a dict of all registered providers. + """ + providers = load_entry_points('tailbone.providers') + for key in list(providers): + providers[key] = providers[key](config) + return providers