Commit d81a5b38 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Add user parameter in service_quotas API call

Using this parameter, the service can query the quotas of a specific
user.
parent 04ed1128
......@@ -90,7 +90,7 @@ Status Description
}
Get Quotas per Service
..........
......................
**GET** /astakos/api/service_quotas
......@@ -100,6 +100,8 @@ Request Header Name Value
X-Auth-Token Service authentication token
==================== ============================
Optional GET parameter: ?user=<uuid>
**Normal Response Code**: 200
**Error Response Codes**:
......
......@@ -63,7 +63,9 @@ def quotas(request, user=None):
@api.api_method(http_method='GET', token_required=True, user_required=False)
@service_from_token
def service_quotas(request):
result = get_service_quotas(request.service_instance)
user = request.GET.get('user')
users = [user] if user is not None else None
result = get_service_quotas(request.service_instance, users=users)
return json_response(result)
......
......@@ -68,12 +68,12 @@ def transform_data(holdings, func=None):
return quota
def get_counters(users, resources=None, sources=None):
def get_counters(users, resources=None, sources=None):
uuids = [user.uuid for user in users]
counters = qh.get_holder_quota(holders=uuids,
resources=resources,
sources=sources)
counters = qh.get_quota(holders=uuids,
resources=resources,
sources=sources)
return counters
......@@ -95,10 +95,10 @@ def get_user_quotas(user, resources=None, sources=None):
return quotas[user.uuid]
def get_service_quotas(service):
def get_service_quotas(service, users=None):
resources = Resource.objects.filter(service=service.name)
resource_names = [r.name for r in resources]
counters = qh.get_resource_quota(resource_names)
counters = qh.get_quota(holders=users, resources=resource_names)
return transform_data(counters)
......
......@@ -52,8 +52,11 @@ from .models import (Holding,
class QuotaholderDjangoDBCallpoint(object):
def get_holder_quota(self, holders=None, sources=None, resources=None):
holdings = Holding.objects.filter(holder__in=holders)
def get_quota(self, holders=None, sources=None, resources=None):
holdings = Holding.objects.all()
if holders is not None:
holdings = holdings.filter(holder__in=holders)
if sources is not None:
holdings = holdings.filter(source__in=sources)
......@@ -69,20 +72,6 @@ class QuotaholderDjangoDBCallpoint(object):
return quotas
def get_resource_quota(self, resources, sources=None):
holdings = Holding.objects.filter(resource__in=resources)
if sources is not None:
holdings = holdings.filter(source__in=sources)
quotas = {}
for holding in holdings:
key = (holding.holder, holding.source, holding.resource)
value = (holding.limit, holding.imported_min, holding.imported_max)
quotas[key] = value
return quotas
def _get_holdings_for_update(self, holding_keys):
holding_keys = sorted(holding_keys)
holdings = {}
......
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