Revision 9747707e snf-astakos-app/astakos/im/resources.py
b/snf-astakos-app/astakos/im/resources.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from astakos.im.models import Service, Resource |
35 | 35 |
from astakos.im.functions import qh_sync_all_users |
36 |
from astakos.im.quotas import qh_add_resource_limit, qh_sync_new_resource |
|
37 |
import logging |
|
36 | 38 |
|
39 |
logger = logging.getLogger(__name__) |
|
37 | 40 |
|
38 |
def add_resources(service, resources, conf): |
|
41 |
|
|
42 |
def add_resource(service, resource, uplimit): |
|
39 | 43 |
try: |
40 | 44 |
s = Service.objects.get(name=service) |
41 | 45 |
except Service.DoesNotExist: |
42 | 46 |
raise Exception("Service %s is not registered." % (service)) |
43 | 47 |
|
44 |
names = [resource['name'] for resource in resources] |
|
45 |
rs = Resource.objects.filter(name__in=names).select_for_update() |
|
46 |
rs = dict((r.name, r) for r in rs) |
|
47 |
|
|
48 |
for resource in resources: |
|
49 |
name = resource['name'] |
|
50 |
existing = rs.get(name) |
|
51 |
r = existing if existing is not None else Resource() |
|
48 |
name = resource['name'] |
|
49 |
try: |
|
50 |
r = Resource.objects.get_for_update(name=name) |
|
51 |
old_uplimit = r.uplimit |
|
52 |
except Resource.DoesNotExist: |
|
53 |
r = Resource() |
|
54 |
old_uplimit = None |
|
52 | 55 |
|
53 |
uplimit = conf.get(name) |
|
54 |
if uplimit is None: |
|
55 |
raise Exception("Limit for resource %s is missing." % (name)) |
|
56 |
r.uplimit = uplimit |
|
57 |
r.service = s |
|
58 |
for key, value in resource.iteritems(): |
|
59 |
setattr(r, key, value) |
|
56 | 60 |
|
57 |
if not isinstance(uplimit, (int, long)): |
|
58 |
raise Exception("Limit for resource %s is not an integer." % |
|
59 |
(name)) |
|
61 |
r.save() |
|
60 | 62 |
|
61 |
r.uplimit = uplimit
|
|
62 |
r.service = s
|
|
63 |
for key, value in resource.iteritems():
|
|
64 |
setattr(r, key, value)
|
|
63 |
if old_uplimit is not None:
|
|
64 |
logger.info("Updated resource %s with limit %s." % (name, uplimit))
|
|
65 |
else:
|
|
66 |
logger.info("Added resource %s with limit %s." % (name, uplimit))
|
|
65 | 67 |
|
66 |
r.save() |
|
67 |
qh_sync_all_users() |
|
68 |
if old_uplimit is not None: |
|
69 |
diff = uplimit - old_uplimit |
|
70 |
if diff != 0: |
|
71 |
qh_add_resource_limit(name, diff) |
|
72 |
else: |
|
73 |
qh_sync_new_resource(name, uplimit) |
Also available in: Unified diff