Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / test / views.py @ 53f980c2

History | View | Annotate | Download (3 kB)

1 2f0cd9e1 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 2f0cd9e1 Giorgos Korfiatis
#
3 2f0cd9e1 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 2f0cd9e1 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 2f0cd9e1 Giorgos Korfiatis
# conditions are met:
6 2f0cd9e1 Giorgos Korfiatis
#
7 2f0cd9e1 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 2f0cd9e1 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 2f0cd9e1 Giorgos Korfiatis
#      disclaimer.
10 2f0cd9e1 Giorgos Korfiatis
#
11 2f0cd9e1 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 2f0cd9e1 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 2f0cd9e1 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 2f0cd9e1 Giorgos Korfiatis
#      provided with the distribution.
15 2f0cd9e1 Giorgos Korfiatis
#
16 2f0cd9e1 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 2f0cd9e1 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 2f0cd9e1 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 2f0cd9e1 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 2f0cd9e1 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 2f0cd9e1 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 2f0cd9e1 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 2f0cd9e1 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 2f0cd9e1 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 2f0cd9e1 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 2f0cd9e1 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 2f0cd9e1 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 2f0cd9e1 Giorgos Korfiatis
#
29 2f0cd9e1 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 2f0cd9e1 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 2f0cd9e1 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 2f0cd9e1 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 2f0cd9e1 Giorgos Korfiatis
34 2f0cd9e1 Giorgos Korfiatis
from datetime import datetime, timedelta
35 2f0cd9e1 Giorgos Korfiatis
36 2f0cd9e1 Giorgos Korfiatis
from astakos.im.models import AstakosUser, PendingMembershipError
37 2f0cd9e1 Giorgos Korfiatis
from astakos.im.functions import (join_project, leave_project,
38 2f0cd9e1 Giorgos Korfiatis
                                  submit_application, approve_application)
39 0932ac43 Giorgos Korfiatis
from astakos.im.project_xctx import cmd_project_transaction_context
40 2f0cd9e1 Giorgos Korfiatis
from astakos.im.retry_xctx import RetryException
41 2f0cd9e1 Giorgos Korfiatis
42 0932ac43 Giorgos Korfiatis
@cmd_project_transaction_context(sync=True)
43 2f0cd9e1 Giorgos Korfiatis
def join(proj_id, user_id, ctx=None):
44 2f0cd9e1 Giorgos Korfiatis
    join_project(proj_id, user_id)
45 2f0cd9e1 Giorgos Korfiatis
46 0932ac43 Giorgos Korfiatis
@cmd_project_transaction_context(sync=True)
47 2f0cd9e1 Giorgos Korfiatis
def leave(proj_id, user_id, ctx=None):
48 2f0cd9e1 Giorgos Korfiatis
    try:
49 2f0cd9e1 Giorgos Korfiatis
        leave_project(proj_id, user_id)
50 2f0cd9e1 Giorgos Korfiatis
    except PendingMembershipError as e:
51 2f0cd9e1 Giorgos Korfiatis
        print e
52 2f0cd9e1 Giorgos Korfiatis
        raise RetryException()
53 2f0cd9e1 Giorgos Korfiatis
54 327eb666 Giorgos Korfiatis
@cmd_project_transaction_context()
55 2f0cd9e1 Giorgos Korfiatis
def submit(name, user_id, prec, ctx=None):
56 2f0cd9e1 Giorgos Korfiatis
    try:
57 2f0cd9e1 Giorgos Korfiatis
        owner = AstakosUser.objects.get(id=user_id)
58 2f0cd9e1 Giorgos Korfiatis
    except AstakosUser.DoesNotExist:
59 2f0cd9e1 Giorgos Korfiatis
        raise AttributeError('user does not exist')
60 2f0cd9e1 Giorgos Korfiatis
61 2f0cd9e1 Giorgos Korfiatis
    resource_policies = [{'service': 'cyclades',
62 2f0cd9e1 Giorgos Korfiatis
                          'resource': 'network.private',
63 2f0cd9e1 Giorgos Korfiatis
                          'uplimit': 5}]
64 2f0cd9e1 Giorgos Korfiatis
    data = {'owner': owner,
65 2f0cd9e1 Giorgos Korfiatis
            'name': name,
66 2f0cd9e1 Giorgos Korfiatis
            'precursor_application': prec,
67 2f0cd9e1 Giorgos Korfiatis
            'end_date': datetime.now() + timedelta(days=1),
68 2f0cd9e1 Giorgos Korfiatis
            'member_join_policy': 1,
69 2f0cd9e1 Giorgos Korfiatis
            'member_leave_policy': 1,
70 2f0cd9e1 Giorgos Korfiatis
            'resource_policies': resource_policies,
71 2f0cd9e1 Giorgos Korfiatis
            }
72 2f0cd9e1 Giorgos Korfiatis
73 2f0cd9e1 Giorgos Korfiatis
    app = submit_application(data, request_user=owner)
74 2f0cd9e1 Giorgos Korfiatis
    return app.id
75 2f0cd9e1 Giorgos Korfiatis
76 0932ac43 Giorgos Korfiatis
@cmd_project_transaction_context(sync=True)
77 2f0cd9e1 Giorgos Korfiatis
def approve(app_id, ctx=None):
78 2f0cd9e1 Giorgos Korfiatis
    approve_application(app_id)