some pep8 cleanups for readability

This commit is contained in:
MasterofJOKers 2012-03-11 02:03:13 +01:00
parent a059e69dff
commit b6f99b07ec
4 changed files with 25 additions and 9 deletions

View File

@ -20,6 +20,7 @@ class TransactionAdmin(admin.ModelAdmin):
date_hierarchy = 'dateTime' date_hierarchy = 'dateTime'
ordering = ['-dateTime'] ordering = ['-dateTime']
class VirtualTransactionAdmin(admin.ModelAdmin): class VirtualTransactionAdmin(admin.ModelAdmin):
list_display = ('user','recipient','amount','dateTime','comment') list_display = ('user','recipient','amount','dateTime','comment')
list_display_links = ('user','recipient') list_display_links = ('user','recipient')
@ -28,6 +29,7 @@ class VirtualTransactionAdmin(admin.ModelAdmin):
date_hierarchy = 'dateTime' date_hierarchy = 'dateTime'
ordering = ['-dateTime'] ordering = ['-dateTime']
admin.site.register(Transaction, TransactionAdmin)
admin.site.register(TransactionType) admin.site.register(TransactionType)
admin.site.register(Transaction, TransactionAdmin)
admin.site.register(VirtualTransaction, VirtualTransactionAdmin) admin.site.register(VirtualTransaction, VirtualTransactionAdmin)

View File

@ -17,9 +17,11 @@ from transaction.models import Transaction, VirtualTransaction
class TransactionForm(forms.ModelForm): class TransactionForm(forms.ModelForm):
""" ModelForm for :class:`Transactions <Transaction>` with a currency field. """ """ ModelForm for :class:`Transactions <Transaction>` with a currency field. """
amount = CurrencyField(label='Betrag') amount = CurrencyField(label='Betrag')
class Meta: class Meta:
model = Transaction model = Transaction
exclude = ('user', 'dateTime', 'checked') exclude = ('user', 'dateTime', 'checked')
def clean(self): def clean(self):
# needed to enforce TransactionTypes needsCheck "default value" # needed to enforce TransactionTypes needsCheck "default value"
cleaned_data = super(TransactionForm, self).clean() cleaned_data = super(TransactionForm, self).clean()
@ -27,11 +29,13 @@ class TransactionForm(forms.ModelForm):
self.instance.checked = not cleaned_data['transactionType'].needsCheck self.instance.checked = not cleaned_data['transactionType'].needsCheck
return cleaned_data return cleaned_data
class VirtualTransactionForm(forms.ModelForm): class VirtualTransactionForm(forms.ModelForm):
""" ModelForm for :class:`Virtual Transactions <VirtualTransaction>` with a """ ModelForm for :class:`Virtual Transactions <VirtualTransaction>` with a
currency field. """ currency field. """
recipient = forms.CharField(max_length=100) recipient = forms.CharField(max_length=100)
amount = CurrencyField(label='Betrag') amount = CurrencyField(label='Betrag')
class Meta: class Meta:
model = VirtualTransaction model = VirtualTransaction
exclude = ('user', 'dateTime') exclude = ('user', 'dateTime')
@ -40,7 +44,10 @@ class VirtualTransactionForm(forms.ModelForm):
try: try:
user = User.objects.get(username=self.cleaned_data['recipient']) user = User.objects.get(username=self.cleaned_data['recipient'])
except User.DoesNotExist: 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: 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 return user

View File

@ -27,16 +27,20 @@ class TransactionType(models.Model):
def __unicode__(self): def __unicode__(self):
return unicode(self.name) return unicode(self.name)
class Transaction(models.Model): class Transaction(models.Model):
"""Represents a money transaction into the users account. """ """Represents a money transaction into the users account. """
user = models.ForeignKey(User) user = models.ForeignKey(User)
transactionType = models.ForeignKey(TransactionType, verbose_name='Typ') transactionType = models.ForeignKey(TransactionType, verbose_name='Typ')
dateTime = models.DateTimeField(auto_now_add=True) 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) checked = models.BooleanField(default=False)
def __unicode__(self): 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): def save(self, *args, **kwargs):
profile = self.user.get_profile() profile = self.user.get_profile()
@ -63,15 +67,18 @@ class Transaction(models.Model):
profile = instance.user.get_profile() profile = instance.user.get_profile()
profile.balance -= instance.amount profile.balance -= instance.amount
profile.save() profile.save()
pre_delete.connect(Transaction.pre_delete_signal, sender=Transaction) pre_delete.connect(Transaction.pre_delete_signal, sender=Transaction)
class VirtualTransaction(models.Model): class VirtualTransaction(models.Model):
""" Represents a transaction between two users. """ """ Represents a transaction between two users. """
user = models.ForeignKey(User, null=True, on_delete=SET_NULL) 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) 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') comment = models.CharField(max_length=100, verbose_name='Verwendungszweck')
def __unicode__(self): def __unicode__(self):
@ -118,4 +125,3 @@ class VirtualTransaction(models.Model):
VirtualTransaction.moveMoney(instance.recipient, instance.user, amount) VirtualTransaction.moveMoney(instance.recipient, instance.user, amount)
pre_delete.connect(VirtualTransaction.pre_delete_signal, sender=VirtualTransaction) pre_delete.connect(VirtualTransaction.pre_delete_signal, sender=VirtualTransaction)

View File

@ -21,6 +21,7 @@ def overview(request):
# create history # create history
history = Transaction.objects.filter(user=request.user).order_by("-dateTime") 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") vhistory = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")
# create forms # create forms
form = TransactionForm() form = TransactionForm()
vform = VirtualTransactionForm() vform = VirtualTransactionForm()