--- a/src/pyams_notify/views/notification.py Wed Jul 12 12:11:43 2017 +0200
+++ b/src/pyams_notify/views/notification.py Wed Jul 12 12:12:08 2017 +0200
@@ -15,20 +15,45 @@
# import standard library
import pickle
+from datetime import datetime
# import interfaces
from pyams_cache.interfaces import ICacheHandler
from pyams_notify.interfaces import CACHE_QUEUE_KEY, CACHE_CONFIGURATION_KEY
from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.date import format_datetime
+from pyams_utils.timezone import tztime
# import packages
from pyams_cache.cache import get_cache_handler
from pyramid.view import view_config
-def filtered(notification, request):
- """Filter notification against current request"""
- return request.principal.id in notification['target'].get('principals', ())
+def get_user_notifications(cache_server, request):
+ """Get notifications for given request"""
+
+ def filtered(notification, request):
+ """Filter notification against current request"""
+ target = notification.pop('target', {})
+ return request.principal.id in target.get('principals', ())
+
+ timestamp = format_datetime(tztime(datetime.utcnow()), request=request)
+ if cache_server is not None:
+ client = get_cache_handler(cache_server, ICacheHandler)
+ if client is None:
+ return {'timestamp': timestamp,
+ 'notifications': ()}
+ notifications = client.get(CACHE_QUEUE_KEY)
+ if notifications is None:
+ return {'timestamp': timestamp,
+ 'notifications': ()}
+ else:
+ return {'timestamp': timestamp,
+ 'notifications': [n for n in reversed(list(filter(lambda x: filtered(x, request),
+ pickle.loads(notifications))))]}
+ else:
+ return {'timestamp': timestamp,
+ 'notifications': ()}
@view_config(name='get-user-notifications.json', request_type=IPyAMSLayer, renderer='json', xhr=True)
@@ -44,16 +69,4 @@
return self.request.registry.settings.get(CACHE_CONFIGURATION_KEY)
def __call__(self):
- server = self.cache_server
- if server is not None:
- client = get_cache_handler(server, ICacheHandler)
- if client is None:
- return ()
- notifications = client.get(CACHE_QUEUE_KEY)
- if notifications is None:
- return ()
- else:
- return [n for n in reversed(list(filter(lambda x: filtered(x, self.request),
- pickle.loads(notifications))))]
- else:
- return ()
+ return get_user_notifications(self.cache_server, self.request)