Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Set custom request properties on request instances generated by pyram…

…id.scripting._make_request().
  • Loading branch information...
commit 93b4ba64dd22dd8953dbc3c113b3736b411c900c 1 parent c9ec6bd
@dairiki authored
View
6 CHANGES.txt
@@ -9,6 +9,12 @@ Bug Fixes
return the empty list. This was incorrect, it should have unconditionally
returned ``[Everyone]``, and now does.
+- Request instances generated by ``pyramid.scripting.prepare()``,
+ ``pyramid.scripting.get_root()`` and ``pyramid.paster.bootstrap()``
+ will now get request properties which were configured via
+ ``pyramid.config.Configurator.set_request_property()``. Note that
+ if an explicit request instance is passed in, the custom properties
+ *will not* be added.
Features
--------
View
6 pyramid/scripting.py
@@ -4,6 +4,7 @@
from pyramid.interfaces import (
IRequestFactory,
+ IRequestProperties,
IRootFactory,
)
@@ -105,4 +106,9 @@ def _make_request(path, registry=None):
request_factory = registry.queryUtility(IRequestFactory, default=Request)
request = request_factory.blank(path)
request.registry = registry
+
+ plist = registry.queryUtility(IRequestProperties, [])
+ for name, callable, reify in plist:
+ request.set_property(callable, name=name, reify=reify)
+
return request
View
31 pyramid/tests/test_scripting.py
@@ -1,5 +1,11 @@
import unittest
+from pyramid.interfaces import (
+ IRequestFactory,
+ IRequestProperties,
+ IRootFactory,
+ )
+
class Test_get_root(unittest.TestCase):
def _callFUT(self, app, request=None):
from pyramid.scripting import get_root
@@ -111,6 +117,12 @@ def test_it_with_no_registry(self):
finally:
global_registries.empty()
+ def test_request_gets_custom_properties(self):
+ plist = [('my_custom_property', 'callable', False)]
+ registry = DummyRegistry(DummyFactory, plist)
+ request = self._callFUT('/', registry)
+ self.assertEqual(request.plist, plist)
+
class Dummy:
pass
@@ -127,11 +139,15 @@ def __init__(self, *a, **kw):
self.kw = kw
class DummyRegistry(object):
- def __init__(self, factory=None):
- self.factory = factory
+ def __init__(self, factory=None, properties=None):
+ self.utilities = {
+ IRequestFactory: factory,
+ IRootFactory: factory,
+ IRequestProperties: properties,
+ }
def queryUtility(self, iface, default=None):
- return self.factory or default
+ return self.utilities[iface] or default
dummy_registry = DummyRegistry(DummyFactory)
@@ -148,14 +164,17 @@ class DummyThreadLocalManager:
def __init__(self):
self.pushed = []
self.popped = []
-
+
def push(self, item):
self.pushed.append(item)
def pop(self):
self.popped.append(True)
-
+
class DummyRequest:
def __init__(self, environ):
self.environ = environ
-
+ self.plist = []
+
+ def set_property(self, callable, name=None, reify=False):
+ self.plist.append((name, callable, reify))
Please sign in to comment.
Something went wrong with that request. Please try again.