Revision 92c53da1 db/models.py

b/db/models.py
3 3
from django.conf import settings
4 4
from django.db import models
5 5

  
6
from logic.credits import debit_account
7

  
8 6
import datetime
9 7

  
10
backend_prefix_id = settings.BACKEND_PREFIX_ID
11 8

  
12 9
class SynnefoUser(models.Model):
13 10
    name = models.CharField('Synnefo Username', max_length=255)
14 11
    credit = models.IntegerField('Credit Balance')
15 12
    created = models.DateTimeField('Time of creation', auto_now_add=True)
16 13
    updated = models.DateTimeField('Time of last update', auto_now=True)
17
    #
18
    # We do not rely on Django's user authentication mechanism.
19
    # Hence, no references to the User model.
20
    # [vkoukis], after discussion with [faidon].
21
    # user = models.ForeignKey(User)
22
    
14

  
23 15
    class Meta:
24 16
        verbose_name = u'Synnefo User'
25 17
    
......
133 125
                return fch_list[0].cost_inactive
134 126

  
135 127
        return 0
136
        
137 128

  
138 129
    def _current_cost_active(self):
139 130
        """Returns current active cost (property method)"""
......
344 335
        Strips the ganeti prefix atm. Needs a better name!
345 336
        
346 337
        """
347
        if not str(name).startswith(backend_prefix_id):
338
        if not str(name).startswith(settings.BACKEND_PREFIX_ID):
348 339
            raise VirtualMachine.InvalidBackendIdError(str(name))
349
        ns = str(name).lstrip(backend_prefix_id)
340
        ns = str(name).lstrip(settings.BACKEND_PREFIX_ID)
350 341
        if not ns.isdigit():
351 342
            raise VirtualMachine.InvalidBackendIdError(str(name))
352 343
        return int(ns)
......
434 425

  
435 426
    def _get_backend_id(self):
436 427
        """Returns the backend id for this VM by prepending backend-prefix."""
437
        return '%s%s' % (backend_prefix_id, str(self.id))
428
        return '%s%s' % (settings.BACKEND_PREFIX_ID, str(self.id))
438 429

  
439 430
    backend_id = property(_get_backend_id)
440 431

  
......
457 448
        self.charge()
458 449
        self._operstate = new_operstate
459 450

  
460
    @transaction.commit_on_success
461
    def charge(self):
462
        """Charges the owner of this VM.
463
        
464
        Charges the owner of a VM for the period
465
        from vm.charged to datetime.now(), based on the
466
        current operating state.
467
        
468
        """
469
        charged_states = ('STARTED', 'STOPPED')
470
       
471
        start_datetime = self.charged
472
        self.charged = datetime.datetime.now()
473

  
474
        # Only charge for a specific set of states
475
        if self._operstate in charged_states:
476
            cost_list = []
477

  
478
            # remember, we charge only for Started and Stopped
479
            if self._operstate == 'STARTED':
480
                cost_list = self.flavor.get_cost_active(start_datetime, self.charged)
481
            elif self._operstate == 'STOPPED':
482
                cost_list = self.flavor.get_cost_inactive(start_datetime, self.charged)
483

  
484
            # find the total vost
485
            total_cost = sum([x[1] for x in cost_list])
486

  
487
            # add the debit entry
488
            description = "Server = %s, charge = %d for state: %s" % (self.name, total_cost, self._operstate)
489
            debit_account(self.owner, total_cost, self, description)
490
        
491
        self.save()
492

  
493 451

  
494 452
class VirtualMachineGroup(models.Model):
495 453
    """Groups of VMs for SynnefoUsers"""

Also available in: Unified diff