Skip to content
Browse files

- Make compatible with BFG 1.2.

  • Loading branch information...
1 parent 6c0f9f1 commit 1d37e25b735e8c2e4602bf65817871e787be82f4 Chris McDonough committed Mar 31, 2010
View
11 CHANGES.txt
@@ -1,9 +1,16 @@
+Next release
+------------
+
+- Make compatible with BFG 1.2.
+
0.2
+---
- Changes to support upcoming bfg release (which does not require
+- Changes to support upcoming bfg release (which does not require
zope.security, so we don't either).
0.1
+---
- Initial release.
+- Initial release.
View
2 COPYRIGHT.txt
@@ -1,3 +1,3 @@
-Copyright (c) 2008 Agendaless Consulting and Contributors.
+Copyright (c) 2008-2010 Agendaless Consulting and Contributors.
(http://www.agendaless.com), All Rights Reserved
View
26 README.txt
@@ -54,10 +54,9 @@ Create a ``bfg:viewgroup`` registration in ZCML via the
name="tabs_group"
viewnames="login_tab content_tab"
for=".interfaces.ISite"
- request_type="repoze.bfg.interfaces.IRequest"
/>
-The consitutient attributes of this directive are:
+The consituent attributes of this directive are:
``name`` -- The name by which the viewgroup can be found via traversal
or programmatic view execution. This attribute has the same meaning
@@ -71,18 +70,13 @@ The consitutient attributes of this directive are:
This attribute has the same meaning as its counterpart in
``bfg:view`` directive.
-``request_type`` -- The "model" interface which this view is
- registered for. This attribute has the same meaning as its
- counterpart in ``bfg:view`` directive.
+The ``for`` argument is optional; it defaults to ``None``. The
+``name`` argument is also optional. It defaults to the empty string
+(indicating the default view).
-The ``for`` and ``request_type`` arguments are optional; they default
-to ``zope.interface.Interface`` and ``repoze.bfg.interfaces.IRequest``
-respectively. The ``name`` argument is also optional. It defaults to
-the empty string (indicating the default view).
-
-Viewgroups registered wih a ``name``, ``for`` and ``request_type``
-will conflict with views registered with the same arguments, so it's
-wise to name your viewgroups differently than your views.
+Viewgroups registered wih a ``name`` and a ``for`` and will conflict
+with views registered with the same arguments, so it's wise to name
+your viewgroups differently than your views.
A viewgroup can refer to another viewgroup in its ``viewname``
argument as necessary (although this is insane).
@@ -106,11 +100,13 @@ instance of ``Provider`` can be constructed and passed into a template
rendering so it may be used ala the ``provider:`` expression type in
Zope 3. For example, this view might render a template::
+ from repoze.bfg.view import bfg_view
+
+ @bfg_view(template='templates/mytemplate.pt')
def myview(context, request):
from repoze.bfg.viewgroup.group import Provider
provider = Provider(context, request)
- return render_template_to_response('templates/mytemplate.pt',
- provider=provider)
+ return {'provider':provider}
The template being rendered can use the provider to "fill slots" by
passing in view or viewgroup names as necessary, e.g.::
View
1 TODO.txt
@@ -1 +0,0 @@
-List todo items here.
View
9 repoze/bfg/viewgroup/group.py
@@ -1,17 +1,12 @@
import itertools
-from zope.interface import implements
-
from webob import Response
-from repoze.bfg.interfaces import IView
-from repoze.bfg.security import Unauthorized
+from repoze.bfg.exceptions import Forbidden
from repoze.bfg.view import render_view_to_iterable
from repoze.bfg.view import render_view
class ViewGroup(object):
- implements(IView)
-
def __init__(self, name, viewnames):
self.name = name
self.viewnames = viewnames
@@ -22,7 +17,7 @@ def __call__(self, context, request):
for viewname in self.viewnames:
try:
iterable = render_view_to_iterable(context, request, viewname)
- except Unauthorized:
+ except Forbidden:
continue
if iterable is None:
raise ValueError(
View
1 repoze/bfg/viewgroup/tests/fixtureapp/configure.zcml
@@ -21,7 +21,6 @@
name="group"
viewnames="one two"
for=".models.IFixture"
- request_type=".views.IDummy"
/>
</configure>
View
167 repoze/bfg/viewgroup/tests/test_all.py
@@ -1,14 +1,15 @@
import unittest
-from zope.component.testing import PlacelessSetup
from zope.interface import Interface
-class TestViewGroupDirective(unittest.TestCase, PlacelessSetup):
+class TestViewGroupDirective(unittest.TestCase):
def setUp(self):
- PlacelessSetup.setUp(self)
+ from repoze.bfg.configuration import Configurator
+ self.config = Configurator()
+ self.config.hook_zca()
def tearDown(self):
- PlacelessSetup.tearDown(self)
+ self.config.end()
def _getFUT(self):
from repoze.bfg.viewgroup.zcml import viewgroup
@@ -20,132 +21,94 @@ def test_no_viewnames(self):
context = DummyContext()
self.assertRaises(ConfigurationError, f, context)
- def test_only_viewnames(self):
- f = self._getFUT()
- context = DummyContext()
- f(context, 'viewgroup', ['a', 'b', 'c'])
- actions = context.actions
- from repoze.bfg.interfaces import IRequest
+ def test_call(self):
from repoze.bfg.interfaces import IView
- from repoze.bfg.viewgroup.zcml import handler
-
- self.assertEqual(len(actions), 1)
-
- regadapt = actions[0]
- regadapt_discriminator = ('view', None, 'viewgroup', IRequest, IView)
- self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
- self.assertEqual(regadapt['callable'], handler)
- self.assertEqual(regadapt['args'][0], 'registerAdapter')
- self.assertEqual(regadapt['args'][1].name, 'viewgroup')
- self.assertEqual(regadapt['args'][1].viewnames, ['a', 'b', 'c'])
- self.assertEqual(regadapt['args'][2], (None, IRequest))
- self.assertEqual(regadapt['args'][3], IView)
- self.assertEqual(regadapt['args'][4], 'viewgroup')
- self.assertEqual(regadapt['args'][5], None)
-
- def test_with_request_type(self):
+ from repoze.bfg.interfaces import IRequest
f = self._getFUT()
context = DummyContext()
class IFoo:
pass
def view(context, request):
pass
- f(context, 'viewgroup', ['a', 'b', 'c'], IFoo, request_type=IDummy)
+ f(context, 'viewgroup', ['a', 'b', 'c'], IFoo)
actions = context.actions
- from repoze.bfg.interfaces import IView
- from repoze.bfg.viewgroup.zcml import handler
- from zope.component.interface import provideInterface
-
- self.assertEqual(len(actions), 2)
-
- provide = actions[0]
- self.assertEqual(provide['discriminator'], None)
- self.assertEqual(provide['callable'], provideInterface)
- self.assertEqual(provide['args'][0], '')
- self.assertEqual(provide['args'][1], IFoo)
-
- regadapt = actions[1]
- regadapt_discriminator = ('view', IFoo, 'viewgroup', IDummy, IView)
- self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
- self.assertEqual(regadapt['callable'], handler)
- self.assertEqual(regadapt['args'][0], 'registerAdapter')
- self.assertEqual(regadapt['args'][1].name, 'viewgroup')
- self.assertEqual(regadapt['args'][1].viewnames, ['a', 'b', 'c'])
- self.assertEqual(regadapt['args'][2], (IFoo, IDummy))
- self.assertEqual(regadapt['args'][3], IView)
- self.assertEqual(regadapt['args'][4], 'viewgroup')
- self.assertEqual(regadapt['args'][5], None)
-
-class TestViewGroup(unittest.TestCase, PlacelessSetup):
+
+ self.assertEqual(len(actions), 1)
+
+ action = actions[0]
+ registrar = action['callable']
+ registrar()
+ reg = self.config.registry
+ wrapper = reg.adapters.lookup((IRequest, Interface), IView,
+ name='viewgroup')
+ self.assertEqual(wrapper, None)
+
+class TestViewGroup(unittest.TestCase):
def setUp(self):
- PlacelessSetup.setUp(self)
+ from repoze.bfg.configuration import Configurator
+ self.config = Configurator()
+ self.config.begin()
+ self.config.hook_zca()
def tearDown(self):
- PlacelessSetup.tearDown(self)
+ self.config.end()
def _getTargetClass(self):
from repoze.bfg.viewgroup.group import ViewGroup
return ViewGroup
- def _registerSecurityPolicy(self, secpol):
- import zope.component
- gsm = zope.component.getGlobalSiteManager()
- from repoze.bfg.interfaces import ISecurityPolicy
- gsm.registerUtility(secpol, ISecurityPolicy)
+ def _registerSecurityPolicy(self):
+ self.config.testing_securitypolicy()
- def _registerView(self, app, name, *for_):
- import zope.component
- gsm = zope.component.getGlobalSiteManager()
- from repoze.bfg.interfaces import IView
- gsm.registerAdapter(app, for_, IView, name)
+ def _registerView(self, view, name):
+ self.config.add_view(view, name=name)
def _makeOne(self, name, viewnames):
return self._getTargetClass()(name, viewnames)
def test_no_viewnames(self):
- secpol = DummySecurityPolicy()
- self._registerSecurityPolicy(secpol)
group = self._makeOne('viewgroup', [])
context = DummyContext()
request = DummyRequest()
response = group(context, request)
self.assertEqual(''.join(response.app_iter), '')
def test_viewname_not_found(self):
- secpol = DummySecurityPolicy()
- self._registerSecurityPolicy(secpol)
+ self._registerSecurityPolicy()
group = self._makeOne('viewgroup', ['view1'])
context = DummyContext()
request = DummyRequest()
self.assertRaises(ValueError, group, context, request)
def test_all_permitted(self):
- secpol = DummySecurityPolicy()
- self._registerSecurityPolicy(secpol)
+ self._registerSecurityPolicy()
response1 = DummyResponse()
response1.app_iter = ['Response1']
view1 = make_view(response1)
- self._registerView(view1, 'view1', None, None)
+ self._registerView(view1, 'view1')
response2 = DummyResponse()
response2.app_iter = ['Response2']
view2 = make_view(response2)
- self._registerView(view2, 'view2', None, None)
+ self._registerView(view2, 'view2')
group = self._makeOne('viewgroup', ['view1', 'view2'])
context = DummyContext()
request = DummyRequest()
+ request.registry = self.config.registry
response = group(context, request)
self.assertEqual(''.join(response.app_iter), 'Response1Response2')
-class TestProvider(unittest.TestCase, PlacelessSetup):
+class TestProvider(unittest.TestCase):
def setUp(self):
- PlacelessSetup.setUp(self)
+ from repoze.bfg.configuration import Configurator
+ self.config = Configurator()
+ self.config.hook_zca()
def tearDown(self):
- PlacelessSetup.tearDown(self)
+ self.config.end()
def _getTargetClass(self):
from repoze.bfg.viewgroup.group import Provider
@@ -154,59 +117,51 @@ def _getTargetClass(self):
def _makeOne(self, context, request):
return self._getTargetClass()(context, request)
- def _registerView(self, app, name, *for_):
- import zope.component
- gsm = zope.component.getGlobalSiteManager()
- from repoze.bfg.interfaces import IView
- gsm.registerAdapter(app, for_, IView, name)
+ def _registerView(self, view, name):
+ self.config.add_view(view, name=name)
def test_call(self):
response1 = DummyResponse()
response1.app_iter = ['Response1']
view1 = make_view(response1)
- self._registerView(view1, 'view1', None, None)
+ self._registerView(view1, 'view1')
response2 = DummyResponse()
response2.app_iter = ['Response2']
view2 = make_view(response2)
- self._registerView(view2, 'view2', None, None)
+ self._registerView(view2, 'view2')
from repoze.bfg.viewgroup.group import ViewGroup
group = ViewGroup('viewgroup', ['view1', 'view2'])
- self._registerView(group, 'viewgroup', None, None)
+ self._registerView(group, 'viewgroup')
context = DummyContext()
request = DummyRequest()
+ request.registry = self.config.registry
provider = self._makeOne(context, request)
self.assertEqual(provider('view1'), 'Response1')
self.assertEqual(provider('view2'), 'Response2')
self.assertEqual(provider('viewgroup'), 'Response1Response2')
-class TestFixtureApp(unittest.TestCase, PlacelessSetup):
+
+class TestFixtureApp(unittest.TestCase):
def setUp(self):
- PlacelessSetup.setUp(self)
+ from repoze.bfg.configuration import Configurator
+ from repoze.bfg.viewgroup.tests import fixtureapp
+ self.config = Configurator(package=fixtureapp)
+ self.config.hook_zca()
def tearDown(self):
- PlacelessSetup.tearDown(self)
-
- def test_registry_actions_can_be_pickled_and_unpickled(self):
- import repoze.bfg.viewgroup.tests.fixtureapp as package
- from zope.configuration import config
- from zope.configuration import xmlconfig
- context = config.ConfigurationMachine()
- xmlconfig.registerCommonDirectives(context)
- context.package = package
- xmlconfig.include(context, 'configure.zcml', package)
- context.execute_actions(clear=False)
- actions = context.actions
- import cPickle
- dumped = cPickle.dumps(actions, -1)
- new = cPickle.loads(dumped)
- self.assertEqual(len(actions), len(new))
+ self.config.end()
+
+ def test_it(self):
+ self.config.load_zcml('configure.zcml')
class DummyRequest:
- pass
+ from zope.interface import implements
+ from repoze.bfg.interfaces import IRequest
+ implements(IRequest)
class DummyContext:
pass
@@ -218,6 +173,7 @@ class Dummy:
pass
class DummyContext:
+ package = None
def __init__(self):
self.actions = []
self.info = None
@@ -228,11 +184,10 @@ def path(self, name):
fixtures = os.path.join(here, 'fixtures')
return os.path.join(fixtures, name)
- def action(self, discriminator, callable, args):
+ def action(self, discriminator, callable):
self.actions.append(
{'discriminator':discriminator,
- 'callable':callable,
- 'args':args}
+ 'callable':callable}
)
class IDummy(Interface):
View
43 repoze/bfg/viewgroup/zcml.py
@@ -1,55 +1,48 @@
from zope.interface import Interface
-from zope.component.interface import provideInterface
-from zope.component import getSiteManager
from zope.configuration.exceptions import ConfigurationError
from zope.configuration.fields import GlobalObject
from zope.configuration.fields import Tokens
from zope.schema import TextLine
-from repoze.bfg.interfaces import IRequest
from repoze.bfg.interfaces import IView
+from repoze.bfg.configuration import Configurator
from repoze.bfg.viewgroup.group import ViewGroup
+from repoze.bfg.threadlocal import get_current_registry
"""
<bfg:viewgroup
name="headers"
viewnames="header1 header2 header3"
for=".interfaces.IContent"
- request_type="repoze.bfg.interfaces.IRequest"
/>
"""
-def handler(methodName, *args, **kwargs):
- method = getattr(getSiteManager(), methodName)
- method(*args, **kwargs)
-
def viewgroup(_context,
name="",
viewnames=None,
for_=None,
- request_type=IRequest,
):
if not viewnames:
raise ConfigurationError('"viewnames" attribute was not specified')
- if for_ is not None:
- _context.action(
- discriminator = None,
- callable = provideInterface,
- args = ('', for_)
- )
-
viewgroup = ViewGroup(name, viewnames)
+ reg = get_current_registry()
+
+ def register():
+ config = Configurator(reg, package=_context.package)
+ config.add_view(viewgroup, name=name, context=for_, _info=_context.info)
+
+ discriminator = ('view', for_, name, None, IView, None,
+ None, None, None, None, None, None, None, None)
+
+
_context.action(
- discriminator = ('view', for_, name, request_type, IView),
- callable = handler,
- args = ('registerAdapter',
- viewgroup, (for_, request_type), IView, name,
- _context.info),
+ discriminator = discriminator,
+ callable = register,
)
class IViewGroupDirective(Interface):
@@ -72,11 +65,3 @@ class IViewGroupDirective(Interface):
value_type=TextLine(),
)
- request_type = GlobalObject(
- title=u"""The request type interface for the viewgroup""",
- description=(u"The viewgroup will be called if the interface "
- u"represented by 'request_type' is implemented by the "
- u"request. The default request type is "
- u"'repoze.bfg.interfaces.IRequest'"),
- required=False
- )
View
8 setup.cfg
@@ -1,3 +1,9 @@
[easy_install]
-index_url = http://dist.repoze.org/lemonade/dev/simple
zip_ok = false
+
+[nosetests]
+match=^test
+where=repoze/bfg/viewgroup
+nocapture=1
+cover-package=repoze.bfg.viewgroup
+cover-erase=1

0 comments on commit 1d37e25

Please sign in to comment.
Something went wrong with that request. Please try again.