Esempio n. 1
0
    def test_set_default_currency(self):
        expected = Currency('eur', 'EURO')
        from pymonon import set_default_currency

        set_default_currency(expected)

        self.assertEqual(expected, Currency.get_default())
Esempio n. 2
0
    def to_python(self, value):
        if not isinstance(value, tuple):
            raise Exception("Invalid money input, expected sum and currency.")

        amount = super(MoneyField, self).to_python(value[0])
        currency = value[1]
        if not currency:
            raise forms.ValidationError(_(u'Currency is missing'))
        currency = currency.upper()
        if not CURRENCIES.get(currency, False) or currency == Currency.get_default().code:
            raise forms.ValidationError(_(u"Unrecognized currency type '%s'." % currency))
        return Money(amount=amount, currency=currency)
Esempio n. 3
0
    def __init__(self, verbose_name=None, name=None,
                 max_digits=13, decimal_places=3,
                 default=Decimal("0.0"), default_currency=Currency.get_default(), **kwargs):
        if isinstance(default, Money):
            self.default_currency = default.currency

        # Avoid giving the user hard-to-debug errors if they miss required attributes
        if max_digits is None:
            raise Exception("You have to provide a max_digits attribute to Money fields.")

        if decimal_places is None:
            raise Exception("You have to provide a decimal_places attribute to Money fields.")

        self.default_currency = default_currency
        super(MoneyField, self).__init__(verbose_name, name, max_digits, decimal_places, default=default, **kwargs)
Esempio n. 4
0
 def __init__(self, verbose_name=None, name=None, default=Currency.get_default(), **kwargs):
     if isinstance(default, Currency):
         default = default.code
     kwargs['max_length'] = 3
     super(CurrencyField, self).__init__(verbose_name, name, default=default, **kwargs)
Esempio n. 5
0
    def test_initialize_without_currency(self):
        money = Money(1000)

        self.assertEqual(Currency.get_default(), money.currency)
Esempio n. 6
0
    def test_default_currency(self):
        expected = Currency('usd', 'US Dollar')

        self.assertEqual(expected, Currency.get_default())
Esempio n. 7
0
from django import forms
from pymonon import Money, CURRENCIES, Currency
from decimal import Decimal
import operator

__all__ = ('InputMoneyWidget', 'CurrencySelectWidget',)

CURRENCY_CHOICES = [(c.code, c.name) for i, c in CURRENCIES.items() if c.code != Currency.get_default()]
CURRENCY_CHOICES.sort(key=operator.itemgetter(1))


class CurrencySelectWidget(forms.Select):
    def __init__(self, attrs=None, choices=CURRENCY_CHOICES):
        super(CurrencySelectWidget, self).__init__(attrs, choices)


class InputMoneyWidget(forms.TextInput):
    def __init__(self, attrs=None, currency_widget=None):
        self.currency_widget = currency_widget or CurrencySelectWidget()
        super(InputMoneyWidget, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        amount = ''
        currency = ''
        if isinstance(value, Money):
            amount = value.amount
            currency = value.currency.code
        if isinstance(value, tuple):
            amount = value[0]
            currency = value[1]
        if isinstance(value, int) or isinstance(value, Decimal):