Cleanup of VirtualTransaction

This commit is contained in:
Sebastian Lohff 2011-10-25 19:03:51 +02:00
parent ccd9e327c7
commit 5f0c0c14fc
2 changed files with 31 additions and 23 deletions

View File

@ -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)

View File

@ -56,20 +56,36 @@ 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):
@ -77,13 +93,7 @@ class VirtualTransaction(models.Model):
# 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)