Cleanup of VirtualTransaction
This commit is contained in:
parent
ccd9e327c7
commit
5f0c0c14fc
|
@ -11,8 +11,6 @@ class BuyableAdminForm(forms.ModelForm):
|
||||||
|
|
||||||
def clean_image(self):
|
def clean_image(self):
|
||||||
img = self.cleaned_data['image']
|
img = self.cleaned_data['image']
|
||||||
print img.file
|
|
||||||
print dir(img)
|
|
||||||
width, height = (0, 0)
|
width, height = (0, 0)
|
||||||
if isinstance(img, InMemoryUploadedFile):
|
if isinstance(img, InMemoryUploadedFile):
|
||||||
i = Image.open(img)
|
i = Image.open(img)
|
||||||
|
|
|
@ -56,34 +56,44 @@ class VirtualTransaction(models.Model):
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return u"%s ==> %s: %s Euro" % (self.user, self.recipient, self.amount)
|
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):
|
def save(self, *args, **kwargs):
|
||||||
userProfile = self.user.get_profile()
|
if self.user and self.recipient:
|
||||||
recipientProfile = self.recipient.get_profile()
|
oldobj = None
|
||||||
amount = None
|
try:
|
||||||
if self.id == None:
|
oldobj = VirtualTransaction.objects.get(id=self.id)
|
||||||
amount = self.amount
|
except VirtualTransaction.DoesNotExist:
|
||||||
else:
|
pass
|
||||||
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 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
|
@staticmethod
|
||||||
def pre_delete_signal(sender, instance, **kwargs):
|
def pre_delete_signal(sender, instance, **kwargs):
|
||||||
""" Pre delete signal to ensure consistent balances on object delete. """
|
""" Pre delete signal to ensure consistent balances on object delete. """
|
||||||
# Only revert if both users exist.
|
# Only revert if both users exist.
|
||||||
if instance.user and instance.recipient:
|
if instance.user and instance.recipient:
|
||||||
# revert transaction
|
# revert transaction
|
||||||
print instance.user
|
VirtualTransaction.moveMoney(instance.recipient, instance.user, amount)
|
||||||
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()
|
|
||||||
pre_delete.connect(VirtualTransaction.pre_delete_signal, sender=VirtualTransaction)
|
pre_delete.connect(VirtualTransaction.pre_delete_signal, sender=VirtualTransaction)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue