Commit a4dc4bb0 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

quotaholder: Merge similar provisions

Make sure that issue_commission handles a single provision per (holder,
source, resource) tuple by merging similar provisions.
parent 38855659
......@@ -38,7 +38,7 @@ from astakos.quotaholder_app.exception import (
NoCommissionError,
CorruptedError,
NoHoldingError,
DuplicateError)
)
from astakos.quotaholder_app.commission import (
Import, Release, Operations, finalize, undo)
......@@ -139,22 +139,23 @@ def set_quota(quotas, resource=None):
Holding.objects.bulk_create(new_holdings.values())
def _merge_same_keys(provisions):
prov_dict = _partition_by(lambda t: t[0], provisions, lambda t: t[1])
tuples = []
for key, values in prov_dict.iteritems():
tuples.append((key, sum(values)))
return tuples
def issue_commission(clientkey, provisions, name="", force=False):
operations = Operations()
provisions_to_create = []
provisions = _merge_same_keys(provisions)
keys = [key for (key, value) in provisions]
holdings = _get_holdings_for_update(keys)
try:
checked = []
for key, quantity in provisions:
if key in checked:
m = "Duplicate provision for %s" % str(key)
provision = _mkProvision(key, quantity)
raise DuplicateError(m,
provision=provision)
checked.append(key)
# Target
try:
th = holdings[key]
......@@ -221,12 +222,14 @@ def _get_commissions_for_update(clientkey, serials):
return commissions
def _partition_by(f, l):
def _partition_by(f, l, convert=None):
if convert is None:
convert = lambda x: x
d = {}
for x in l:
group = f(x)
group_l = d.get(group, [])
group_l.append(x)
group_l.append(convert(x))
d[group] = group_l
return d
......
......@@ -80,7 +80,3 @@ class NoQuantityError(OverLimitError):
class NoHoldingError(CommissionException):
pass
class DuplicateError(CommissionException):
pass
......@@ -41,7 +41,7 @@ from astakos.quotaholder_app.exception import (
NoQuantityError,
NoCapacityError,
NoHoldingError,
DuplicateError)
)
class QuotaholderTest(TestCase):
......@@ -139,17 +139,6 @@ class QuotaholderTest(TestCase):
self.assertEqual(provision['resource'], resource1)
self.assertEqual(provision['quantity'], 1)
with assertRaises(DuplicateError) as cm:
self.issue_commission([((holder, source, resource1), 1),
((holder, source, resource1), 2)])
e = cm.exception
provision = e.data['provision']
self.assertEqual(provision['holder'], holder)
self.assertEqual(provision['source'], source)
self.assertEqual(provision['resource'], resource1)
self.assertEqual(provision['quantity'], 2)
r = qh.get_quota(holders=[holder])
quotas = {(holder, source, resource1): (limit1, 0, limit1/2),
(holder, source, resource2): (limit2, 0, limit2),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment