Commit 4904cc6e authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Move quota-related code out of models.py

Intention is to collect all quota-related code to a single file.
parent 81cadb43
......@@ -70,7 +70,8 @@ from astakos.im.notifications import build_notification, NotificationError
from astakos.im.models import (
AstakosUser, Invitation, ProjectMembership, ProjectApplication, Project,
UserSetting,
get_resource_names, new_chain, users_quotas)
get_resource_names, new_chain)
from astakos.im.quotas import users_quotas
from astakos.im.project_notif import (
membership_change_notify, membership_enroll_notify,
membership_request_notify, membership_leave_request_notify,
......
......@@ -34,7 +34,8 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from astakos.im.models import sync_all_users, sync_users, AstakosUser
from astakos.im.models import AstakosUser
from astakos.im.quotas import sync_all_users, sync_users
from astakos.im.functions import get_user_by_uuid
from astakos.im.management.commands._common import is_uuid, is_email
from synnefo.lib.db.transaction import commit_on_success_strict
......
......@@ -34,6 +34,7 @@
from django.core.management.base import BaseCommand, CommandError
from astakos.im.models import AstakosUser, get_latest_terms
from astakos.im.quotas import user_quotas
from astakos.im.util import model_to_dict
from synnefo.lib.ordereddict import OrderedDict
......@@ -66,7 +67,7 @@ class Command(BaseCommand):
raise CommandError(msg)
for user in users:
quotas = user.all_quotas()
quotas = user_quotas(user)
settings_dict = {}
settings = user.settings()
......
......@@ -84,8 +84,6 @@ from astakos.quotaholder.api import QH_PRACTICALLY_INFINITE
from synnefo.lib.db.intdecimalfield import intDecimalField
from synnefo.util.text import uenc, udec
from astakos.im.quotas import get_users_quotas_and_limits, set_user_quota
logger = logging.getLogger(__name__)
DEFAULT_CONTENT_TYPE = None
......@@ -257,16 +255,6 @@ def load_service_resources():
register_resources(rs)
def get_default_quota():
_DEFAULT_QUOTA = {}
resources = Resource.objects.select_related('service').all()
for resource in resources:
capacity = resource.uplimit
_DEFAULT_QUOTA[resource.full_name()] = capacity
return _DEFAULT_QUOTA
def get_resource_names():
_RESOURCE_NAMES = []
resources = Resource.objects.select_related('service').all()
......@@ -768,86 +756,6 @@ class AstakosUser(User):
def settings(self):
return UserSetting.objects.filter(user=self)
def all_quotas(self):
quotas = users_quotas([self])
try:
return quotas[self.uuid]
except:
raise ValueError("could not compute quotas")
SYSTEM = 'system'
def initial_quotas(users):
initial = {}
default_quotas = get_default_quota()
for user in users:
uuid = user.uuid
source_quota = {SYSTEM: dict(default_quotas)}
initial[uuid] = source_quota
objs = AstakosUserQuota.objects.select_related()
orig_quotas = objs.filter(user__in=users)
for user_quota in orig_quotas:
uuid = user_quota.user.uuid
user_init = initial.get(uuid, {})
resource = user_quota.resource.full_name()
user_init[resource] = user_quota.capacity
initial[uuid] = user_init
return initial
def get_grant_source(grant):
return SYSTEM
def users_quotas(users, initial=None):
if initial is None:
quotas = initial_quotas(users)
else:
quotas = copy.deepcopy(initial)
ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED
objs = ProjectMembership.objects.select_related('project', 'person')
memberships = objs.filter(person__in=users,
state__in=ACTUALLY_ACCEPTED,
project__state=Project.APPROVED)
project_ids = set(m.project_id for m in memberships)
objs = ProjectApplication.objects.select_related('project')
apps = objs.filter(project__in=project_ids)
project_dict = {}
for app in apps:
project_dict[app.project] = app
objs = ProjectResourceGrant.objects.select_related()
grants = objs.filter(project_application__in=apps)
for membership in memberships:
uuid = membership.person.uuid
userquotas = quotas.get(uuid, {})
application = project_dict[membership.project]
for grant in grants:
if grant.project_application_id != application.id:
continue
source = get_grant_source(grant)
source_quotas = userquotas.get(source, {})
resource = grant.resource.full_name()
prev = source_quotas.get(resource, 0)
new = prev + grant.member_capacity
source_quotas[resource] = new
userquotas[source] = source_quotas
quotas[uuid] = userquotas
return quotas
class AstakosUserAuthProviderManager(models.Manager):
......@@ -2320,36 +2228,6 @@ class Serial(models.Model):
serial = models.AutoField(primary_key=True)
def sync_users(users, sync=True):
def _sync_users(users, sync):
info = {}
for user in users:
info[user.uuid] = user.email
qh_quotas, qh_limits = get_users_quotas_and_limits(users)
astakos_initial = initial_quotas(users)
astakos_quotas = users_quotas(users)
diff_quotas = {}
for holder, local in astakos_quotas.iteritems():
registered = qh_limits.get(holder, None)
if local != registered:
diff_quotas[holder] = dict(local)
if sync:
r = set_user_quota(diff_quotas)
return (qh_limits, qh_quotas,
astakos_initial, diff_quotas, info)
return _sync_users(users, sync)
def sync_all_users(sync=True):
users = AstakosUser.objects.verified()
return sync_users(users, sync)
class ProjectMembershipHistory(models.Model):
reasons_list = ['ACCEPT', 'REJECT', 'REMOVE']
reasons = dict((k, v) for v, k in enumerate(reasons_list))
......
......@@ -31,6 +31,9 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from astakos.im.models import (
Resource, AstakosUserQuota, AstakosUser,
Project, ProjectMembership, ProjectResourceGrant, ProjectApplication)
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint
qh = QuotaholderDjangoDBCallpoint()
......@@ -94,3 +97,126 @@ def get_user_quotas(user, resources=None, sources=None):
def set_user_quota(quotas):
qh.set_holder_quota(quotas)
def get_default_quota():
_DEFAULT_QUOTA = {}
resources = Resource.objects.select_related('service').all()
for resource in resources:
capacity = resource.uplimit
_DEFAULT_QUOTA[resource.full_name()] = capacity
return _DEFAULT_QUOTA
SYSTEM = 'system'
def initial_quotas(users):
initial = {}
default_quotas = get_default_quota()
for user in users:
uuid = user.uuid
source_quota = {SYSTEM: dict(default_quotas)}
initial[uuid] = source_quota
objs = AstakosUserQuota.objects.select_related()
orig_quotas = objs.filter(user__in=users)
for user_quota in orig_quotas:
uuid = user_quota.user.uuid
user_init = initial.get(uuid, {})
resource = user_quota.resource.full_name()
user_init[resource] = user_quota.capacity
initial[uuid] = user_init
return initial
def get_grant_source(grant):
return SYSTEM
def users_quotas(users, initial=None):
if initial is None:
quotas = initial_quotas(users)
else:
quotas = copy.deepcopy(initial)
ACTUALLY_ACCEPTED = ProjectMembership.ACTUALLY_ACCEPTED
objs = ProjectMembership.objects.select_related('project', 'person')
memberships = objs.filter(person__in=users,
state__in=ACTUALLY_ACCEPTED,
project__state=Project.APPROVED)
project_ids = set(m.project_id for m in memberships)
objs = ProjectApplication.objects.select_related('project')
apps = objs.filter(project__in=project_ids)
project_dict = {}
for app in apps:
project_dict[app.project] = app
objs = ProjectResourceGrant.objects.select_related()
grants = objs.filter(project_application__in=apps)
for membership in memberships:
uuid = membership.person.uuid
userquotas = quotas.get(uuid, {})
application = project_dict[membership.project]
for grant in grants:
if grant.project_application_id != application.id:
continue
source = get_grant_source(grant)
source_quotas = userquotas.get(source, {})
resource = grant.resource.full_name()
prev = source_quotas.get(resource, 0)
new = prev + grant.member_capacity
source_quotas[resource] = new
userquotas[source] = source_quotas
quotas[uuid] = userquotas
return quotas
def user_quotas(user):
quotas = users_quotas([user])
try:
return quotas[user.uuid]
except KeyError:
raise ValueError("could not compute quotas")
def sync_users(users, sync=True):
def _sync_users(users, sync):
info = {}
for user in users:
info[user.uuid] = user.email
qh_quotas, qh_limits = get_users_quotas_and_limits(users)
astakos_initial = initial_quotas(users)
astakos_quotas = users_quotas(users)
diff_quotas = {}
for holder, local in astakos_quotas.iteritems():
registered = qh_limits.get(holder, None)
if local != registered:
diff_quotas[holder] = dict(local)
if sync:
r = set_user_quota(diff_quotas)
return (qh_limits, qh_quotas,
astakos_initial, diff_quotas, info)
return _sync_users(users, sync)
def sync_all_users(sync=True):
users = AstakosUser.objects.verified()
return sync_users(users, sync)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment