Revision 7b7f9205

b/db/models.py
2 2

  
3 3
from django.conf import settings
4 4
from django.db import models
5
from django.db import transaction
5

  
6
from logic.credits import debit_account
6 7

  
7 8
import datetime
8 9

  
......
51 52
        
52 53
    min_credits = property(_get_min_credits)
53 54

  
54
    @transaction.commit_on_success
55
    def debit_account(self, amount, vm, description):
56
        """Charges the user with the specified amount of credits for a vm (resource)"""
57
        date_now = datetime.datetime.now()
58
        self.credit = self.credit - amount
59
        self.save()
60

  
61
        # then write the debit entry
62
        debit = Debit()
63

  
64
        debit.user = self
65
        debit.vm = vm
66
        debit.when = date_now
67
        debit.description = description
68

  
69
        debit.save()
70

  
71
    def credit_account(self, amount, creditor, description):
72
        """No clue :)"""
73
        return
74

  
75 55

  
76 56
class Image(models.Model):
77 57
    # This is WIP, FIXME
......
506 486

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

  
b/logic/credits.py
3 3
#
4 4
# Copyright 2010 Greek Research and Technology Network
5 5
#
6
from datetime import datetime
6 7

  
8
from db.models import Debit
9
from django.db import transaction
10

  
11
@transaction.commit_on_success
12
def debit_account(user , amount, vm, description):
13
    """Charges the user with the specified amount of credits for a vm (resource)"""
14
    date_now = datetime.datetime.now()
15
    user.credit = user.credit - amount
16
    user.save()
17

  
18
    # then write the debit entry
19
    debit = Debit()
20
    debit.user = user
21
    debit.vm = vm
22
    debit.when = date_now
23
    debit.description = description
24
    debit.save()
25

  
26

  
27
@transaction.commit_on_success
28
def credit_account(self, amount, creditor, description):
29
    """No clue :)"""
30
    return

Also available in: Unified diff