Revision 0da5e49a snf-astakos-app/astakos/im/models.py
b/snf-astakos-app/astakos/im/models.py | ||
---|---|---|
84 | 84 |
from synnefo.lib.db.intdecimalfield import intDecimalField |
85 | 85 |
from synnefo.util.text import uenc, udec |
86 | 86 |
|
87 |
from astakos.im.quotas import get_users_quotas_and_limits, set_user_quota |
|
88 |
|
|
87 | 89 |
logger = logging.getLogger(__name__) |
88 | 90 |
|
89 | 91 |
DEFAULT_CONTENT_TYPE = None |
... | ... | |
774 | 776 |
raise ValueError("could not compute quotas") |
775 | 777 |
|
776 | 778 |
|
779 |
SYSTEM = 'system' |
|
780 |
|
|
777 | 781 |
def initial_quotas(users): |
778 | 782 |
initial = {} |
779 | 783 |
default_quotas = get_default_quota() |
780 | 784 |
|
781 | 785 |
for user in users: |
782 | 786 |
uuid = user.uuid |
783 |
initial[uuid] = dict(default_quotas) |
|
787 |
source_quota = {SYSTEM: dict(default_quotas)} |
|
788 |
initial[uuid] = source_quota |
|
784 | 789 |
|
785 | 790 |
objs = AstakosUserQuota.objects.select_related() |
786 | 791 |
orig_quotas = objs.filter(user__in=users) |
... | ... | |
794 | 799 |
return initial |
795 | 800 |
|
796 | 801 |
|
802 |
def get_grant_source(grant): |
|
803 |
return SYSTEM |
|
804 |
|
|
805 |
|
|
797 | 806 |
def users_quotas(users, initial=None): |
798 | 807 |
if initial is None: |
799 | 808 |
quotas = initial_quotas(users) |
800 | 809 |
else: |
801 | 810 |
quotas = copy.deepcopy(initial) |
802 | 811 |
|
803 |
objs = ProjectMembership.objects.select_related('application', 'person') |
|
804 |
memberships = objs.filter(person__in=users) |
|
812 |
ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED |
|
813 |
objs = ProjectMembership.objects.select_related('project', 'person') |
|
814 |
memberships = objs.filter(person__in=users, |
|
815 |
state__in=ACTUALLY_ACCEPTED, |
|
816 |
project__state=Project.APPROVED) |
|
817 |
|
|
818 |
project_ids = set(m.project_id for m in memberships) |
|
819 |
objs = ProjectApplication.objects.select_related('project') |
|
820 |
apps = objs.filter(project__in=project_ids) |
|
821 |
|
|
822 |
project_dict = {} |
|
823 |
for app in apps: |
|
824 |
project_dict[app.project] = app |
|
805 | 825 |
|
806 |
apps = set(m.application for m in memberships if m.application is not None) |
|
807 | 826 |
objs = ProjectResourceGrant.objects.select_related() |
808 | 827 |
grants = objs.filter(project_application__in=apps) |
809 | 828 |
|
... | ... | |
811 | 830 |
uuid = membership.person.uuid |
812 | 831 |
userquotas = quotas.get(uuid, {}) |
813 | 832 |
|
814 |
application = membership.application |
|
815 |
if application is None: |
|
816 |
continue |
|
833 |
application = project_dict[membership.project] |
|
817 | 834 |
|
818 | 835 |
for grant in grants: |
819 | 836 |
if grant.project_application_id != application.id: |
820 | 837 |
continue |
838 |
|
|
839 |
source = get_grant_source(grant) |
|
840 |
source_quotas = userquotas.get(source, {}) |
|
841 |
|
|
821 | 842 |
resource = grant.resource.full_name() |
822 |
prev = userquotas.get(resource, 0)
|
|
843 |
prev = source_quotas.get(resource, 0)
|
|
823 | 844 |
new = prev + grant.member_capacity |
824 |
userquotas[resource] = new |
|
845 |
source_quotas[resource] = new |
|
846 |
userquotas[source] = source_quotas |
|
825 | 847 |
quotas[uuid] = userquotas |
826 | 848 |
|
827 | 849 |
return quotas |
... | ... | |
2305 | 2327 |
for user in users: |
2306 | 2328 |
info[user.uuid] = user.email |
2307 | 2329 |
|
2308 |
resources = get_resource_names() |
|
2309 |
qh_limits, qh_counters = qh_get_quotas(users, resources) |
|
2330 |
qh_quotas, qh_limits = get_users_quotas_and_limits(users) |
|
2310 | 2331 |
astakos_initial = initial_quotas(users) |
2311 | 2332 |
astakos_quotas = users_quotas(users) |
2312 | 2333 |
|
... | ... | |
2317 | 2338 |
diff_quotas[holder] = dict(local) |
2318 | 2339 |
|
2319 | 2340 |
if sync: |
2320 |
r = send_quotas(diff_quotas)
|
|
2341 |
r = set_user_quota(diff_quotas)
|
|
2321 | 2342 |
|
2322 |
return (qh_limits, qh_counters,
|
|
2343 |
return (qh_limits, qh_quotas,
|
|
2323 | 2344 |
astakos_initial, diff_quotas, info) |
2324 | 2345 |
|
2325 | 2346 |
return _sync_users(users, sync) |
Also available in: Unified diff