Revision 20c6de35
b/snf-astakos-app/astakos/quotaholder_app/callpoint.py | ||
---|---|---|
72 | 72 |
return quotas |
73 | 73 |
|
74 | 74 |
|
75 |
def _get_holdings_for_update(holding_keys): |
|
75 |
def _get_holdings_for_update(holding_keys, delete=False):
|
|
76 | 76 |
holders = set(holder for (holder, source, resource) in holding_keys) |
77 |
objs = Holding.objects |
|
78 |
hs = objs.filter(holder__in=holders).order_by('pk').select_for_update()
|
|
77 |
objs = Holding.objects.filter(holder__in=holders).order_by('pk')
|
|
78 |
hs = objs.select_for_update() |
|
79 | 79 |
|
80 |
keys = set(holding_keys) |
|
80 | 81 |
holdings = {} |
82 |
put_back = [] |
|
81 | 83 |
for h in hs: |
82 | 84 |
key = h.holder, h.source, h.resource |
83 |
holdings[key] = h |
|
85 |
if key in keys: |
|
86 |
holdings[key] = h |
|
87 |
else: |
|
88 |
put_back.append(h) |
|
84 | 89 |
|
90 |
if delete: |
|
91 |
objs.delete() |
|
92 |
Holding.objects.bulk_create(put_back) |
|
85 | 93 |
return holdings |
86 | 94 |
|
87 | 95 |
|
... | ... | |
96 | 104 |
|
97 | 105 |
def set_quota(quotas): |
98 | 106 |
holding_keys = [key for (key, limit) in quotas] |
99 |
holdings = _get_holdings_for_update(holding_keys) |
|
107 |
holdings = _get_holdings_for_update(holding_keys, delete=True)
|
|
100 | 108 |
|
109 |
new_holdings = {} |
|
101 | 110 |
for key, limit in quotas: |
111 |
holder, source, resource = key |
|
112 |
h = Holding(holder=holder, |
|
113 |
source=source, |
|
114 |
resource=resource, |
|
115 |
limit=limit) |
|
102 | 116 |
try: |
103 |
h = holdings[key] |
|
117 |
h_old = holdings[key] |
|
118 |
h.usage_min = h_old.usage_min |
|
119 |
h.usage_max = h_old.usage_max |
|
104 | 120 |
except KeyError: |
105 |
holder, source, resource = key |
|
106 |
h = Holding(holder=holder, |
|
107 |
source=source, |
|
108 |
resource=resource) |
|
109 |
h.limit = limit |
|
110 |
h.save() |
|
111 |
holdings[key] = h |
|
121 |
pass |
|
122 |
new_holdings[key] = h |
|
123 |
|
|
124 |
Holding.objects.bulk_create(new_holdings.values()) |
|
112 | 125 |
|
113 | 126 |
|
114 | 127 |
def add_resource_limit(holders=None, sources=None, resources=None, diff=0): |
b/snf-astakos-app/astakos/quotaholder_app/tests.py | ||
---|---|---|
34 | 34 |
from django.test import TestCase |
35 | 35 |
|
36 | 36 |
from snf_django.utils.testing import assertGreater, assertIn, assertRaises |
37 |
from astakos.quotaholder_app import models |
|
37 | 38 |
import astakos.quotaholder_app.callpoint as qh |
38 | 39 |
from astakos.quotaholder_app.exception import ( |
39 | 40 |
InvalidDataError, |
... | ... | |
275 | 276 |
serial = self.issue_commission([]) |
276 | 277 |
r = qh.resolve_pending_commission(self.client, serial) |
277 | 278 |
self.assertEqual(r, True) |
279 |
|
|
280 |
def test_030_set(self): |
|
281 |
holder = 'h0' |
|
282 |
source = 'system' |
|
283 |
resource1 = 'r1' |
|
284 |
resource2 = 'r2' |
|
285 |
limit1 = 10 |
|
286 |
limit2 = 20 |
|
287 |
|
|
288 |
models.Holding.objects.create( |
|
289 |
holder=holder, source=source, resource=resource1, |
|
290 |
usage_min=1, usage_max=1, limit=2) |
|
291 |
models.Holding.objects.create( |
|
292 |
holder=holder, source=source, resource=resource2, |
|
293 |
usage_min=2, usage_max=2, limit=22) |
|
294 |
|
|
295 |
qh.set_quota([((holder, source, resource1), limit1), |
|
296 |
((holder, source, resource1), limit2)]) |
|
297 |
|
|
298 |
r = qh.get_quota(holders=[holder]) |
|
299 |
self.assertEqual(r, {(holder, source, resource1): (limit2, 1, 1), |
|
300 |
(holder, source, resource2): (22, 2, 2)}) |
Also available in: Unified diff