Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added support for passing multiple decorators to add_view.

  • Loading branch information...
commit 6454a73a9ea0b00f3881e003efda3048e8407a5f 1 parent 71b4737
rforkel authored
20 pyramid/config/views.py
@@ -807,11 +807,12 @@ def add_view(self, view=None, name="", for_=None, permission=None,
807 807
808 808 decorator
809 809
810   - A :term:`dotted Python name` to function (or the function itself)
  810 + A :term:`dotted Python name` to function (or the function itself,
  811 + or a list or tuple of the aforementioned)
811 812 which will be used to decorate the registered :term:`view
812   - callable`. The decorator function will be called with the view
  813 + callable`. The decorator function(s) will be called with the view
813 814 callable as a single argument. The view callable it is passed will
814   - accept ``(context, request)``. The decorator must return a
  815 + accept ``(context, request)``. The decorator(s) must return a
815 816 replacement view callable which also accepts ``(context,
816 817 request)``.
817 818
@@ -979,7 +980,18 @@ def add_view(self, view=None, name="", for_=None, permission=None,
979 980 for_ = self.maybe_dotted(for_)
980 981 containment = self.maybe_dotted(containment)
981 982 mapper = self.maybe_dotted(mapper)
982   - decorator = self.maybe_dotted(decorator)
  983 +
  984 + def combine(*decorators):
  985 + def decorated(view_callable):
  986 + for decorator in decorators:
  987 + view_callable = decorator(view_callable)
  988 + return view_callable
  989 + return decorated
  990 +
  991 + if isinstance(decorator, (tuple, list)):
  992 + decorator = combine(*map(self.maybe_dotted, decorator))
  993 + else:
  994 + decorator = self.maybe_dotted(decorator)
983 995
984 996 if not view:
985 997 if renderer:
2  pyramid/scaffolds/copydir.py
@@ -245,7 +245,7 @@ def out(msg):
245 245 def makedirs(dir, verbosity, pad):
246 246 parent = os.path.dirname(os.path.abspath(dir))
247 247 if not os.path.exists(parent):
248   - makedirs(parent, verbosity, pad)
  248 + makedirs(parent, verbosity, pad) # pragma: no cover
249 249 os.mkdir(dir)
250 250
251 251 def substitute_filename(fn, vars):
22 pyramid/tests/test_config/test_views.py
@@ -184,6 +184,28 @@ def inner(context, request):
184 184 result = wrapper(None, None)
185 185 self.assertEqual(result, 'OK')
186 186
  187 + def test_add_view_with_decorator_tuple(self):
  188 + from pyramid.renderers import null_renderer
  189 + def view(request):
  190 + """ ABC """
  191 + return 'OK'
  192 + def view_wrapper1(fn):
  193 + def inner(context, request):
  194 + return 'wrapped1' + fn(context, request)
  195 + return inner
  196 + def view_wrapper2(fn):
  197 + def inner(context, request):
  198 + return 'wrapped2' + fn(context, request)
  199 + return inner
  200 + config = self._makeOne(autocommit=True)
  201 + config.add_view(view=view, decorator=(view_wrapper1, view_wrapper2),
  202 + renderer=null_renderer)
  203 + wrapper = self._getViewCallable(config)
  204 + self.assertFalse(wrapper is view)
  205 + self.assertEqual(wrapper.__doc__, view.__doc__)
  206 + result = wrapper(None, None)
  207 + self.assertEqual(result, 'wrapped2wrapped1OK')
  208 +
187 209 def test_add_view_with_http_cache(self):
188 210 import datetime
189 211 from pyramid.response import Response
4 pyramid/tests/test_view.py
@@ -371,6 +371,10 @@ def test_create_nondefaults(self):
371 371 self.assertEqual(decorator.mapper, 'mapper')
372 372 self.assertEqual(decorator.decorator, 'decorator')
373 373 self.assertEqual(decorator.match_param, 'match_param')
  374 +
  375 + def test_create_decorator_tuple(self):
  376 + decorator = self._makeOne(decorator=('decorator1', 'decorator2'))
  377 + self.assertEqual(decorator.decorator, ('decorator1', 'decorator2'))
374 378
375 379 def test_call_function(self):
376 380 decorator = self._makeOne()

0 comments on commit 6454a73

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