From a958a7b285f8b763324fd3df3000a7f781cfdc91 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 21 Feb 2014 10:10:10 -0800 Subject: [PATCH] Add event hook for attaching Rattail `config` to new requests. --- docs/api/subscribers.rst | 7 +++++++ docs/index.rst | 8 ++++++-- tailbone/subscribers.py | 30 ++++++++++++++++++++++++++++++ tests/test_subscribers.py | 28 +++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 docs/api/subscribers.rst diff --git a/docs/api/subscribers.rst b/docs/api/subscribers.rst new file mode 100644 index 00000000..abafe0c9 --- /dev/null +++ b/docs/api/subscribers.rst @@ -0,0 +1,7 @@ + +``tailbone.subscribers`` +======================== + +.. automodule:: tailbone.subscribers + +.. autofunction:: add_rattail_config_attribute_to_request diff --git a/docs/index.rst b/docs/index.rst index f5b66144..19c21af6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -5,13 +5,17 @@ Tailbone Welcome to Tailbone, part of the Rattail project. The documentation you are currently reading is for the Tailbone web application -package. More information is (sort of) available at http://rattail.edbob.org/. +package. More information is (sort of) available at http://rattailproject.org/. -Contents: +Clearly not everything is documented yet. Below you can see what has received +some attention thus far. + +API: .. toctree:: :maxdepth: 2 + api/subscribers Indices and tables diff --git a/tailbone/subscribers.py b/tailbone/subscribers.py index eea4f403..890d6809 100644 --- a/tailbone/subscribers.py +++ b/tailbone/subscribers.py @@ -36,6 +36,35 @@ from rattail.db.model import User from rattail.db.auth import has_permission +def add_rattail_config_attribute_to_request(event): + """ + Add a ``rattail_config`` attribute to a request object. + + This function is really just a matter of convenience, but it should help to + make other code more terse (example below). It is designed to act as a + subscriber to the Pyramid ``NewRequest`` event. + + A global Rattail ``config`` should already be present within the Pyramid + application registry's settings, which would normally be accessed via:: + + request.registry.settings['rattail_config'] + + This function merely "promotes" this config object so that it is more + directly accessible, a la:: + + request.rattail_config + + .. note:: + All this of course assumes that a Rattail ``config`` object *has* in + fact already been placed in the application registry settings. If this + is not the case, this function will do nothing. + """ + request = event.request + rattail_config = request.registry.settings.get('rattail_config') + if rattail_config: + request.rattail_config = rattail_config + + def before_render(event): """ Adds goodies to the global template renderer context. @@ -96,5 +125,6 @@ def context_found(event): def includeme(config): + config.add_subscriber(add_rattail_config_attribute_to_request, 'pyramid.events.NewRequest') config.add_subscriber(before_render, 'pyramid.events.BeforeRender') config.add_subscriber(context_found, 'pyramid.events.ContextFound') diff --git a/tests/test_subscribers.py b/tests/test_subscribers.py index 7e63b7c7..4317a262 100644 --- a/tests/test_subscribers.py +++ b/tests/test_subscribers.py @@ -1,7 +1,9 @@ +from unittest import TestCase + from mock import Mock +from pyramid import testing -from . import TestCase from tailbone import subscribers @@ -11,3 +13,27 @@ class SubscribersTests(TestCase): event = Mock() event.__setitem__ = Mock() subscribers.before_render(event) + + +class TestAddRattailConfigAttributeToRequest(TestCase): + + def test_nothing_is_done_if_no_config_in_registry_settings(self): + request = testing.DummyRequest() + config = testing.setUp(request=request) + self.assertFalse('rattail_config' in request.registry.settings) + self.assertFalse(hasattr(request, 'rattail_config')) + event = Mock(request=request) + subscribers.add_rattail_config_attribute_to_request(event) + self.assertFalse(hasattr(request, 'rattail_config')) + testing.tearDown() + + def test_attribute_added_if_config_present_in_registry_settings(self): + rattail_config = Mock() + request = testing.DummyRequest() + config = testing.setUp(request=request, settings={'rattail_config': rattail_config}) + self.assertTrue('rattail_config' in request.registry.settings) + self.assertFalse(hasattr(request, 'rattail_config')) + event = Mock(request=request) + subscribers.add_rattail_config_attribute_to_request(event) + self.assertTrue(request.rattail_config is rattail_config) + testing.tearDown()