diff --git a/k4ever/transaction/admin.py b/k4ever/transaction/admin.py index 4de287f..6142617 100644 --- a/k4ever/transaction/admin.py +++ b/k4ever/transaction/admin.py @@ -20,6 +20,7 @@ class TransactionAdmin(admin.ModelAdmin): date_hierarchy = 'dateTime' ordering = ['-dateTime'] + class VirtualTransactionAdmin(admin.ModelAdmin): list_display = ('user','recipient','amount','dateTime','comment') list_display_links = ('user','recipient') @@ -28,6 +29,7 @@ class VirtualTransactionAdmin(admin.ModelAdmin): date_hierarchy = 'dateTime' ordering = ['-dateTime'] -admin.site.register(Transaction, TransactionAdmin) + admin.site.register(TransactionType) +admin.site.register(Transaction, TransactionAdmin) admin.site.register(VirtualTransaction, VirtualTransactionAdmin) diff --git a/k4ever/transaction/forms.py b/k4ever/transaction/forms.py index 3405b9d..734b78b 100644 --- a/k4ever/transaction/forms.py +++ b/k4ever/transaction/forms.py @@ -17,9 +17,11 @@ from transaction.models import Transaction, VirtualTransaction class TransactionForm(forms.ModelForm): """ ModelForm for :class:`Transactions ` with a currency field. """ amount = CurrencyField(label='Betrag') + class Meta: model = Transaction exclude = ('user', 'dateTime', 'checked') + def clean(self): # needed to enforce TransactionTypes needsCheck "default value" cleaned_data = super(TransactionForm, self).clean() @@ -27,11 +29,13 @@ class TransactionForm(forms.ModelForm): self.instance.checked = not cleaned_data['transactionType'].needsCheck return cleaned_data + class VirtualTransactionForm(forms.ModelForm): """ ModelForm for :class:`Virtual Transactions ` with a currency field. """ recipient = forms.CharField(max_length=100) amount = CurrencyField(label='Betrag') + class Meta: model = VirtualTransaction exclude = ('user', 'dateTime') @@ -40,7 +44,10 @@ class VirtualTransactionForm(forms.ModelForm): try: user = User.objects.get(username=self.cleaned_data['recipient']) except User.DoesNotExist: - raise ValidationError(u"Emfpänger '%s' konnte nicht gefunden werden" % self.cleaned_data['recipient']) + raise ValidationError(u"Emfpänger '%s' konnte nicht gefunden werden"\ + % self.cleaned_data['recipient']) + if user == self.instance.user: - raise ValidationError(u"Emfpänger '%s' bist du selbst" % self.cleaned_data['recipient']) + raise ValidationError(u"Emfpänger '%s' bist du selbst"\ + % self.cleaned_data['recipient']) return user diff --git a/k4ever/transaction/models.py b/k4ever/transaction/models.py index 72e1150..2e1135b 100644 --- a/k4ever/transaction/models.py +++ b/k4ever/transaction/models.py @@ -27,16 +27,20 @@ class TransactionType(models.Model): def __unicode__(self): return unicode(self.name) + class Transaction(models.Model): """Represents a money transaction into the users account. """ user = models.ForeignKey(User) transactionType = models.ForeignKey(TransactionType, verbose_name='Typ') dateTime = models.DateTimeField(auto_now_add=True) - amount = models.DecimalField(max_digits=8, decimal_places=2, validators=[validate_notZero]) + amount = models.DecimalField(max_digits=8, decimal_places=2, + validators=[validate_notZero]) checked = models.BooleanField(default=False) def __unicode__(self): - return u"%s for user %s (%s),%schecked" % (self.amount, self.user, self.transactionType, (self.checked and " " or " not ")) + return u"%s for user %s (%s),%schecked" % ( + self.amount, self.user, self.transactionType, + (self.checked and " " or " not ")) def save(self, *args, **kwargs): profile = self.user.get_profile() @@ -63,15 +67,18 @@ class Transaction(models.Model): profile = instance.user.get_profile() profile.balance -= instance.amount profile.save() - + pre_delete.connect(Transaction.pre_delete_signal, sender=Transaction) + class VirtualTransaction(models.Model): """ Represents a transaction between two users. """ user = models.ForeignKey(User, null=True, on_delete=SET_NULL) - recipient = models.ForeignKey(User, related_name='receivedtransaction', null=True, on_delete=SET_NULL, verbose_name=u'Empfänger') + recipient = models.ForeignKey(User, related_name='receivedtransaction', + null=True, on_delete=SET_NULL, verbose_name=u'Empfänger') dateTime = models.DateTimeField(auto_now_add=True) - amount = models.DecimalField(max_digits=8, decimal_places=2, validators=[MinValueValidator(Decimal("0.01"))], verbose_name='Betrag') + amount = models.DecimalField(max_digits=8, decimal_places=2, + validators=[MinValueValidator(Decimal("0.01"))], verbose_name='Betrag') comment = models.CharField(max_length=100, verbose_name='Verwendungszweck') def __unicode__(self): @@ -118,4 +125,3 @@ class VirtualTransaction(models.Model): VirtualTransaction.moveMoney(instance.recipient, instance.user, amount) pre_delete.connect(VirtualTransaction.pre_delete_signal, sender=VirtualTransaction) - diff --git a/k4ever/transaction/views.py b/k4ever/transaction/views.py index 8166347..b233753 100644 --- a/k4ever/transaction/views.py +++ b/k4ever/transaction/views.py @@ -21,6 +21,7 @@ def overview(request): # create history history = Transaction.objects.filter(user=request.user).order_by("-dateTime") vhistory = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime") + # create forms form = TransactionForm() vform = VirtualTransactionForm()