13 __docformat__ = 'restructuredtext' |
13 __docformat__ = 'restructuredtext' |
14 |
14 |
15 |
15 |
16 # import standard library |
16 # import standard library |
17 import pickle |
17 import pickle |
18 try: |
|
19 import pylibmc |
|
20 except ImportError: |
|
21 pylibmc = None |
|
22 |
18 |
23 # import interfaces |
19 # import interfaces |
24 from pyams_notify.interfaces import MEMCACHED_QUEUE_KEY |
20 from pyams_cache.interfaces import ICacheHandler |
|
21 from pyams_notify.interfaces import CACHE_QUEUE_KEY, CACHE_CONFIGURATION_KEY |
25 from pyams_skin.layer import IPyAMSLayer |
22 from pyams_skin.layer import IPyAMSLayer |
26 |
23 |
27 # import packages |
24 # import packages |
|
25 from pyams_cache.cache import get_cache_handler |
28 from pyramid.view import view_config |
26 from pyramid.view import view_config |
29 |
27 |
30 |
28 |
31 def filtered(notification, request): |
29 def filtered(notification, request): |
32 """Filter notification against current request""" |
30 """Filter notification against current request""" |
40 def __init__(self, request): |
38 def __init__(self, request): |
41 self.request = request |
39 self.request = request |
42 self.context = request.context |
40 self.context = request.context |
43 |
41 |
44 @property |
42 @property |
45 def memcached_server(self): |
43 def cache_server(self): |
46 return self.request.registry.settings.get('pyams_notify_ws.memcached_server') |
44 return self.request.registry.settings.get(CACHE_CONFIGURATION_KEY) |
47 |
45 |
48 def __call__(self): |
46 def __call__(self): |
49 if pylibmc is None: |
47 server = self.cache_server |
50 return () |
|
51 server = self.memcached_server |
|
52 if server is not None: |
48 if server is not None: |
53 client = pylibmc.Client([server]) |
49 client = get_cache_handler(server, ICacheHandler) |
54 notifications = client.get(MEMCACHED_QUEUE_KEY) |
50 if client is None: |
|
51 return () |
|
52 notifications = client.get(CACHE_QUEUE_KEY) |
55 if notifications is None: |
53 if notifications is None: |
56 return () |
54 return () |
57 else: |
55 else: |
58 return [n for n in reversed(list(filter(lambda x: filtered(x, self.request), |
56 return [n for n in reversed(list(filter(lambda x: filtered(x, self.request), |
59 pickle.loads(notifications))))] |
57 pickle.loads(notifications))))] |