some pep8 cleanups for readability
This commit is contained in:
parent
a059e69dff
commit
b6f99b07ec
|
@ -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)
|
||||
|
|
|
@ -17,9 +17,11 @@ from transaction.models import Transaction, VirtualTransaction
|
|||
class TransactionForm(forms.ModelForm):
|
||||
""" ModelForm for :class:`Transactions <Transaction>` 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 <VirtualTransaction>` 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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue