Revision 111b2cda

b/db/models.py
142 142
    def __unicode__(self):
143 143
        return self.name
144 144

  
145
    def _get_costs(self, start_datetime, end_datetime, active):
146
        """Return a list with FlavorCost objects for the specified duration"""
147
        def between(enh_fc, a_date):
148
            """Checks if a date is between a FlavorCost duration"""
149
            if enh_fc.effective_from <= a_date and enh_fc.effective_to is None:
150
                return True
151

  
152
            return enh_fc.effective_from <= a_date and enh_fc.effective_to >= a_date
153

  
154
        # Get the related FlavorCost objects, sorted.
155
        price_list = FlavorCost.objects.filter(flavor=self).order_by('effective_from')
156

  
157
        # add the extra field FlavorCost.effective_to
158
        for idx in range(0, len(price_list)):
159
            if idx + 1 == len(price_list):
160
                price_list[idx].effective_to = None
161
            else:
162
                price_list[idx].effective_to = price_list[idx + 1].effective_from
163

  
164
        price_result = []
165
        found_start = False
166

  
167
        # Find the affected FlavorCost, according to the
168
        # dates, and put them in price_result
169
        for p in price_list:
170
            if between(p, start_datetime):
171
                found_start = True
172
                p.effective_from = start_datetime
173
            if between(p, end_datetime):
174
                p.effective_to = end_datetime
175
                price_result.append(p)
176
                break
177
            if found_start:
178
                price_result.append(p)
179

  
180
        results = []
181

  
182
        # Create the list and the result tuples
183
        for p in price_result:
184
            if active:
185
                cost = p.cost_active
186
            else:
187
                cost = p.cost_inactive
188

  
189
            results.append( ( p.effective_from, utils.calculate_cost(p.effective_from, p.effective_to, cost)) )
190

  
191
        return results
192

  
193 145
    def get_cost_active(self, start_datetime, end_datetime):
194 146
        """Returns a list with the active costs for the specified duration"""
195
        return self._get_costs(start_datetime, end_datetime, True)
147
        return credits.get_costs(self, start_datetime, end_datetime, True)
196 148

  
197 149
    def get_cost_inactive(self, start_datetime, end_datetime):
198 150
        """Returns a list with the inactive costs for the specified duration"""
199
        return self._get_costs(start_datetime, end_datetime, False)
151
        return credits.get_costs(self, start_datetime, end_datetime, False)
200 152

  
201 153

  
202 154
class FlavorCost(models.Model):
......
209 161
        verbose_name = u'Pricing history for flavors'
210 162
    
211 163
    def __unicode__(self):
212
        return u'Costs (up, down)=(%d, %d) for %s since %s' % (self.cost_active, self.cost_inactive, flavor.name, self.effective_from)
164
        return u'Costs (up, down)=(%d, %d) for %s since %s' % (self.cost_active, self.cost_inactive, self.flavor.name, self.effective_from)
213 165

  
214 166

  
215 167
class VirtualMachine(models.Model):
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