Revision c8cb5d53 snf-astakos-app/astakos/im/quotas.py
b/snf-astakos-app/astakos/im/quotas.py | ||
---|---|---|
37 | 37 |
import astakos.quotaholder_app.callpoint as qh |
38 | 38 |
from astakos.quotaholder_app.exception import NoCapacityError |
39 | 39 |
from django.db.models import Q |
40 |
from synnefo.util.keypath import set_path
|
|
40 |
from collections import defaultdict
|
|
41 | 41 |
|
42 | 42 |
|
43 |
QuotaDict = lambda: defaultdict(lambda: defaultdict(dict)) |
|
44 |
|
|
43 | 45 |
PROJECT_TAG = "project:" |
44 | 46 |
USER_TAG = "user:" |
45 | 47 |
|
... | ... | |
104 | 106 |
|
105 | 107 |
|
106 | 108 |
def mk_quota_dict(users_counters, project_counters): |
107 |
quota = {}
|
|
109 |
quota = QuotaDict()
|
|
108 | 110 |
for (holder, source, resource), u_value in users_counters.iteritems(): |
109 | 111 |
p_value = project_counters[(source, None, resource)] |
110 | 112 |
values_dict = from_holding(u_value) |
111 | 113 |
values_dict.update(from_holding(p_value, is_project=True)) |
112 |
set_path(quota, [holder, source, resource], values_dict, |
|
113 |
createpath=True) |
|
114 |
quota[holder][source][resource] = values_dict |
|
114 | 115 |
return quota |
115 | 116 |
|
116 | 117 |
|
... | ... | |
144 | 145 |
|
145 | 146 |
|
146 | 147 |
def mk_limits_dict(counters): |
147 |
quota = {} |
|
148 |
for key, (limit, _, _) in counters.iteritems(): |
|
149 |
path = list(key) |
|
150 |
set_path(quota, path, limit, createpath=True) |
|
148 |
quota = QuotaDict() |
|
149 |
for (holder, source, resource), (limit, _, _) in counters.iteritems(): |
|
150 |
quota[holder][source][resource] = limit |
|
151 | 151 |
return quota |
152 | 152 |
|
153 | 153 |
|
154 | 154 |
def mk_project_quota_dict(project_counters): |
155 |
quota = {}
|
|
155 |
quota = QuotaDict()
|
|
156 | 156 |
for (holder, _, resource), p_value in project_counters.iteritems(): |
157 | 157 |
values_dict = from_holding(p_value, is_project=True) |
158 |
set_path(quota, [holder, resource], values_dict, |
|
159 |
createpath=True) |
|
158 |
quota[holder][resource] = values_dict |
|
160 | 159 |
return quota |
161 | 160 |
|
162 | 161 |
|
... | ... | |
263 | 262 |
"person", "project") |
264 | 263 |
memberships_d = _partition_by(lambda m: m.project_id, memberships) |
265 | 264 |
|
266 |
user_quota = {}
|
|
267 |
project_quota = {}
|
|
265 |
user_quota = QuotaDict()
|
|
266 |
project_quota = QuotaDict()
|
|
268 | 267 |
|
269 | 268 |
for project in projects: |
270 | 269 |
pr_ref = get_project_ref(project) |
... | ... | |
276 | 275 |
project_memberships = memberships_d.get(project.id, []) |
277 | 276 |
for grant in project_grants: |
278 | 277 |
resource = grant.resource.name |
279 |
path = [pr_ref, None, resource] |
|
280 | 278 |
val = grant.project_capacity if state == Project.NORMAL else 0 |
281 |
set_path(project_quota, path, val, createpath=True)
|
|
279 |
project_quota[pr_ref][None][resource] = val
|
|
282 | 280 |
for membership in project_memberships: |
283 | 281 |
u_ref = get_user_ref(membership.person) |
284 |
path = [u_ref, pr_ref, resource] |
|
285 | 282 |
val = grant.member_capacity if membership.is_active() else 0 |
286 |
set_path(user_quota, path, val, createpath=True)
|
|
283 |
user_quota[u_ref][pr_ref][resource] = val
|
|
287 | 284 |
|
288 | 285 |
return project_quota, user_quota |
289 | 286 |
|
... | ... | |
309 | 306 |
u_ref = get_user_ref(membership.person) |
310 | 307 |
objs = ProjectResourceQuota.objects.select_related() |
311 | 308 |
grants = objs.filter(project=project) |
312 |
user_quota = {}
|
|
309 |
user_quota = QuotaDict()
|
|
313 | 310 |
is_active = membership.is_active() |
314 | 311 |
for grant in grants: |
315 | 312 |
resource = grant.resource.name |
316 |
path = [u_ref, pr_ref, resource] |
|
317 | 313 |
value = grant.member_capacity if is_active else 0 |
318 |
set_path(user_quota, path, value, createpath=True)
|
|
314 |
user_quota[u_ref][pr_ref][resource] = value
|
|
319 | 315 |
return user_quota |
320 | 316 |
|
321 | 317 |
|
Also available in: Unified diff