k4ever/k4ever/transaction/models.py

39 lines
1.2 KiB
Python

from django.db import models
from django.contrib.auth.models import User
class TransactionType(models.Model):
name = models.CharField(max_length=100)
needsCheck = models.BooleanField(default=True)
def __unicode__(self):
return unicode(self.name)
class Transaction(models.Model):
user = models.ForeignKey(User)
transactionType = models.ForeignKey(TransactionType)
dateTime = models.DateTimeField()
amount = models.DecimalField(max_digits=8, decimal_places=2)
checked = models.BooleanField(default=False)
def __unicode__(self):
return u"%s for user %s (%s),%schecked" % (self.amount, self.user, self.transactionType, (self.checked and " " or " not "))
# TODO: Find out what would happen if parent save/delete does not like us
def save(self, *args, **kwargs):
profile = self.user.get_profile()
if self.id == None:
# insert
profile.balance += self.amount
else:
# update
oldobj = Transaction.objects.get(id=self.id)
profile.balance += (self.amount-oldobj.amount)
profile.save()
super(Transaction, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
profile = self.user.get_profile()
profile.balance -= self.amount
profile.save()
super(Transaction, self).delete(*args, **kwargs)