Commit 5305ab1a authored by Georgios D. Tsoukalas's avatar Georgios D. Tsoukalas
Browse files

add list_holdings quotaholder api call

list_holdings() to be used to get all holdings for multiple resources
without having to call list_resources/get_holding for each entity/holding.
parent 6d807c31
......@@ -195,6 +195,18 @@ class QuotaholderAPI(Specificator):
resources = ListOf(Resource)
return resources
def list_holdings (
self,
context = Context,
list_holdings = ListOf(Entity, Key)
):
rejected = ListOf(Entity)
holdings_list = ListOf(ListOf(Entity, Resource,
Imported, Exported,
Returned, Released))
return Tuple(holdings_list, rejected)
def get_quota (
self,
context = Context,
......
......@@ -393,6 +393,28 @@ class QuotaholderDjangoDBCallpoint(Callpoint):
resources = [h.resource for h in holdings]
return resources
def list_holdings(self, context={}, list_holdings=()):
rejected = []
reject = rejected.append
holdings_list = []
append = holdings_list.append
for entity, key in list_holdings:
try:
e = Entity.objects.get(entity=entity)
if e.key != key:
raise Entity.DoesNotExist("wrong key")
except Entity.DoesNotExist:
reject(entity)
continue
holdings = e.holding_set.filter(entity=entity)
append([[entity, h.resource,
h.imported, h.exported, h.returned, h.released]
for h in holdings])
return holdings_list, rejected
def get_quota(self, context={}, get_quota=()):
quotas = []
append = quotas.append
......
......@@ -232,6 +232,40 @@ class QHAPITest(QHTestCase):
name='something',
provisions=[(e1, resource, 1)])
def test_010_list_holdings(self):
e0, k0 = ('list_holdings_one', '1')
e1, k1 = ('list_holdings_two', '1')
resource = 'list_holdings_resource'
sys = 'system'
r = self.qh.create_entity(create_entity=[(e0, sys, k0, ''),
(e1, sys, k1, '')])
if r:
raise AssertionError("cannot create entities")
self.qh.set_quota(set_quota=[(sys, resource, '', 10, 0, None, None, 0),
(e0, resource, k0, 0, 10, None, None, 0),
(e1, resource, k1, 0, 10, None, None, 0)])
s0 = self.qh.issue_commission(clientkey=self.client, target=e0, key=k0,
name='a commission',
provisions=[('system', resource, 3)])
s1 = self.qh.issue_commission(clientkey=self.client, target=e1, key=k1,
name='a commission',
provisions=[('system', resource, 4)])
self.qh.accept_commission(clientkey=self.client, serials=[s0, s1])
holdings_list, rejected = self.qh.list_holdings(list_holdings=[
(e0, k0),
(e1, k1),
(e0+e1, k0+k1)])
self.assertEqual(rejected, [e0+e1])
self.assertEqual(holdings_list, [[(e0, resource, 3, 0, 0, 0)],
[(e1, resource, 4, 0, 0, 0)]])
def test_010_pending_commissions(self):
r = self.qh.get_pending_commissions(clientkey=self.client)
......
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