Cleaned up Transaction save/delete

This commit is contained in:
Sebastian Lohff 2011-10-31 17:22:21 +01:00
parent c2d144ea91
commit 9f55a64923
2 changed files with 18 additions and 21 deletions

View File

@ -4,28 +4,13 @@ from django.contrib import admin
class TransactionAdmin(admin.ModelAdmin):
list_filter = ('transactionType','checked','user')
actions = ['really_delete_selected']
#actions = ['really_delete_selected']
list_display = ('user', 'transactionType', 'amount', 'dateTime', 'checked')
list_display_links = ('user', 'transactionType')
search_fields = ['user__username','user__first_name','user__last_name']
date_hierarchy = 'dateTime'
ordering = ['-dateTime']
def get_actions(self, request):
actions = super(TransactionAdmin, self).get_actions(request)
del actions['delete_selected']
return actions
# FIXME: Can we instead of replacing the whole page just
# hook the release process? Also - make this nicer
# in terms of localization/naming foo
def really_delete_selected(self, request, queryset):
for obj in queryset:
obj.delete()
num = queryset.count()
message = "%s Transaktion%s" % (num, (num != 1 and "en" or ""))
self.message_user(request, u"Erfolgreich %s gelöscht." % message)
really_delete_selected.short_description = u"Ausgewählte Transaktion(en) löschen"
class VirtualTransactionAdmin(admin.ModelAdmin):
list_display = ('user','recipient','amount','dateTime','comment')
list_display_links = ('user','recipient')

View File

@ -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):
else:
# update
oldobj = Transaction.objects.get(id=self.id)
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. """