Revision 22e52ede db/models.py

b/db/models.py
151 151

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

  
154
        def calculate_cost(start_date, end_date, cost):
155
            """Calculate the total cost for the specified duration"""
156
            td = end_date - start_date
157
            sec = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
158
            total_hours = float(sec) / float(60.0*60.0)
159
            total_cost = float(cost)*total_hours
160

  
161
            return round(total_cost)
162

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

  
......
195 186
            else:
196 187
                cost = p.cost_inactive
197 188

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

  
200 191
        return results
201 192

  
......
345 336
            # breaking VirtualMachine.object.create() among other things.
346 337
            self._operstate = 'BUILD'
347 338

  
348
    def start_action(self, action):
349
        """Update the state of a VM when a new action is initiated."""
350
        if not action in [x[0] for x in VirtualMachine.ACTIONS]:
351
            raise VirtualMachine.InvalidActionError(action)
352

  
353
        # No actions to deleted and no actions beside destroy to suspended VMs
354
        if self.deleted:
355
            raise VirtualMachine.InvalidActionError(action)
356

  
357
        self._action = action
358
        self._backendjobid = None
359
        self._backendopcode = None
360
        self._backendjobstatus = None
361
        self._backendlogmsg = None
362

  
363
        # Update the relevant flags if the VM is being suspended or destroyed
364
        if action == "DESTROY":
365
            self.deleted = True
366
        elif action == "SUSPEND":
367
            self.suspended = True
368
        elif action == "START":
369
            self.suspended = False
370
        self.save()
371

  
372 339
    # FIXME: leave this here to preserve the property rsapistate
373 340
    def _get_rsapi_state(self):
374 341
        return utils.get_rsapi_state(self)
......
397 364

  
398 365
        # Call charge() unconditionally before any change of
399 366
        # internal state.
400
        credits.charge()
367
        credits.charge(self)
401 368
        self._operstate = new_operstate
402 369

  
403 370

  

Also available in: Unified diff