k4ever/k4ever/transaction/forms.py

45 lines
1.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2012-01-21 00:21:29 +01:00
# This file is part of k4ever, a point-of-sale system
# Contact............ <k4ever@lists.someserver.de>
# Website............ http://k4ever.someserver.de/
# Bug tracker........ http://k4ever.someserver.de/report
#
# Licensed under GNU Affero General Public License v3 or later
2010-10-16 17:33:18 +02:00
from django import forms
from models import Transaction, VirtualTransaction
2010-10-16 17:33:18 +02:00
from main.fields import CurrencyField
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
2010-10-16 17:33:18 +02:00
class TransactionForm(forms.ModelForm):
""" ModelForm for :class:`Transactions <Transaction>` with a currency field. """
2011-10-02 17:58:54 +02:00
amount = CurrencyField(label='Betrag')
2010-10-16 17:33:18 +02:00
class Meta:
model = Transaction
exclude = ('user', 'dateTime', 'checked')
def clean(self):
# needed to enforce TransactionTypes needsCheck "default value"
cleaned_data = super(TransactionForm, self).clean()
if cleaned_data.has_key("transactionType"):
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'])
if user == self.instance.user:
raise ValidationError(u"Emfpänger '%s' bist du selbst" % self.cleaned_data['recipient'])
return user