You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k4ever/k4ever/transaction/forms.py

40 lines
1.3 KiB

# -*- coding: utf-8 -*-
from django import forms
from models import Transaction, VirtualTransaction
from main.fields import CurrencyField
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
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_amount(self):
data = self.cleaned_data['amount']
return data
def clean(self):
# needed to enforce TransactionTypes needsCheck "default value"
cleaned_data = super(TransactionForm, self).clean()
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')
def clean_recipient(self):
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'])
return user