Add event hook for attaching Rattail config to new requests.
				
					
				
			This commit is contained in:
		
							parent
							
								
									23ffcc5a78
								
							
						
					
					
						commit
						a958a7b285
					
				
					 4 changed files with 70 additions and 3 deletions
				
			
		
							
								
								
									
										7
									
								
								docs/api/subscribers.rst
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								docs/api/subscribers.rst
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
 | 
			
		||||
``tailbone.subscribers``
 | 
			
		||||
========================
 | 
			
		||||
 | 
			
		||||
.. automodule:: tailbone.subscribers
 | 
			
		||||
 | 
			
		||||
.. autofunction:: add_rattail_config_attribute_to_request
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue