src/pyams_notify/views/notification.py
changeset 0 f53281280c23
child 10 7dea2cd0fa8a
equal deleted inserted replaced
-1:000000000000 0:f53281280c23
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 import pickle
       
    18 try:
       
    19     import pylibmc
       
    20 except ImportError:
       
    21     pylibmc = None
       
    22 
       
    23 # import interfaces
       
    24 from pyams_notify.interfaces import MEMCACHED_QUEUE_KEY
       
    25 from pyams_skin.layer import IPyAMSLayer
       
    26 
       
    27 # import packages
       
    28 from pyramid.view import view_config
       
    29 
       
    30 
       
    31 def filtered(notification, request):
       
    32     """Filter notification against current request"""
       
    33     return request.principal.id in notification['target'].get('principals', ())
       
    34 
       
    35 
       
    36 @view_config(name='get-user-notifications.json', request_type=IPyAMSLayer, renderer='json', xhr=True)
       
    37 class UserNotificationsView(object):
       
    38     """User notifications view"""
       
    39 
       
    40     def __init__(self, request):
       
    41         self.request = request
       
    42         self.context = request.context
       
    43 
       
    44     @property
       
    45     def memcached_server(self):
       
    46         return self.request.registry.settings.get('pyams_notify_ws.memcached_server')
       
    47 
       
    48     def __call__(self):
       
    49         if pylibmc is None:
       
    50             return ()
       
    51         server = self.memcached_server
       
    52         if server is not None:
       
    53             client = pylibmc.Client([server])
       
    54             notifications = client.get(MEMCACHED_QUEUE_KEY)
       
    55             if notifications is None:
       
    56                 return ()
       
    57             else:
       
    58                 return [n for n in reversed(list(filter(lambda x: filtered(x, self.request),
       
    59                                                         pickle.loads(notifications))))]
       
    60         else:
       
    61             return ()