Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / quotas.py @ dc9da5b9

History | View | Annotate | Download (3.2 kB)

1
# Copyright 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint
35

    
36
qh = QuotaholderDjangoDBCallpoint()
37

    
38

    
39
def from_holding(holding):
40
    limit, imported_min, imported_max = holding
41
    body = {'limit':       limit,
42
            'used':        imported_min,
43
            'available':   max(0, limit-imported_max),
44
            }
45
    return body
46

    
47

    
48
def limits_only(holding):
49
    limit, imported_min, imported_max = holding
50
    return limit
51

    
52

    
53
def transform_data(holdings, func=None):
54
    if func is None:
55
        func = from_holding
56

    
57
    quota = {}
58
    for (holder, source, resource), value in holdings.iteritems():
59
        holder_quota = quota.get(holder, {})
60
        source_quota = holder_quota.get(source, {})
61
        body = func(value)
62
        source_quota[resource] = body
63
        holder_quota[source] = source_quota
64
        quota[holder] = holder_quota
65
    return quota
66

    
67

    
68
def get_counters(users,  resources=None, sources=None):
69
    uuids = [user.uuid for user in users]
70

    
71
    counters = qh.get_holder_quota(holders=uuids,
72
                                   resources=resources,
73
                                   sources=sources)
74
    return counters
75

    
76

    
77
def get_users_quotas(users, resources=None, sources=None):
78
    counters = get_counters(users, resources, sources)
79
    quotas = transform_data(counters)
80
    return quotas
81

    
82

    
83
def get_users_quotas_and_limits(users, resources=None, sources=None):
84
    counters = get_counters(users, resources, sources)
85
    quotas = transform_data(counters)
86
    limits = transform_data(counters, limits_only)
87
    return quotas, limits
88

    
89

    
90
def get_user_quotas(user, resources=None, sources=None):
91
    quotas = get_users_quotas([user], resources, sources)
92
    return quotas[user.uuid]
93

    
94

    
95
def set_user_quota(quotas):
96
    qh.set_holder_quota(quotas)