root / snf-astakos-app / astakos / im / notification_xctx.py @ 469d0997
History | View | Annotate | Download (3.3 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 | 8a8578c5 | Giorgos Korfiatis | from synnefo.lib.db.xctx import TransactionContext |
35 | 8a8578c5 | Giorgos Korfiatis | from astakos.im.retry_xctx import RetryTransactionHandler |
36 | 8cf9b2dd | Giorgos Korfiatis | from astakos.im.notifications import Notification |
37 | 8cf9b2dd | Giorgos Korfiatis | |
38 | 8cf9b2dd | Giorgos Korfiatis | # USAGE
|
39 | 8cf9b2dd | Giorgos Korfiatis | # =====
|
40 | 8cf9b2dd | Giorgos Korfiatis | # @notification_transaction_context(notify=False)
|
41 | 8cf9b2dd | Giorgos Korfiatis | # def a_view(args, ctx=None):
|
42 | 8cf9b2dd | Giorgos Korfiatis | # ...
|
43 | 8cf9b2dd | Giorgos Korfiatis | # if ctx:
|
44 | 8cf9b2dd | Giorgos Korfiatis | # ctx.mark_rollback()
|
45 | 8cf9b2dd | Giorgos Korfiatis | # ...
|
46 | 8cf9b2dd | Giorgos Korfiatis | # return http response
|
47 | 8cf9b2dd | Giorgos Korfiatis | #
|
48 | e4c26c85 | Giorgos Korfiatis | # OR (more cleanly)
|
49 | 8cf9b2dd | Giorgos Korfiatis | #
|
50 | 8cf9b2dd | Giorgos Korfiatis | # def a_view(args):
|
51 | 8cf9b2dd | Giorgos Korfiatis | # with notification_transaction_context(notify=False) as ctx:
|
52 | 8cf9b2dd | Giorgos Korfiatis | # ...
|
53 | 8cf9b2dd | Giorgos Korfiatis | # ctx.mark_rollback()
|
54 | 8cf9b2dd | Giorgos Korfiatis | # ...
|
55 | 8cf9b2dd | Giorgos Korfiatis | # return http response
|
56 | 8cf9b2dd | Giorgos Korfiatis | |
57 | 8cf9b2dd | Giorgos Korfiatis | def notification_transaction_context(**kwargs): |
58 | 8a8578c5 | Giorgos Korfiatis | return RetryTransactionHandler(ctx=NotificationTransactionContext, **kwargs)
|
59 | 8cf9b2dd | Giorgos Korfiatis | |
60 | e4c26c85 | Giorgos Korfiatis | |
61 | 8cf9b2dd | Giorgos Korfiatis | class NotificationTransactionContext(TransactionContext): |
62 | 8cf9b2dd | Giorgos Korfiatis | def __init__(self, notify=True, **kwargs): |
63 | 8cf9b2dd | Giorgos Korfiatis | self._notifications = []
|
64 | 8cf9b2dd | Giorgos Korfiatis | self._messages = []
|
65 | 8cf9b2dd | Giorgos Korfiatis | self._notify = notify
|
66 | 8cf9b2dd | Giorgos Korfiatis | TransactionContext.__init__(self, **kwargs)
|
67 | 8cf9b2dd | Giorgos Korfiatis | |
68 | 8cf9b2dd | Giorgos Korfiatis | def register(self, o): |
69 | 8cf9b2dd | Giorgos Korfiatis | if isinstance(o, dict): |
70 | 8cf9b2dd | Giorgos Korfiatis | msg = o.get('msg', None) |
71 | e4c26c85 | Giorgos Korfiatis | if msg is not None: |
72 | 8cf9b2dd | Giorgos Korfiatis | if isinstance(msg, basestring): |
73 | 8cf9b2dd | Giorgos Korfiatis | self.queue_message(msg)
|
74 | 8cf9b2dd | Giorgos Korfiatis | |
75 | 8cf9b2dd | Giorgos Korfiatis | notif = o.get('notif', None) |
76 | e4c26c85 | Giorgos Korfiatis | if notif is not None: |
77 | e4c26c85 | Giorgos Korfiatis | if isinstance(notif, Notification): |
78 | e4c26c85 | Giorgos Korfiatis | self.queue_notification(notif)
|
79 | e4c26c85 | Giorgos Korfiatis | |
80 | e4c26c85 | Giorgos Korfiatis | if o.has_key('value'): |
81 | e4c26c85 | Giorgos Korfiatis | return o['value'] |
82 | 8cf9b2dd | Giorgos Korfiatis | return o
|
83 | 8cf9b2dd | Giorgos Korfiatis | |
84 | e4c26c85 | Giorgos Korfiatis | def queue_message(self, m): |
85 | 8cf9b2dd | Giorgos Korfiatis | self._messages.append(m)
|
86 | 8cf9b2dd | Giorgos Korfiatis | |
87 | 8cf9b2dd | Giorgos Korfiatis | def queue_notification(self, n): |
88 | 8cf9b2dd | Giorgos Korfiatis | self._notifications.append(n)
|
89 | 8cf9b2dd | Giorgos Korfiatis | |
90 | 8cf9b2dd | Giorgos Korfiatis | def _send_notifications(self): |
91 | 8cf9b2dd | Giorgos Korfiatis | if self._notifications is None: |
92 | 8cf9b2dd | Giorgos Korfiatis | return
|
93 | 8cf9b2dd | Giorgos Korfiatis | # send mail
|
94 | 8cf9b2dd | Giorgos Korfiatis | |
95 | 8cf9b2dd | Giorgos Korfiatis | def postprocess(self): |
96 | 8cf9b2dd | Giorgos Korfiatis | if self._notify: |
97 | 8cf9b2dd | Giorgos Korfiatis | self._send_notifications()
|
98 | 8cf9b2dd | Giorgos Korfiatis | TransactionContext.postprocess(self) |