Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / project_xctx.py @ 04e05445

History | View | Annotate | Download (2.5 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 TransactionHandler
35 8cf9b2dd Giorgos Korfiatis
from astakos.im.notification_xctx import NotificationTransactionContext
36 8cf9b2dd Giorgos Korfiatis
from astakos.im.models import sync_projects
37 8cf9b2dd Giorgos Korfiatis
38 8cf9b2dd Giorgos Korfiatis
# USAGE
39 8cf9b2dd Giorgos Korfiatis
# =====
40 8cf9b2dd Giorgos Korfiatis
# @project_transaction_context(sync=True)
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 729d1abe Giorgos Korfiatis
# OR (more cleanly)
49 8cf9b2dd Giorgos Korfiatis
#
50 8cf9b2dd Giorgos Korfiatis
# def a_view(args):
51 8cf9b2dd Giorgos Korfiatis
#     with project_transaction_context(sync=True) 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 project_transaction_context(**kwargs):
58 8cf9b2dd Giorgos Korfiatis
    return TransactionHandler(ctx=ProjectTransactionContext, **kwargs)
59 8cf9b2dd Giorgos Korfiatis
60 8cf9b2dd Giorgos Korfiatis
class ProjectTransactionContext(NotificationTransactionContext):
61 8cf9b2dd Giorgos Korfiatis
    def __init__(self, sync=False, **kwargs):
62 8cf9b2dd Giorgos Korfiatis
        self._sync = sync
63 8cf9b2dd Giorgos Korfiatis
        NotificationTransactionContext.__init__(self, **kwargs)
64 8cf9b2dd Giorgos Korfiatis
65 8cf9b2dd Giorgos Korfiatis
    def postprocess(self):
66 8cf9b2dd Giorgos Korfiatis
        if self._sync:
67 8cf9b2dd Giorgos Korfiatis
            sync_projects()
68 8cf9b2dd Giorgos Korfiatis
        NotificationTransactionContext.postprocess(self)