Revision 111b2cda logic/credits.py
b/logic/credits.py | ||
---|---|---|
6 | 6 |
|
7 | 7 |
from datetime import datetime |
8 | 8 |
|
9 |
from db.models import Debit |
|
9 |
from db.models import Debit, FlavorCost
|
|
10 | 10 |
from django.db import transaction |
11 |
import utils |
|
11 | 12 |
|
12 | 13 |
@transaction.commit_on_success |
13 | 14 |
def debit_account(user , amount, vm, description): |
... | ... | |
63 | 64 |
debit_account(vm.owner, total_cost, vm, description) |
64 | 65 |
|
65 | 66 |
vm.save() |
67 |
|
|
68 |
def get_costs(vm, start_datetime, end_datetime, active): |
|
69 |
"""Return a list with FlavorCost objects for the specified duration""" |
|
70 |
def between(enh_fc, a_date): |
|
71 |
"""Checks if a date is between a FlavorCost duration""" |
|
72 |
if enh_fc.effective_from <= a_date and enh_fc.effective_to is None: |
|
73 |
return True |
|
74 |
|
|
75 |
return enh_fc.effective_from <= a_date and enh_fc.effective_to >= a_date |
|
76 |
|
|
77 |
# Get the related FlavorCost objects, sorted. |
|
78 |
price_list = FlavorCost.objects.filter(flavor=vm).order_by('effective_from') |
|
79 |
|
|
80 |
# add the extra field FlavorCost.effective_to |
|
81 |
for idx in range(0, len(price_list)): |
|
82 |
if idx + 1 == len(price_list): |
|
83 |
price_list[idx].effective_to = None |
|
84 |
else: |
|
85 |
price_list[idx].effective_to = price_list[idx + 1].effective_from |
|
86 |
|
|
87 |
price_result = [] |
|
88 |
found_start = False |
|
89 |
|
|
90 |
# Find the affected FlavorCost, according to the |
|
91 |
# dates, and put them in price_result |
|
92 |
for p in price_list: |
|
93 |
if between(p, start_datetime): |
|
94 |
found_start = True |
|
95 |
p.effective_from = start_datetime |
|
96 |
if between(p, end_datetime): |
|
97 |
p.effective_to = end_datetime |
|
98 |
price_result.append(p) |
|
99 |
break |
|
100 |
if found_start: |
|
101 |
price_result.append(p) |
|
102 |
|
|
103 |
results = [] |
|
104 |
|
|
105 |
# Create the list and the result tuples |
|
106 |
for p in price_result: |
|
107 |
if active: |
|
108 |
cost = p.cost_active |
|
109 |
else: |
|
110 |
cost = p.cost_inactive |
|
111 |
|
|
112 |
results.append( ( p.effective_from, utils.calculate_cost(p.effective_from, p.effective_to, cost)) ) |
|
113 |
|
|
114 |
return results |
Also available in: Unified diff