Commit bf8e4fc0 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Respect user-specific quota when changing resource default

Do not update holding limit if there exist user-specific base quota
for the particular resource.
parent 357c3f0d
...@@ -35,6 +35,7 @@ from astakos.im.models import ( ...@@ -35,6 +35,7 @@ from astakos.im.models import (
Resource, AstakosUserQuota, AstakosUser, Resource, AstakosUserQuota, AstakosUser,
Project, ProjectMembership, ProjectResourceGrant, ProjectApplication) Project, ProjectMembership, ProjectResourceGrant, ProjectApplication)
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint
from django.db.models import Q
qh = QuotaholderDjangoDBCallpoint() qh = QuotaholderDjangoDBCallpoint()
...@@ -248,18 +249,23 @@ def sync_all_users(sync=True): ...@@ -248,18 +249,23 @@ def sync_all_users(sync=True):
def qh_add_resource_limit(resource, diff): def qh_add_resource_limit(resource, diff):
users = AstakosUser.forupdate.all().select_for_update() objs = AstakosUser.forupdate.filter(Q(email_verified=True) &
qh.add_resource_limit(SYSTEM, resource, diff) ~Q(policy=resource))
users = objs.select_for_update()
uuids = [u.uuid for u in users]
qh.add_resource_limit(holders=uuids, sources=[SYSTEM],
resources=[resource.name], diff=diff)
def qh_sync_new_resource(resource, limit): def qh_sync_new_resource(resource, limit):
users = AstakosUser.forupdate.filter( users = AstakosUser.forupdate.filter(
email_verified=True).select_for_update() email_verified=True).select_for_update()
resource_name = resource.name
data = [] data = []
for user in users: for user in users:
uuid = user.uuid uuid = user.uuid
key = uuid, SYSTEM, resource key = uuid, SYSTEM, resource_name
data.append((key, limit)) data.append((key, limit))
qh.set_quota(data) qh.set_quota(data)
...@@ -68,9 +68,9 @@ def add_resource(service, resource, uplimit): ...@@ -68,9 +68,9 @@ def add_resource(service, resource, uplimit):
if old_uplimit is not None: if old_uplimit is not None:
diff = uplimit - old_uplimit diff = uplimit - old_uplimit
if diff != 0: if diff != 0:
qh_add_resource_limit(name, diff) qh_add_resource_limit(r, diff)
else: else:
qh_sync_new_resource(name, uplimit) qh_sync_new_resource(r, uplimit)
def update_resource(name, uplimit): def update_resource(name, uplimit):
...@@ -82,4 +82,4 @@ def update_resource(name, uplimit): ...@@ -82,4 +82,4 @@ def update_resource(name, uplimit):
logger.info("Updated resource %s with limit %s." % (name, uplimit)) logger.info("Updated resource %s with limit %s." % (name, uplimit))
diff = uplimit - old_uplimit diff = uplimit - old_uplimit
if diff != 0: if diff != 0:
qh_add_resource_limit(name, diff) qh_add_resource_limit(r, diff)
...@@ -135,9 +135,20 @@ class QuotaholderDjangoDBCallpoint(object): ...@@ -135,9 +135,20 @@ class QuotaholderDjangoDBCallpoint(object):
h.save() h.save()
holdings[key] = h holdings[key] = h
def add_resource_limit(self, source, resource, diff): def add_resource_limit(self, holders=None, sources=None, resources=None,
objs = Holding.objects.filter(source=source, resource=resource) diff=0):
objs.update(limit=F('limit')+diff) holdings = Holding.objects.all()
if holders is not None:
holdings = holdings.filter(holder__in=holders)
if sources is not None:
holdings = holdings.filter(source__in=sources)
if resources is not None:
holdings = holdings.filter(resource__in=resources)
holdings.update(limit=F('limit')+diff)
def issue_commission(self, def issue_commission(self,
context=None, context=None,
......
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