Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- Make compatible with BFG 1.2.

  • Loading branch information...
commit 1d37e25b735e8c2e4602bf65817871e787be82f4 1 parent 6c0f9f1
Chris McDonough authored
11 CHANGES.txt
... ... @@ -1,9 +1,16 @@
  1 +Next release
  2 +------------
  3 +
  4 +- Make compatible with BFG 1.2.
  5 +
1 6 0.2
  7 +---
2 8
3   - Changes to support upcoming bfg release (which does not require
  9 +- Changes to support upcoming bfg release (which does not require
4 10 zope.security, so we don't either).
5 11
6 12 0.1
  13 +---
7 14
8   - Initial release.
  15 +- Initial release.
9 16
2  COPYRIGHT.txt
... ... @@ -1,3 +1,3 @@
26 README.txt
@@ -54,10 +54,9 @@ Create a ``bfg:viewgroup`` registration in ZCML via the
54 54 name="tabs_group"
55 55 viewnames="login_tab content_tab"
56 56 for=".interfaces.ISite"
57   - request_type="repoze.bfg.interfaces.IRequest"
58 57 />
59 58
60   -The consitutient attributes of this directive are:
  59 +The consituent attributes of this directive are:
61 60
62 61 ``name`` -- The name by which the viewgroup can be found via traversal
63 62 or programmatic view execution. This attribute has the same meaning
@@ -71,18 +70,13 @@ The consitutient attributes of this directive are:
71 70 This attribute has the same meaning as its counterpart in
72 71 ``bfg:view`` directive.
73 72
74   -``request_type`` -- The "model" interface which this view is
75   - registered for. This attribute has the same meaning as its
76   - counterpart in ``bfg:view`` directive.
  73 +The ``for`` argument is optional; it defaults to ``None``. The
  74 +``name`` argument is also optional. It defaults to the empty string
  75 +(indicating the default view).
77 76
78   -The ``for`` and ``request_type`` arguments are optional; they default
79   -to ``zope.interface.Interface`` and ``repoze.bfg.interfaces.IRequest``
80   -respectively. The ``name`` argument is also optional. It defaults to
81   -the empty string (indicating the default view).
82   -
83   -Viewgroups registered wih a ``name``, ``for`` and ``request_type``
84   -will conflict with views registered with the same arguments, so it's
85   -wise to name your viewgroups differently than your views.
  77 +Viewgroups registered wih a ``name`` and a ``for`` and will conflict
  78 +with views registered with the same arguments, so it's wise to name
  79 +your viewgroups differently than your views.
86 80
87 81 A viewgroup can refer to another viewgroup in its ``viewname``
88 82 argument as necessary (although this is insane).
@@ -106,11 +100,13 @@ instance of ``Provider`` can be constructed and passed into a template
106 100 rendering so it may be used ala the ``provider:`` expression type in
107 101 Zope 3. For example, this view might render a template::
108 102
  103 + from repoze.bfg.view import bfg_view
  104 +
  105 + @bfg_view(template='templates/mytemplate.pt')
109 106 def myview(context, request):
110 107 from repoze.bfg.viewgroup.group import Provider
111 108 provider = Provider(context, request)
112   - return render_template_to_response('templates/mytemplate.pt',
113   - provider=provider)
  109 + return {'provider':provider}
114 110
115 111 The template being rendered can use the provider to "fill slots" by
116 112 passing in view or viewgroup names as necessary, e.g.::
1  TODO.txt
... ... @@ -1 +0,0 @@
1   -List todo items here.
9 repoze/bfg/viewgroup/group.py
... ... @@ -1,17 +1,12 @@
1 1 import itertools
2 2
3   -from zope.interface import implements
4   -
5 3 from webob import Response
6 4
7   -from repoze.bfg.interfaces import IView
8   -from repoze.bfg.security import Unauthorized
  5 +from repoze.bfg.exceptions import Forbidden
9 6 from repoze.bfg.view import render_view_to_iterable
10 7 from repoze.bfg.view import render_view
11 8
12 9 class ViewGroup(object):
13   - implements(IView)
14   -
15 10 def __init__(self, name, viewnames):
16 11 self.name = name
17 12 self.viewnames = viewnames
@@ -22,7 +17,7 @@ def __call__(self, context, request):
22 17 for viewname in self.viewnames:
23 18 try:
24 19 iterable = render_view_to_iterable(context, request, viewname)
25   - except Unauthorized:
  20 + except Forbidden:
26 21 continue
27 22 if iterable is None:
28 23 raise ValueError(
1  repoze/bfg/viewgroup/tests/fixtureapp/configure.zcml
@@ -21,7 +21,6 @@
21 21 name="group"
22 22 viewnames="one two"
23 23 for=".models.IFixture"
24   - request_type=".views.IDummy"
25 24 />
26 25
27 26 </configure>
167 repoze/bfg/viewgroup/tests/test_all.py
... ... @@ -1,14 +1,15 @@
1 1 import unittest
2 2
3   -from zope.component.testing import PlacelessSetup
4 3 from zope.interface import Interface
5 4
6   -class TestViewGroupDirective(unittest.TestCase, PlacelessSetup):
  5 +class TestViewGroupDirective(unittest.TestCase):
7 6 def setUp(self):
8   - PlacelessSetup.setUp(self)
  7 + from repoze.bfg.configuration import Configurator
  8 + self.config = Configurator()
  9 + self.config.hook_zca()
9 10
10 11 def tearDown(self):
11   - PlacelessSetup.tearDown(self)
  12 + self.config.end()
12 13
13 14 def _getFUT(self):
14 15 from repoze.bfg.viewgroup.zcml import viewgroup
@@ -20,91 +21,52 @@ def test_no_viewnames(self):
20 21 context = DummyContext()
21 22 self.assertRaises(ConfigurationError, f, context)
22 23
23   - def test_only_viewnames(self):
24   - f = self._getFUT()
25   - context = DummyContext()
26   - f(context, 'viewgroup', ['a', 'b', 'c'])
27   - actions = context.actions
28   - from repoze.bfg.interfaces import IRequest
  24 + def test_call(self):
29 25 from repoze.bfg.interfaces import IView
30   - from repoze.bfg.viewgroup.zcml import handler
31   -
32   - self.assertEqual(len(actions), 1)
33   -
34   - regadapt = actions[0]
35   - regadapt_discriminator = ('view', None, 'viewgroup', IRequest, IView)
36   - self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
37   - self.assertEqual(regadapt['callable'], handler)
38   - self.assertEqual(regadapt['args'][0], 'registerAdapter')
39   - self.assertEqual(regadapt['args'][1].name, 'viewgroup')
40   - self.assertEqual(regadapt['args'][1].viewnames, ['a', 'b', 'c'])
41   - self.assertEqual(regadapt['args'][2], (None, IRequest))
42   - self.assertEqual(regadapt['args'][3], IView)
43   - self.assertEqual(regadapt['args'][4], 'viewgroup')
44   - self.assertEqual(regadapt['args'][5], None)
45   -
46   - def test_with_request_type(self):
  26 + from repoze.bfg.interfaces import IRequest
47 27 f = self._getFUT()
48 28 context = DummyContext()
49 29 class IFoo:
50 30 pass
51 31 def view(context, request):
52 32 pass
53   - f(context, 'viewgroup', ['a', 'b', 'c'], IFoo, request_type=IDummy)
  33 + f(context, 'viewgroup', ['a', 'b', 'c'], IFoo)
54 34 actions = context.actions
55   - from repoze.bfg.interfaces import IView
56   - from repoze.bfg.viewgroup.zcml import handler
57   - from zope.component.interface import provideInterface
58   -
59   - self.assertEqual(len(actions), 2)
60   -
61   - provide = actions[0]
62   - self.assertEqual(provide['discriminator'], None)
63   - self.assertEqual(provide['callable'], provideInterface)
64   - self.assertEqual(provide['args'][0], '')
65   - self.assertEqual(provide['args'][1], IFoo)
66   -
67   - regadapt = actions[1]
68   - regadapt_discriminator = ('view', IFoo, 'viewgroup', IDummy, IView)
69   - self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
70   - self.assertEqual(regadapt['callable'], handler)
71   - self.assertEqual(regadapt['args'][0], 'registerAdapter')
72   - self.assertEqual(regadapt['args'][1].name, 'viewgroup')
73   - self.assertEqual(regadapt['args'][1].viewnames, ['a', 'b', 'c'])
74   - self.assertEqual(regadapt['args'][2], (IFoo, IDummy))
75   - self.assertEqual(regadapt['args'][3], IView)
76   - self.assertEqual(regadapt['args'][4], 'viewgroup')
77   - self.assertEqual(regadapt['args'][5], None)
78   -
79   -class TestViewGroup(unittest.TestCase, PlacelessSetup):
  35 +
  36 + self.assertEqual(len(actions), 1)
  37 +
  38 + action = actions[0]
  39 + registrar = action['callable']
  40 + registrar()
  41 + reg = self.config.registry
  42 + wrapper = reg.adapters.lookup((IRequest, Interface), IView,
  43 + name='viewgroup')
  44 + self.assertEqual(wrapper, None)
  45 +
  46 +class TestViewGroup(unittest.TestCase):
80 47 def setUp(self):
81   - PlacelessSetup.setUp(self)
  48 + from repoze.bfg.configuration import Configurator
  49 + self.config = Configurator()
  50 + self.config.begin()
  51 + self.config.hook_zca()
82 52
83 53 def tearDown(self):
84   - PlacelessSetup.tearDown(self)
  54 + self.config.end()
85 55
86 56 def _getTargetClass(self):
87 57 from repoze.bfg.viewgroup.group import ViewGroup
88 58 return ViewGroup
89 59
90   - def _registerSecurityPolicy(self, secpol):
91   - import zope.component
92   - gsm = zope.component.getGlobalSiteManager()
93   - from repoze.bfg.interfaces import ISecurityPolicy
94   - gsm.registerUtility(secpol, ISecurityPolicy)
  60 + def _registerSecurityPolicy(self):
  61 + self.config.testing_securitypolicy()
95 62
96   - def _registerView(self, app, name, *for_):
97   - import zope.component
98   - gsm = zope.component.getGlobalSiteManager()
99   - from repoze.bfg.interfaces import IView
100   - gsm.registerAdapter(app, for_, IView, name)
  63 + def _registerView(self, view, name):
  64 + self.config.add_view(view, name=name)
101 65
102 66 def _makeOne(self, name, viewnames):
103 67 return self._getTargetClass()(name, viewnames)
104 68
105 69 def test_no_viewnames(self):
106   - secpol = DummySecurityPolicy()
107   - self._registerSecurityPolicy(secpol)
108 70 group = self._makeOne('viewgroup', [])
109 71 context = DummyContext()
110 72 request = DummyRequest()
@@ -112,8 +74,7 @@ def test_no_viewnames(self):
112 74 self.assertEqual(''.join(response.app_iter), '')
113 75
114 76 def test_viewname_not_found(self):
115   - secpol = DummySecurityPolicy()
116   - self._registerSecurityPolicy(secpol)
  77 + self._registerSecurityPolicy()
117 78
118 79 group = self._makeOne('viewgroup', ['view1'])
119 80 context = DummyContext()
@@ -121,31 +82,33 @@ def test_viewname_not_found(self):
121 82 self.assertRaises(ValueError, group, context, request)
122 83
123 84 def test_all_permitted(self):
124   - secpol = DummySecurityPolicy()
125   - self._registerSecurityPolicy(secpol)
  85 + self._registerSecurityPolicy()
126 86
127 87 response1 = DummyResponse()
128 88 response1.app_iter = ['Response1']
129 89 view1 = make_view(response1)
130   - self._registerView(view1, 'view1', None, None)
  90 + self._registerView(view1, 'view1')
131 91
132 92 response2 = DummyResponse()
133 93 response2.app_iter = ['Response2']
134 94 view2 = make_view(response2)
135   - self._registerView(view2, 'view2', None, None)
  95 + self._registerView(view2, 'view2')
136 96
137 97 group = self._makeOne('viewgroup', ['view1', 'view2'])
138 98 context = DummyContext()
139 99 request = DummyRequest()
  100 + request.registry = self.config.registry
140 101 response = group(context, request)
141 102 self.assertEqual(''.join(response.app_iter), 'Response1Response2')
142 103
143   -class TestProvider(unittest.TestCase, PlacelessSetup):
  104 +class TestProvider(unittest.TestCase):
144 105 def setUp(self):
145   - PlacelessSetup.setUp(self)
  106 + from repoze.bfg.configuration import Configurator
  107 + self.config = Configurator()
  108 + self.config.hook_zca()
146 109
147 110 def tearDown(self):
148   - PlacelessSetup.tearDown(self)
  111 + self.config.end()
149 112
150 113 def _getTargetClass(self):
151 114 from repoze.bfg.viewgroup.group import Provider
@@ -154,59 +117,51 @@ def _getTargetClass(self):
154 117 def _makeOne(self, context, request):
155 118 return self._getTargetClass()(context, request)
156 119
157   - def _registerView(self, app, name, *for_):
158   - import zope.component
159   - gsm = zope.component.getGlobalSiteManager()
160   - from repoze.bfg.interfaces import IView
161   - gsm.registerAdapter(app, for_, IView, name)
  120 + def _registerView(self, view, name):
  121 + self.config.add_view(view, name=name)
162 122
163 123 def test_call(self):
164 124 response1 = DummyResponse()
165 125 response1.app_iter = ['Response1']
166 126 view1 = make_view(response1)
167   - self._registerView(view1, 'view1', None, None)
  127 + self._registerView(view1, 'view1')
168 128
169 129 response2 = DummyResponse()
170 130 response2.app_iter = ['Response2']
171 131 view2 = make_view(response2)
172   - self._registerView(view2, 'view2', None, None)
  132 + self._registerView(view2, 'view2')
173 133
174 134 from repoze.bfg.viewgroup.group import ViewGroup
175 135
176 136 group = ViewGroup('viewgroup', ['view1', 'view2'])
177   - self._registerView(group, 'viewgroup', None, None)
  137 + self._registerView(group, 'viewgroup')
178 138
179 139 context = DummyContext()
180 140 request = DummyRequest()
  141 + request.registry = self.config.registry
181 142 provider = self._makeOne(context, request)
182 143 self.assertEqual(provider('view1'), 'Response1')
183 144 self.assertEqual(provider('view2'), 'Response2')
184 145 self.assertEqual(provider('viewgroup'), 'Response1Response2')
185 146
186   -class TestFixtureApp(unittest.TestCase, PlacelessSetup):
  147 +
  148 +class TestFixtureApp(unittest.TestCase):
187 149 def setUp(self):
188   - PlacelessSetup.setUp(self)
  150 + from repoze.bfg.configuration import Configurator
  151 + from repoze.bfg.viewgroup.tests import fixtureapp
  152 + self.config = Configurator(package=fixtureapp)
  153 + self.config.hook_zca()
189 154
190 155 def tearDown(self):
191   - PlacelessSetup.tearDown(self)
192   -
193   - def test_registry_actions_can_be_pickled_and_unpickled(self):
194   - import repoze.bfg.viewgroup.tests.fixtureapp as package
195   - from zope.configuration import config
196   - from zope.configuration import xmlconfig
197   - context = config.ConfigurationMachine()
198   - xmlconfig.registerCommonDirectives(context)
199   - context.package = package
200   - xmlconfig.include(context, 'configure.zcml', package)
201   - context.execute_actions(clear=False)
202   - actions = context.actions
203   - import cPickle
204   - dumped = cPickle.dumps(actions, -1)
205   - new = cPickle.loads(dumped)
206   - self.assertEqual(len(actions), len(new))
  156 + self.config.end()
  157 +
  158 + def test_it(self):
  159 + self.config.load_zcml('configure.zcml')
207 160
208 161 class DummyRequest:
209   - pass
  162 + from zope.interface import implements
  163 + from repoze.bfg.interfaces import IRequest
  164 + implements(IRequest)
210 165
211 166 class DummyContext:
212 167 pass
@@ -218,6 +173,7 @@ class Dummy:
218 173 pass
219 174
220 175 class DummyContext:
  176 + package = None
221 177 def __init__(self):
222 178 self.actions = []
223 179 self.info = None
@@ -228,11 +184,10 @@ def path(self, name):
228 184 fixtures = os.path.join(here, 'fixtures')
229 185 return os.path.join(fixtures, name)
230 186
231   - def action(self, discriminator, callable, args):
  187 + def action(self, discriminator, callable):
232 188 self.actions.append(
233 189 {'discriminator':discriminator,
234   - 'callable':callable,
235   - 'args':args}
  190 + 'callable':callable}
236 191 )
237 192
238 193 class IDummy(Interface):
43 repoze/bfg/viewgroup/zcml.py
... ... @@ -1,6 +1,4 @@
1 1 from zope.interface import Interface
2   -from zope.component.interface import provideInterface
3   -from zope.component import getSiteManager
4 2
5 3 from zope.configuration.exceptions import ConfigurationError
6 4 from zope.configuration.fields import GlobalObject
@@ -8,48 +6,43 @@
8 6
9 7 from zope.schema import TextLine
10 8
11   -from repoze.bfg.interfaces import IRequest
12 9 from repoze.bfg.interfaces import IView
13 10
  11 +from repoze.bfg.configuration import Configurator
14 12 from repoze.bfg.viewgroup.group import ViewGroup
  13 +from repoze.bfg.threadlocal import get_current_registry
15 14
16 15 """
17 16 <bfg:viewgroup
18 17 name="headers"
19 18 viewnames="header1 header2 header3"
20 19 for=".interfaces.IContent"
21   - request_type="repoze.bfg.interfaces.IRequest"
22 20 />
23 21 """
24 22
25   -def handler(methodName, *args, **kwargs):
26   - method = getattr(getSiteManager(), methodName)
27   - method(*args, **kwargs)
28   -
29 23 def viewgroup(_context,
30 24 name="",
31 25 viewnames=None,
32 26 for_=None,
33   - request_type=IRequest,
34 27 ):
35 28
36 29 if not viewnames:
37 30 raise ConfigurationError('"viewnames" attribute was not specified')
38 31
39   - if for_ is not None:
40   - _context.action(
41   - discriminator = None,
42   - callable = provideInterface,
43   - args = ('', for_)
44   - )
45   -
46 32 viewgroup = ViewGroup(name, viewnames)
  33 + reg = get_current_registry()
  34 +
  35 + def register():
  36 + config = Configurator(reg, package=_context.package)
  37 + config.add_view(viewgroup, name=name, context=for_, _info=_context.info)
  38 +
  39 + discriminator = ('view', for_, name, None, IView, None,
  40 + None, None, None, None, None, None, None, None)
  41 +
  42 +
47 43 _context.action(
48   - discriminator = ('view', for_, name, request_type, IView),
49   - callable = handler,
50   - args = ('registerAdapter',
51   - viewgroup, (for_, request_type), IView, name,
52   - _context.info),
  44 + discriminator = discriminator,
  45 + callable = register,
53 46 )
54 47
55 48 class IViewGroupDirective(Interface):
@@ -72,11 +65,3 @@ class IViewGroupDirective(Interface):
72 65 value_type=TextLine(),
73 66 )
74 67
75   - request_type = GlobalObject(
76   - title=u"""The request type interface for the viewgroup""",
77   - description=(u"The viewgroup will be called if the interface "
78   - u"represented by 'request_type' is implemented by the "
79   - u"request. The default request type is "
80   - u"'repoze.bfg.interfaces.IRequest'"),
81   - required=False
82   - )
8 setup.cfg
... ... @@ -1,3 +1,9 @@
1 1 [easy_install]
2   -index_url = http://dist.repoze.org/lemonade/dev/simple
3 2 zip_ok = false
  3 +
  4 +[nosetests]
  5 +match=^test
  6 +where=repoze/bfg/viewgroup
  7 +nocapture=1
  8 +cover-package=repoze.bfg.viewgroup
  9 +cover-erase=1

0 comments on commit 1d37e25

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