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 from datetime import datetime |
18 |
19 |
19 # import interfaces |
20 # import interfaces |
20 from pyams_cache.interfaces import ICacheHandler |
21 from pyams_cache.interfaces import ICacheHandler |
21 from pyams_notify.interfaces import CACHE_QUEUE_KEY, CACHE_CONFIGURATION_KEY |
22 from pyams_notify.interfaces import CACHE_QUEUE_KEY, CACHE_CONFIGURATION_KEY |
22 from pyams_skin.layer import IPyAMSLayer |
23 from pyams_skin.layer import IPyAMSLayer |
|
24 from pyams_utils.date import format_datetime |
|
25 from pyams_utils.timezone import tztime |
23 |
26 |
24 # import packages |
27 # import packages |
25 from pyams_cache.cache import get_cache_handler |
28 from pyams_cache.cache import get_cache_handler |
26 from pyramid.view import view_config |
29 from pyramid.view import view_config |
27 |
30 |
28 |
31 |
29 def filtered(notification, request): |
32 def get_user_notifications(cache_server, request): |
30 """Filter notification against current request""" |
33 """Get notifications for given request""" |
31 return request.principal.id in notification['target'].get('principals', ()) |
34 |
|
35 def filtered(notification, request): |
|
36 """Filter notification against current request""" |
|
37 target = notification.pop('target', {}) |
|
38 return request.principal.id in target.get('principals', ()) |
|
39 |
|
40 timestamp = format_datetime(tztime(datetime.utcnow()), request=request) |
|
41 if cache_server is not None: |
|
42 client = get_cache_handler(cache_server, ICacheHandler) |
|
43 if client is None: |
|
44 return {'timestamp': timestamp, |
|
45 'notifications': ()} |
|
46 notifications = client.get(CACHE_QUEUE_KEY) |
|
47 if notifications is None: |
|
48 return {'timestamp': timestamp, |
|
49 'notifications': ()} |
|
50 else: |
|
51 return {'timestamp': timestamp, |
|
52 'notifications': [n for n in reversed(list(filter(lambda x: filtered(x, request), |
|
53 pickle.loads(notifications))))]} |
|
54 else: |
|
55 return {'timestamp': timestamp, |
|
56 'notifications': ()} |
32 |
57 |
33 |
58 |
34 @view_config(name='get-user-notifications.json', request_type=IPyAMSLayer, renderer='json', xhr=True) |
59 @view_config(name='get-user-notifications.json', request_type=IPyAMSLayer, renderer='json', xhr=True) |
35 class UserNotificationsView(object): |
60 class UserNotificationsView(object): |
36 """User notifications view""" |
61 """User notifications view""" |
42 @property |
67 @property |
43 def cache_server(self): |
68 def cache_server(self): |
44 return self.request.registry.settings.get(CACHE_CONFIGURATION_KEY) |
69 return self.request.registry.settings.get(CACHE_CONFIGURATION_KEY) |
45 |
70 |
46 def __call__(self): |
71 def __call__(self): |
47 server = self.cache_server |
72 return get_user_notifications(self.cache_server, self.request) |
48 if server is not None: |
|
49 client = get_cache_handler(server, ICacheHandler) |
|
50 if client is None: |
|
51 return () |
|
52 notifications = client.get(CACHE_QUEUE_KEY) |
|
53 if notifications is None: |
|
54 return () |
|
55 else: |
|
56 return [n for n in reversed(list(filter(lambda x: filtered(x, self.request), |
|
57 pickle.loads(notifications))))] |
|
58 else: |
|
59 return () |
|