Revision f72ba65d snf-astakos-app/astakos/im/quotas.py

b/snf-astakos-app/astakos/im/quotas.py
121 121
    qh.set_quota(q)
122 122

  
123 123

  
124
def get_default_quota():
125
    _DEFAULT_QUOTA = {}
126
    resources = Resource.objects.select_related('service').all()
127
    for resource in resources:
128
        capacity = resource.uplimit
129
        _DEFAULT_QUOTA[resource.full_name()] = capacity
130

  
131
    return _DEFAULT_QUOTA
132

  
133

  
134 124
SYSTEM = 'system'
135 125
PENDING_APP_RESOURCE = 'astakos.pending_app'
136 126

  
......
153 143
    return quota[SYSTEM][PENDING_APP_RESOURCE]
154 144

  
155 145

  
156
def add_base_quota(user, resource, capacity):
157
    resource = Resource.objects.get(name=resource)
158
    user = get_user_for_update(user.id)
159
    obj, created = AstakosUserQuota.objects.get_or_create(
160
        user=user, resource=resource, defaults={
161
            'capacity': capacity,
162
        })
163

  
164
    if not created:
165
        obj.capacity = capacity
166
        obj.save()
167
    qh_sync_locked_user(user)
168

  
169

  
170
def remove_base_quota(user, resource):
171
    user = get_user_for_update(user.id)
172
    AstakosUserQuota.objects.filter(
173
        user=user, resource__name=resource).delete()
174
    qh_sync_locked_user(user)
146
def update_base_quota(quota, capacity):
147
    quota.capacity = capacity
148
    quota.save()
149
    qh_sync_locked_user(quota.user)
175 150

  
176 151

  
177 152
def initial_quotas(users):
178
    users = list(users)
179
    initial = {}
180
    default_quotas = get_default_quota()
181

  
182
    for user in users:
183
        uuid = user.uuid
184
        source_quota = {SYSTEM: dict(default_quotas)}
185
        initial[uuid] = source_quota
186

  
187 153
    userids = [user.pk for user in users]
188 154
    objs = AstakosUserQuota.objects.select_related()
189 155
    orig_quotas = objs.filter(user__pk__in=userids)
156
    initial = {}
190 157
    for user_quota in orig_quotas:
191 158
        uuid = user_quota.user.uuid
192 159
        user_init = initial.get(uuid, {})
......
304 271
    qh_sync_users([user])
305 272

  
306 273

  
274
def qh_sync_new_users(users):
275
    entries = []
276
    for resource in Resource.objects.all():
277
        for user in users:
278
            entries.append(
279
                AstakosUserQuota(user=user, resource=resource,
280
                                 capacity=resource.uplimit))
281
    AstakosUserQuota.objects.bulk_create(entries)
282
    qh_sync_users(users)
283

  
284

  
285
def qh_sync_new_user(user):
286
    qh_sync_new_users([user])
287

  
288

  
307 289
def members_to_sync(project):
308 290
    objs = ProjectMembership.objects.select_related('person')
309 291
    memberships = objs.filter(project=project,
......
316 298
    qh_sync_users(users)
317 299

  
318 300

  
319
def qh_change_resource_limit(resource):
320
    objs = AstakosUser.objects.filter(
321
        Q(moderated=True, is_rejected=False) & ~Q(policy=resource))
322
    users = objs.order_by('id').select_for_update()
323
    quota = astakos_users_quotas(users)
324
    _set_user_quota(quota)
325

  
326

  
327 301
def qh_sync_new_resource(resource):
328 302
    users = AstakosUser.objects.filter(
329 303
        moderated=True, is_rejected=False).order_by('id').select_for_update()
330 304

  
331
    resource_name = resource.name
332
    limit = resource.uplimit
333
    data = []
305
    entries = []
334 306
    for user in users:
335
        uuid = user.uuid
336
        key = uuid, SYSTEM, resource_name
337
        data.append((key, limit))
338

  
339
    qh.set_quota(data)
307
        entries.append(
308
            AstakosUserQuota(user=user, resource=resource,
309
                             capacity=resource.uplimit))
310
    AstakosUserQuota.objects.bulk_create(entries)
311
    qh_sync_users(users)

Also available in: Unified diff