Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / notification_xctx.py @ 18945296

History | View | Annotate | Download (3.2 kB)

1 8cf9b2dd Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 8cf9b2dd Giorgos Korfiatis
#
3 8cf9b2dd Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 8cf9b2dd Giorgos Korfiatis
# without modification, are permitted provided that the following
5 8cf9b2dd Giorgos Korfiatis
# conditions are met:
6 8cf9b2dd Giorgos Korfiatis
#
7 8cf9b2dd Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 8cf9b2dd Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 8cf9b2dd Giorgos Korfiatis
#      disclaimer.
10 8cf9b2dd Giorgos Korfiatis
#
11 8cf9b2dd Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 8cf9b2dd Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 8cf9b2dd Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 8cf9b2dd Giorgos Korfiatis
#      provided with the distribution.
15 8cf9b2dd Giorgos Korfiatis
#
16 8cf9b2dd Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 8cf9b2dd Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 8cf9b2dd Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 8cf9b2dd Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 8cf9b2dd Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 8cf9b2dd Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 8cf9b2dd Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 8cf9b2dd Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 8cf9b2dd Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 8cf9b2dd Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 8cf9b2dd Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 8cf9b2dd Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 8cf9b2dd Giorgos Korfiatis
#
29 8cf9b2dd Giorgos Korfiatis
# The views and conclusions contained in the software and
30 8cf9b2dd Giorgos Korfiatis
# documentation are those of the authors and should not be
31 8cf9b2dd Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 8cf9b2dd Giorgos Korfiatis
# or implied, of GRNET S.A.
33 8cf9b2dd Giorgos Korfiatis
34 8cf9b2dd Giorgos Korfiatis
from synnefo.lib.db.xctx import TransactionContext, TransactionHandler
35 8cf9b2dd Giorgos Korfiatis
from astakos.im.notifications import Notification
36 8cf9b2dd Giorgos Korfiatis
37 8cf9b2dd Giorgos Korfiatis
# USAGE
38 8cf9b2dd Giorgos Korfiatis
# =====
39 8cf9b2dd Giorgos Korfiatis
# @notification_transaction_context(notify=False)
40 8cf9b2dd Giorgos Korfiatis
# def a_view(args, ctx=None):
41 8cf9b2dd Giorgos Korfiatis
#     ...
42 8cf9b2dd Giorgos Korfiatis
#     if ctx:
43 8cf9b2dd Giorgos Korfiatis
#         ctx.mark_rollback()
44 8cf9b2dd Giorgos Korfiatis
#     ...
45 8cf9b2dd Giorgos Korfiatis
#     return http response
46 8cf9b2dd Giorgos Korfiatis
#
47 e4c26c85 Giorgos Korfiatis
# OR (more cleanly)
48 8cf9b2dd Giorgos Korfiatis
#
49 8cf9b2dd Giorgos Korfiatis
# def a_view(args):
50 8cf9b2dd Giorgos Korfiatis
#     with notification_transaction_context(notify=False) as ctx:
51 8cf9b2dd Giorgos Korfiatis
#         ...
52 8cf9b2dd Giorgos Korfiatis
#         ctx.mark_rollback()
53 8cf9b2dd Giorgos Korfiatis
#         ...
54 8cf9b2dd Giorgos Korfiatis
#         return http response
55 8cf9b2dd Giorgos Korfiatis
56 8cf9b2dd Giorgos Korfiatis
def notification_transaction_context(**kwargs):
57 8cf9b2dd Giorgos Korfiatis
    return TransactionHandler(ctx=NotificationTransactionContext, **kwargs)
58 8cf9b2dd Giorgos Korfiatis
59 e4c26c85 Giorgos Korfiatis
60 8cf9b2dd Giorgos Korfiatis
class NotificationTransactionContext(TransactionContext):
61 8cf9b2dd Giorgos Korfiatis
    def __init__(self, notify=True, **kwargs):
62 8cf9b2dd Giorgos Korfiatis
        self._notifications = []
63 8cf9b2dd Giorgos Korfiatis
        self._messages      = []
64 8cf9b2dd Giorgos Korfiatis
        self._notify        = notify
65 8cf9b2dd Giorgos Korfiatis
        TransactionContext.__init__(self, **kwargs)
66 8cf9b2dd Giorgos Korfiatis
67 8cf9b2dd Giorgos Korfiatis
    def register(self, o):
68 8cf9b2dd Giorgos Korfiatis
        if isinstance(o, dict):
69 8cf9b2dd Giorgos Korfiatis
            msg = o.get('msg', None)
70 e4c26c85 Giorgos Korfiatis
            if msg is not None:
71 8cf9b2dd Giorgos Korfiatis
                if isinstance(msg, basestring):
72 8cf9b2dd Giorgos Korfiatis
                    self.queue_message(msg)
73 8cf9b2dd Giorgos Korfiatis
74 8cf9b2dd Giorgos Korfiatis
            notif = o.get('notif', None)
75 e4c26c85 Giorgos Korfiatis
            if notif is not None:
76 e4c26c85 Giorgos Korfiatis
                if isinstance(notif, Notification):
77 e4c26c85 Giorgos Korfiatis
                    self.queue_notification(notif)
78 e4c26c85 Giorgos Korfiatis
79 e4c26c85 Giorgos Korfiatis
            if o.has_key('value'):
80 e4c26c85 Giorgos Korfiatis
                return o['value']
81 8cf9b2dd Giorgos Korfiatis
        return o
82 8cf9b2dd Giorgos Korfiatis
83 e4c26c85 Giorgos Korfiatis
    def queue_message(self, m):
84 8cf9b2dd Giorgos Korfiatis
        self._messages.append(m)
85 8cf9b2dd Giorgos Korfiatis
86 8cf9b2dd Giorgos Korfiatis
    def queue_notification(self, n):
87 8cf9b2dd Giorgos Korfiatis
        self._notifications.append(n)
88 8cf9b2dd Giorgos Korfiatis
89 8cf9b2dd Giorgos Korfiatis
    def _send_notifications(self):
90 8cf9b2dd Giorgos Korfiatis
        if self._notifications is None:
91 8cf9b2dd Giorgos Korfiatis
            return
92 8cf9b2dd Giorgos Korfiatis
        # send mail
93 8cf9b2dd Giorgos Korfiatis
94 8cf9b2dd Giorgos Korfiatis
    def postprocess(self):
95 8cf9b2dd Giorgos Korfiatis
        if self._notify:
96 8cf9b2dd Giorgos Korfiatis
            self._send_notifications()
97 8cf9b2dd Giorgos Korfiatis
        TransactionContext.postprocess(self)