Revision 46b6a8b4 snf-astakos-app/astakos/im/quotas.py

b/snf-astakos-app/astakos/im/quotas.py
151 151
    qh_sync_locked_users(users, resource=resource)
152 152

  
153 153

  
154
def _partition_by(f, l):
155
    d = {}
156
    for x in l:
157
        group = f(x)
158
        group_l = d.get(group, [])
159
        group_l.append(x)
160
        d[group] = group_l
161
    return d
162

  
163

  
154 164
def initial_quotas(users, flt=None):
155 165
    if flt is None:
156 166
        flt = Q()
157 167

  
158 168
    userids = [user.pk for user in users]
159
    objs = AstakosUserQuota.objects.select_related()
169
    objs = AstakosUserQuota.objects.select_related('resource')
160 170
    orig_quotas = objs.filter(user__pk__in=userids).filter(flt)
161
    initial = {}
162
    for user_quota in orig_quotas:
163
        uuid = user_quota.user.uuid
164
        user_init = initial.get(uuid, {})
165
        source_quota = user_init.get(SYSTEM, {})
166
        resource = user_quota.resource.full_name()
167
        source_quota[resource] = user_quota.capacity
168
        user_init[SYSTEM] = source_quota
169
        initial[uuid] = user_init
171
    orig_quotas = _partition_by(lambda q: q.user_id, orig_quotas)
170 172

  
173
    initial = {}
174
    for user in users:
175
        qs = {}
176
        for q in orig_quotas.get(user.pk, []):
177
            qs[q.resource.name] = q.capacity
178
        initial[user.uuid] = {SYSTEM: qs}
171 179
    return initial
172 180

  
173 181

  

Also available in: Unified diff