Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / project_xctx.py @ 5cdd7df3

History | View | Annotate | Download (2.6 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 astakos.im.retry_xctx import RetryTransactionHandler
35 8cf9b2dd Giorgos Korfiatis
from astakos.im.notification_xctx import NotificationTransactionContext
36 8cf9b2dd Giorgos Korfiatis
from astakos.im.models import sync_projects
37 8a8578c5 Giorgos Korfiatis
from astakos.im.project_error import project_error_view
38 8cf9b2dd Giorgos Korfiatis
39 8cf9b2dd Giorgos Korfiatis
# USAGE
40 8cf9b2dd Giorgos Korfiatis
# =====
41 8cf9b2dd Giorgos Korfiatis
# @project_transaction_context(sync=True)
42 8cf9b2dd Giorgos Korfiatis
# def a_view(args, ctx=None):
43 8cf9b2dd Giorgos Korfiatis
#     ...
44 8cf9b2dd Giorgos Korfiatis
#     if ctx:
45 8cf9b2dd Giorgos Korfiatis
#         ctx.mark_rollback()
46 8cf9b2dd Giorgos Korfiatis
#     ...
47 8cf9b2dd Giorgos Korfiatis
#     return http response
48 8cf9b2dd Giorgos Korfiatis
#
49 729d1abe Giorgos Korfiatis
# OR (more cleanly)
50 8cf9b2dd Giorgos Korfiatis
#
51 8cf9b2dd Giorgos Korfiatis
# def a_view(args):
52 8cf9b2dd Giorgos Korfiatis
#     with project_transaction_context(sync=True) as ctx:
53 8cf9b2dd Giorgos Korfiatis
#         ...
54 8cf9b2dd Giorgos Korfiatis
#         ctx.mark_rollback()
55 8cf9b2dd Giorgos Korfiatis
#         ...
56 8cf9b2dd Giorgos Korfiatis
#         return http response
57 8cf9b2dd Giorgos Korfiatis
58 8cf9b2dd Giorgos Korfiatis
def project_transaction_context(**kwargs):
59 8a8578c5 Giorgos Korfiatis
    return RetryTransactionHandler(ctx=ProjectTransactionContext,
60 8a8578c5 Giorgos Korfiatis
                                   on_fail=project_error_view,
61 8a8578c5 Giorgos Korfiatis
                                   **kwargs)
62 8cf9b2dd Giorgos Korfiatis
63 8cf9b2dd Giorgos Korfiatis
class ProjectTransactionContext(NotificationTransactionContext):
64 8cf9b2dd Giorgos Korfiatis
    def __init__(self, sync=False, **kwargs):
65 8cf9b2dd Giorgos Korfiatis
        self._sync = sync
66 8cf9b2dd Giorgos Korfiatis
        NotificationTransactionContext.__init__(self, **kwargs)
67 8cf9b2dd Giorgos Korfiatis
68 8cf9b2dd Giorgos Korfiatis
    def postprocess(self):
69 8cf9b2dd Giorgos Korfiatis
        if self._sync:
70 8cf9b2dd Giorgos Korfiatis
            sync_projects()
71 8cf9b2dd Giorgos Korfiatis
        NotificationTransactionContext.postprocess(self)