Revision a989b48e snf-astakos-app/astakos/im/models.py

b/snf-astakos-app/astakos/im/models.py
36 36
import logging
37 37
import json
38 38
import math
39
import copy
39 40

  
40 41
from time import asctime
41 42
from datetime import datetime, timedelta
......
266 267

  
267 268
def get_default_quota():
268 269
    _DEFAULT_QUOTA = {}
269
    resources = Resource.objects.all()
270
    resources = Resource.objects.select_related('service').all()
270 271
    for resource in resources:
271 272
        capacity = resource.uplimit
272 273
        limits = _quota_values(capacity)
......
276 277

  
277 278
def get_resource_names():
278 279
    _RESOURCE_NAMES = []
279
    resources = Resource.objects.all()
280
    resources = Resource.objects.select_related('service').all()
280 281
    _RESOURCE_NAMES = [resource.full_name() for resource in resources]
281 282
    return _RESOURCE_NAMES
282 283

  
......
444 445
        except Invitation.DoesNotExist:
445 446
            return None
446 447

  
447
    def initial_quotas(self):
448
        quotas = dict(get_default_quota())
449
        for user_quota in self.policies:
450
            resource = user_quota.resource.full_name()
451
            quotas[resource] = user_quota.quota_values()
452
        return quotas
453

  
454
    def all_quotas(self, initial=None):
455
        if initial is None:
456
            quotas = self.initial_quotas()
457
        else:
458
            quotas = dict(initial)
459

  
460
        objects = self.projectmembership_set.select_related()
461
        memberships = objects.filter(is_active=True)
462
        for membership in memberships:
463
            application = membership.application
464
            if application is None:
465
                m = _("missing application for active membership %s"
466
                      % (membership,))
467
                raise AssertionError(m)
468

  
469
            grants = application.projectresourcegrant_set.all()
470
            for grant in grants:
471
                resource = grant.resource.full_name()
472
                prev = quotas.get(resource, 0)
473
                new = add_quota_values(prev, grant.member_quota_values())
474
                quotas[resource] = new
475
        return quotas
476

  
477 448
    @property
478 449
    def policies(self):
479 450
        return self.astakosuserquota_set.select_related().all()
......
845 816
        return UserSetting.objects.filter(user=self)
846 817

  
847 818

  
819
def initial_quotas(users):
820
    initial = {}
821
    default_quotas = get_default_quota()
822

  
823
    for user in users:
824
        uuid = user.uuid
825
        initial[uuid] = dict(default_quotas)
826

  
827
    objs = AstakosUserQuota.objects.select_related()
828
    orig_quotas = objs.filter(user__in=users)
829
    for user_quota in orig_quotas:
830
        uuid = user_quota.uuid
831
        user_init = initial.get(uuid, {})
832
        resource = user_quota.resource.full_name()
833
        user_init[resource] = user_quota.quota_values()
834
        initial[uuid] = user_init
835

  
836
    return initial
837

  
838

  
839
def users_quotas(users, initial=None):
840
    if initial is None:
841
        quotas = initial_quotas(users)
842
    else:
843
        quotas = copy.deepcopy(initial)
844

  
845
    objs = ProjectMembership.objects.select_related('application', 'person')
846
    memberships = objs.filter(person__in=users, is_active=True)
847

  
848
    apps = set(m.application for m in memberships if m.application is not None)
849
    objs = ProjectResourceGrant.objects.select_related()
850
    grants = objs.filter(project_application__in=apps)
851

  
852
    for membership in memberships:
853
        uuid = membership.person.uuid
854
        userquotas = quotas.get(uuid, {})
855

  
856
        application = membership.application
857
        if application is None:
858
            m = _("missing application for active membership %s"
859
                  % (membership,))
860
            raise AssertionError(m)
861

  
862
        for grant in grants:
863
            if grant.project_application_id != application.id:
864
                continue
865
            resource = grant.resource.full_name()
866
            prev = userquotas.get(resource, 0)
867
            new = add_quota_values(prev, grant.member_quota_values())
868
            userquotas[resource] = new
869
        quotas[uuid] = userquotas
870

  
871
    return quotas
872

  
873

  
848 874
class AstakosUserAuthProviderManager(models.Manager):
849 875

  
850 876
    def active(self, **filters):
......
2665 2691
        return (pending, projects_log)
2666 2692
    return _sync_projects(sync)
2667 2693

  
2668
def all_users_quotas(users):
2669
    initial = {}
2670
    quotas = {}
2671
    info = {}
2672
    for user in users:
2673
        uuid = user.uuid
2674
        info[uuid] = user.email
2675
        init = user.initial_quotas()
2676
        initial[uuid] = init
2677
        quotas[user.uuid] = user.all_quotas(initial=init)
2678
    return initial, quotas, info
2694

  
2679 2695

  
2680 2696
def sync_users(users, sync=True, retries=3, retry_wait=1.0):
2681 2697
    @with_lock(retries, retry_wait)
2682 2698
    def _sync_users(users, sync):
2683 2699
        sync_finish_serials()
2684 2700

  
2701
        info = {}
2702
        for user in users:
2703
            info[user.uuid] = user.email
2704

  
2685 2705
        existing, nonexisting = qh_check_users(users)
2686 2706
        resources = get_resource_names()
2687 2707
        qh_limits, qh_counters = qh_get_quotas(existing, resources)
2688
        astakos_initial, astakos_quotas, info = all_users_quotas(users)
2708
        astakos_initial = initial_quotas(users)
2709
        astakos_quotas = users_quotas(users, astakos_initial)
2689 2710

  
2690 2711
        if sync:
2691 2712
            r = register_users(nonexisting)
......
2696 2717
                astakos_initial, astakos_quotas, info)
2697 2718
    return _sync_users(users, sync)
2698 2719

  
2720

  
2699 2721
def sync_all_users(sync=True, retries=3, retry_wait=1.0):
2700 2722
    users = AstakosUser.objects.verified()
2701 2723
    return sync_users(users, sync, retries, retry_wait)

Also available in: Unified diff