Revision d2b32360 snf-quotaholder-app/quotaholder_django/quotaholder_app/callpoint.py
b/snf-quotaholder-app/quotaholder_django/quotaholder_app/callpoint.py | ||
---|---|---|
506 | 506 |
raise ReturnButFail(rejected) |
507 | 507 |
return rejected |
508 | 508 |
|
509 |
def add_quota(self, context={}, clientkey=None, serial=None, add_quota=()): |
|
509 |
def add_quota(self, context={}, clientkey=None, serial=None, |
|
510 |
sub_quota=(), add_quota=()): |
|
510 | 511 |
rejected = [] |
511 | 512 |
append = rejected.append |
512 |
all_pairs = [(q[0], q[1]) for q in add_quota] |
|
513 | 513 |
|
514 | 514 |
if serial is not None: |
515 | 515 |
if clientkey is None: |
516 |
all_pairs = [(q[0], q[1]) for q in sub_quota + add_quota] |
|
516 | 517 |
raise ReturnButFail(all_pairs) |
517 | 518 |
try: |
518 | 519 |
cs = CallSerial.objects.get(serial=serial, clientkey=clientkey) |
520 |
all_pairs = [(q[0], q[1]) for q in sub_quota + add_quota] |
|
519 | 521 |
raise ReturnButFail(all_pairs) |
520 | 522 |
except CallSerial.DoesNotExist: |
521 | 523 |
pass |
522 | 524 |
|
523 |
for ( entity, resource, key, |
|
524 |
quantity, capacity, |
|
525 |
import_limit, export_limit ) in add_quota: |
|
526 |
|
|
527 |
try: |
|
528 |
e = Entity.objects.get(entity=entity, key=key) |
|
529 |
except Entity.DoesNotExist: |
|
530 |
append((entity, resource)) |
|
531 |
continue |
|
525 |
for removing, source in [(True, sub_quota), (False, add_quota)]: |
|
526 |
for ( entity, resource, key, |
|
527 |
quantity, capacity, |
|
528 |
import_limit, export_limit ) in source: |
|
532 | 529 |
|
533 |
try: |
|
534 |
h = db_get_holding(entity=entity, resource=resource, |
|
535 |
for_update=True) |
|
536 |
p = h.policy |
|
537 |
except Holding.DoesNotExist: |
|
538 |
h = Holding(entity=e, resource=resource, flags=0) |
|
539 |
p = None |
|
530 |
try: |
|
531 |
e = Entity.objects.get(entity=entity, key=key) |
|
532 |
except Entity.DoesNotExist: |
|
533 |
append((entity, resource)) |
|
534 |
continue |
|
540 | 535 |
|
541 |
policy = newname('policy_') |
|
542 |
newp = Policy(policy=policy) |
|
543 |
|
|
544 |
newp.quantity = _add(p.quantity if p else 0, quantity) |
|
545 |
newp.capacity = _add(p.capacity if p else 0, capacity) |
|
546 |
newp.import_limit = _add(p.import_limit if p else 0, |
|
547 |
import_limit) |
|
548 |
newp.export_limit = _add(p.export_limit if p else 0, |
|
549 |
export_limit) |
|
550 |
|
|
551 |
new_values = [newp.capacity, |
|
552 |
newp.import_limit, newp.export_limit] |
|
553 |
if any(map(_isneg, new_values)): |
|
554 |
append((entity, resource)) |
|
555 |
continue |
|
536 |
try: |
|
537 |
h = db_get_holding(entity=entity, resource=resource, |
|
538 |
for_update=True) |
|
539 |
p = h.policy |
|
540 |
except Holding.DoesNotExist: |
|
541 |
if removing: |
|
542 |
append((entity, resource)) |
|
543 |
continue |
|
544 |
h = Holding(entity=e, resource=resource, flags=0) |
|
545 |
p = None |
|
546 |
|
|
547 |
policy = newname('policy_') |
|
548 |
newp = Policy(policy=policy) |
|
549 |
|
|
550 |
newp.quantity = _add(p.quantity if p else 0, quantity, |
|
551 |
invert=removing) |
|
552 |
newp.capacity = _add(p.capacity if p else 0, capacity, |
|
553 |
invert=removing) |
|
554 |
newp.import_limit = _add(p.import_limit if p else 0, |
|
555 |
import_limit, invert=removing) |
|
556 |
newp.export_limit = _add(p.export_limit if p else 0, |
|
557 |
export_limit, invert=removing) |
|
558 |
|
|
559 |
new_values = [newp.capacity, |
|
560 |
newp.import_limit, newp.export_limit] |
|
561 |
if any(map(_isneg, new_values)): |
|
562 |
append((entity, resource)) |
|
563 |
continue |
|
556 | 564 |
|
557 |
h.policy = newp |
|
565 |
h.policy = newp
|
|
558 | 566 |
|
559 |
# the order is intentionally reversed so that it |
|
560 |
# would break if we are not within a transaction. |
|
561 |
# Has helped before. |
|
562 |
h.save() |
|
563 |
newp.save() |
|
567 |
# the order is intentionally reversed so that it
|
|
568 |
# would break if we are not within a transaction.
|
|
569 |
# Has helped before.
|
|
570 |
h.save()
|
|
571 |
newp.save()
|
|
564 | 572 |
|
565 |
if p is not None and p.holding_set.count() == 0: |
|
566 |
p.delete() |
|
573 |
if p is not None and p.holding_set.count() == 0:
|
|
574 |
p.delete()
|
|
567 | 575 |
|
568 | 576 |
if rejected: |
569 | 577 |
raise ReturnButFail(rejected) |
... | ... | |
964 | 972 |
|
965 | 973 |
return timeline |
966 | 974 |
|
967 |
def _add(x, y): |
|
975 |
def _add(x, y, invert=False): |
|
976 |
if invert and y is None: |
|
977 |
return 0 |
|
968 | 978 |
if x is None or y is None: |
969 | 979 |
return None |
970 |
return x + y |
|
980 |
return x + y if not invert else x - y
|
|
971 | 981 |
|
972 | 982 |
def _update(dest, source, attr, delta): |
973 | 983 |
dest_attr = getattr(dest, attr) |
Also available in: Unified diff