No Description
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.

forms.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. # This file is part of k4ever, a point-of-sale system
  3. # Contact............ <k4ever@lists.someserver.de>
  4. # Website............ http://k4ever.someserver.de/
  5. # Bug tracker........ http://k4ever.someserver.de/report
  6. #
  7. # Licensed under GNU Affero General Public License v3 or later
  8. from django import forms
  9. from models import Transaction, VirtualTransaction
  10. from main.fields import CurrencyField
  11. from django.contrib.auth.models import User
  12. from django.core.exceptions import ValidationError
  13. class TransactionForm(forms.ModelForm):
  14. """ ModelForm for :class:`Transactions <Transaction>` with a currency field. """
  15. amount = CurrencyField(label='Betrag')
  16. class Meta:
  17. model = Transaction
  18. exclude = ('user', 'dateTime', 'checked')
  19. def clean_amount(self):
  20. data = self.cleaned_data['amount']
  21. return data
  22. def clean(self):
  23. # needed to enforce TransactionTypes needsCheck "default value"
  24. cleaned_data = super(TransactionForm, self).clean()
  25. if cleaned_data.has_key("transactionType"):
  26. self.instance.checked = not cleaned_data['transactionType'].needsCheck
  27. return cleaned_data
  28. class VirtualTransactionForm(forms.ModelForm):
  29. """ ModelForm for :class:`Virtual Transactions <VirtualTransaction>` with a
  30. currency field. """
  31. recipient = forms.CharField(max_length=100)
  32. amount = CurrencyField(label='Betrag')
  33. class Meta:
  34. model = VirtualTransaction
  35. exclude = ('user', 'dateTime')
  36. def clean_recipient(self):
  37. try:
  38. user = User.objects.get(username=self.cleaned_data['recipient'])
  39. except User.DoesNotExist:
  40. raise ValidationError(u"Emfpänger '%s' konnte nicht gefunden werden" % self.cleaned_data['recipient'])
  41. if user == self.instance.user:
  42. raise ValidationError(u"Emfpänger '%s' bist du selbst" % self.cleaned_data['recipient'])
  43. return user