commit 0da5e49a23adc0edc1bfa6f00d90651ac6f496e2
Author: Giorgos Korfiatis <gkorf@grnet.gr>
Date:   Wed Apr 3 12:44:40 2013 +0300

    Compute and show quotas by source
    
    Adjust astakos code to the new nested dict structure of quotas.

diff --git a/snf-astakos-app/astakos/im/models.py b/snf-astakos-app/astakos/im/models.py
index e9acb13..96af69d 100644
--- a/snf-astakos-app/astakos/im/models.py
+++ b/snf-astakos-app/astakos/im/models.py
@@ -84,6 +84,8 @@ from astakos.quotaholder.api import QH_PRACTICALLY_INFINITE
 from synnefo.lib.db.intdecimalfield import intDecimalField
 from synnefo.util.text import uenc, udec
 
+from astakos.im.quotas import get_users_quotas_and_limits, set_user_quota
+
 logger = logging.getLogger(__name__)
 
 DEFAULT_CONTENT_TYPE = None
@@ -774,13 +776,16 @@ class AstakosUser(User):
             raise ValueError("could not compute quotas")
 
 
+SYSTEM = 'system'
+
 def initial_quotas(users):
     initial = {}
     default_quotas = get_default_quota()
 
     for user in users:
         uuid = user.uuid
-        initial[uuid] = dict(default_quotas)
+        source_quota = {SYSTEM: dict(default_quotas)}
+        initial[uuid] = source_quota
 
     objs = AstakosUserQuota.objects.select_related()
     orig_quotas = objs.filter(user__in=users)
@@ -794,16 +799,30 @@ def initial_quotas(users):
     return initial
 
 
+def get_grant_source(grant):
+    return SYSTEM
+
+
 def users_quotas(users, initial=None):
     if initial is None:
         quotas = initial_quotas(users)
     else:
         quotas = copy.deepcopy(initial)
 
-    objs = ProjectMembership.objects.select_related('application', 'person')
-    memberships = objs.filter(person__in=users)
+    ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED
+    objs = ProjectMembership.objects.select_related('project', 'person')
+    memberships = objs.filter(person__in=users,
+                              state__in=ACTUALLY_ACCEPTED,
+                              project__state=Project.APPROVED)
+
+    project_ids = set(m.project_id for m in memberships)
+    objs = ProjectApplication.objects.select_related('project')
+    apps = objs.filter(project__in=project_ids)
+
+    project_dict = {}
+    for app in apps:
+        project_dict[app.project] = app
 
-    apps = set(m.application for m in memberships if m.application is not None)
     objs = ProjectResourceGrant.objects.select_related()
     grants = objs.filter(project_application__in=apps)
 
@@ -811,17 +830,20 @@ def users_quotas(users, initial=None):
         uuid = membership.person.uuid
         userquotas = quotas.get(uuid, {})
 
-        application = membership.application
-        if application is None:
-            continue
+        application = project_dict[membership.project]
 
         for grant in grants:
             if grant.project_application_id != application.id:
                 continue
+
+            source = get_grant_source(grant)
+            source_quotas = userquotas.get(source, {})
+
             resource = grant.resource.full_name()
-            prev = userquotas.get(resource, 0)
+            prev = source_quotas.get(resource, 0)
             new = prev + grant.member_capacity
-            userquotas[resource] = new
+            source_quotas[resource] = new
+            userquotas[source] = source_quotas
         quotas[uuid] = userquotas
 
     return quotas
@@ -2305,8 +2327,7 @@ def sync_users(users, sync=True):
         for user in users:
             info[user.uuid] = user.email
 
-        resources = get_resource_names()
-        qh_limits, qh_counters = qh_get_quotas(users, resources)
+        qh_quotas, qh_limits = get_users_quotas_and_limits(users)
         astakos_initial = initial_quotas(users)
         astakos_quotas = users_quotas(users)
 
@@ -2317,9 +2338,9 @@ def sync_users(users, sync=True):
                 diff_quotas[holder] = dict(local)
 
         if sync:
-            r = send_quotas(diff_quotas)
+            r = set_user_quota(diff_quotas)
 
-        return (qh_limits, qh_counters,
+        return (qh_limits, qh_quotas,
                 astakos_initial, diff_quotas, info)
 
     return _sync_users(users, sync)
