Michael Merickel
2018-10-15 2b024920847481592b1a13d4006d2a9fa8881d72
commit | author | age
9e5c8a 1 from pyramid.view import view_config
CM 2 from pyramid.events import subscriber
3
4 class Yup(object):
5     def __init__(self, val, config):
6         self.val = val
7
8     def text(self):
9         return 'path_startswith = %s' % (self.val,)
10
11     phash = text
12
13     def __call__(self, event):
14         return getattr(event.response, 'yup', False)
15
16 class Foo(object):
17     def __init__(self, response):
18         self.response = response
19
20 class Bar(object):
21     pass
22
23 @subscriber(Foo)
24 def foo(event):
25     event.response.text += 'foo '
26
27 @subscriber(Foo, yup=True)
28 def fooyup(event):
29     event.response.text += 'fooyup '
30     
31 @subscriber([Foo, Bar])
32 def foobar(event):
33     event.response.text += 'foobar '
34
35 @subscriber([Foo, Bar])
36 def foobar2(event, context):
37     event.response.text += 'foobar2 '
38
39 @subscriber([Foo, Bar], yup=True)
40 def foobaryup(event):
41     event.response.text += 'foobaryup '
42
43 @subscriber([Foo, Bar], yup=True)
44 def foobaryup2(event, context):
45     event.response.text += 'foobaryup2 '
46
47 @view_config(name='sendfoo')
48 def sendfoo(request):
49     response = request.response
50     response.yup = True
51     request.registry.notify(Foo(response))
52     return response
53
54 @view_config(name='sendfoobar')
55 def sendfoobar(request):
56     response = request.response
57     response.yup = True
58     request.registry.notify(Foo(response), Bar())
59     return response
60
61 def includeme(config):
62     config.add_subscriber_predicate('yup', Yup)
63     config.scan('pyramid.tests.pkgs.eventonly')
64