Revision 3c22bad0 snf-astakos-app/astakos/im/quotas.py

b/snf-astakos-app/astakos/im/quotas.py
128 128

  
129 129

  
130 130
SYSTEM = 'system'
131
PENDING_APP_RESOURCE = 'astakos.pending_app'
131 132

  
132 133

  
133
def register_pending_apps(user, quantity, force=False, dry_run=False):
134
    provision = (user.uuid, SYSTEM, 'astakos.pending_app'), quantity
135
    name = "DRYRUN" if dry_run else ""
134
def register_pending_apps(user, quantity, force=False):
135
    provision = (user.uuid, SYSTEM, PENDING_APP_RESOURCE), quantity
136 136
    try:
137 137
        s = qh.issue_commission(clientkey='astakos',
138 138
                                force=force,
139
                                name=name,
140 139
                                provisions=[provision])
141 140
    except NoCapacityError as e:
142 141
        limit = e.data['limit']
143 142
        return False, limit
144
    accept = not dry_run
145
    qh.resolve_pending_commission('astakos', s, accept)
143
    qh.resolve_pending_commission('astakos', s)
146 144
    return True, None
147 145

  
148 146

  
147
def get_pending_app_quota(user):
148
    quota = get_user_quotas(user)
149
    return quota[SYSTEM][PENDING_APP_RESOURCE]
150

  
151

  
149 152
def add_base_quota(user, resource, capacity):
150 153
    resource = Resource.objects.get(name=resource)
154
    user = get_user_for_update(user.id)
151 155
    obj, created = AstakosUserQuota.objects.get_or_create(
152 156
        user=user, resource=resource, defaults={
153 157
            'capacity': capacity,
......
156 160
    if not created:
157 161
        obj.capacity = capacity
158 162
        obj.save()
159
    qh_sync_user(user)
163
    qh_sync_locked_user(user)
160 164

  
161 165

  
162 166
def remove_base_quota(user, resource):
167
    user = get_user_for_update(user.id)
163 168
    AstakosUserQuota.objects.filter(
164 169
        user=user, resource__name=resource).delete()
165
    qh_sync_user(user)
170
    qh_sync_locked_user(user)
166 171

  
167 172

  
168 173
def initial_quotas(users):
......
248 253

  
249 254
# Syncing to quotaholder
250 255

  
251
def qh_sync_users(users, sync=True, diff_only=False):
256
def get_users_for_update(user_ids):
257
    uids = sorted(user_ids)
258
    objs = AstakosUser.forupdate
259
    return list(objs.filter(id__in=uids).order_by('id').select_for_update())
260

  
261

  
262
def get_user_for_update(user_id):
263
    return get_users_for_update([user_id])[0]
264

  
265

  
266
def qh_sync_locked_users(users):
267
    astakos_quotas = astakos_users_quotas(users)
268
    _set_user_quota(astakos_quotas)
269

  
270

  
271
def qh_sync_users(users):
272
    uids = [user.id for user in users]
273
    users = get_users_for_update(uids)
274
    qh_sync_locked_users(users)
275

  
276

  
277
def qh_sync_users_diffs(users, sync=True):
252 278
    uids = [user.id for user in users]
253 279
    if sync:
254
        users = AstakosUser.forupdate.filter(id__in=uids).select_for_update()
280
        users = get_users_for_update(uids)
255 281

  
256 282
    astakos_quotas = astakos_users_quotas(users)
283
    qh_limits = get_users_quota_limits(users)
284
    diff_quotas = {}
285
    for holder, local in astakos_quotas.iteritems():
286
        registered = qh_limits.get(holder, None)
287
        if local != registered:
288
            diff_quotas[holder] = dict(local)
289

  
290
    if sync:
291
        _set_user_quota(diff_quotas)
292
    return qh_limits, diff_quotas
257 293

  
258
    if diff_only:
259
        qh_limits = get_users_quota_limits(users)
260
        diff_quotas = {}
261
        for holder, local in astakos_quotas.iteritems():
262
            registered = qh_limits.get(holder, None)
263
            if local != registered:
264
                diff_quotas[holder] = dict(local)
265

  
266
        if sync:
267
            _set_user_quota(diff_quotas)
268
        return qh_limits, diff_quotas
269
    else:
270
        if sync:
271
            _set_user_quota(astakos_quotas)
272
        return None
294

  
295
def qh_sync_locked_user(user):
296
    qh_sync_locked_users([user])
273 297

  
274 298

  
275 299
def qh_sync_user(user):
276 300
    qh_sync_users([user])
277 301

  
278 302

  
279
def qh_sync_projects(projects):
280
    projects = list(projects)
281
    memberships = ProjectMembership.objects.filter(
282
        project__in=projects, state__in=ProjectMembership.ACTUALLY_ACCEPTED)
283
    users = set(m.person for m in memberships)
284

  
285
    qh_sync_users(users)
303
def members_to_sync(project):
304
    objs = ProjectMembership.objects.select_related('person')
305
    memberships = objs.filter(project=project,
306
                              state__in=ProjectMembership.ACTUALLY_ACCEPTED)
307
    return set(m.person for m in memberships)
286 308

  
287 309

  
288 310
def qh_sync_project(project):
289
    qh_sync_projects([project])
311
    users = members_to_sync(project)
312
    qh_sync_users(users)
290 313

  
291 314

  
292 315
def qh_add_resource_limit(resource, diff):
293 316
    objs = AstakosUser.forupdate.filter(Q(email_verified=True) &
294 317
                                        ~Q(policy=resource))
295
    users = objs.select_for_update()
318
    users = objs.order_by('id').select_for_update()
296 319
    uuids = [u.uuid for u in users]
297 320
    qh.add_resource_limit(holders=uuids, sources=[SYSTEM],
298 321
                          resources=[resource.name], diff=diff)
......
300 323

  
301 324
def qh_sync_new_resource(resource, limit):
302 325
    users = AstakosUser.forupdate.filter(
303
        email_verified=True).select_for_update()
326
        email_verified=True).order_by('id').select_for_update()
304 327

  
305 328
    resource_name = resource.name
306 329
    data = []

Also available in: Unified diff