|
|
|
@ -29,6 +29,7 @@ class Transaction(models.Model):
@@ -29,6 +29,7 @@ class Transaction(models.Model):
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
@ -38,14 +39,25 @@ class Transaction(models.Model):
@@ -38,14 +39,25 @@ class Transaction(models.Model):
|
|
|
|
|
else: |
|
|
|
|
# update |
|
|
|
|
oldobj = Transaction.objects.get(id=self.id) |
|
|
|
|
profile.balance += (self.amount-oldobj.amount) |
|
|
|
|
if oldobj.user != self.user: |
|
|
|
|
oldprofile = oldobj.user.get_profile() |
|
|
|
|
oldprofile.balance -= oldobj.amount |
|
|
|
|
oldprofile.save() |
|
|
|
|
# just to be save, reget profile |
|
|
|
|
profile = self.user.get_profile() |
|
|
|
|
profile.balance += self.amount |
|
|
|
|
else: |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def pre_delete_signal(sender, instance, **kwargs): |
|
|
|
|
profile = instance.user.get_profile() |
|
|
|
|
profile.balance -= instance.amount |
|
|
|
|
profile.save() |
|
|
|
|
super(Transaction, self).delete(*args, **kwargs) |
|
|
|
|
|
|
|
|
|
pre_delete.connect(Transaction.pre_delete_signal, sender=Transaction) |
|
|
|
|
|
|
|
|
|
class VirtualTransaction(models.Model): |
|
|
|
|
""" Represents a transaction between two users. """ |
|
|
|
|