Browse Source

Cleanup of VirtualTransaction

master
Sebastian Lohff 11 years ago
parent
commit
5f0c0c14fc
  1. 2
      k4ever/buyable/admin.py
  2. 52
      k4ever/transaction/models.py

2
k4ever/buyable/admin.py

@ -11,8 +11,6 @@ class BuyableAdminForm(forms.ModelForm): @@ -11,8 +11,6 @@ class BuyableAdminForm(forms.ModelForm):
def clean_image(self):
img = self.cleaned_data['image']
print img.file
print dir(img)
width, height = (0, 0)
if isinstance(img, InMemoryUploadedFile):
i = Image.open(img)

52
k4ever/transaction/models.py

@ -56,34 +56,44 @@ class VirtualTransaction(models.Model): @@ -56,34 +56,44 @@ class VirtualTransaction(models.Model):
def __unicode__(self):
return u"%s ==> %s: %s Euro" % (self.user, self.recipient, self.amount)
@staticmethod
def moveMoney(fromUser, toUser, amount, commit=True):
fromProfile = fromUser.get_profile()
toProfile = toUser.get_profile()
fromProfile.balance -= amount
toProfile.balance += amount
if commit:
fromProfile.save()
toProfile.save()
def save(self, *args, **kwargs):
userProfile = self.user.get_profile()
recipientProfile = self.recipient.get_profile()
amount = None
if self.id == None:
amount = self.amount
else:
oldobj = VirtualTransaction.objects.get(id=self.id)
amount = self.amount - oldobj.amount
userProfile.balance -= amount
recipientProfile.balance += amount
userProfile.save()
recipientProfile.save()
super(VirtualTransaction, self).save(*args, **kwargs)
if self.user and self.recipient:
oldobj = None
try:
oldobj = VirtualTransaction.objects.get(id=self.id)
except VirtualTransaction.DoesNotExist:
pass
if oldobj and (oldobj.user != self.user or oldobj.recipient != self.recipient):
VirtualTransaction.moveMoney(oldobj.recipient, oldobj.user, oldobj.amount)
VirtualTransaction.moveMoney(self.user, self.recipient, self.amount)
else:
tmpAmount = None
if oldobj == None:
tmpAmount = self.amount
else:
tmpAmount = self.amount - oldobj.amount
VirtualTransaction.moveMoney(self.user, self.recipient, tmpAmount)
super(VirtualTransaction, self).save(*args, **kwargs)
@staticmethod
def pre_delete_signal(sender, instance, **kwargs):
""" Pre delete signal to ensure consistent balances on object delete. """
# Only revert if both users exist.
if instance.user and instance.recipient:
# revert transaction
print instance.user
print dir(instance)
userProfile = instance.user.get_profile()
recipientProfile = instance.recipient.get_profile()
userProfile.balance += instance.amount
recipientProfile.balance -= instance.amount
userProfile.save()
recipientProfile.save()
VirtualTransaction.moveMoney(instance.recipient, instance.user, amount)
pre_delete.connect(VirtualTransaction.pre_delete_signal, sender=VirtualTransaction)

Loading…
Cancel
Save