def testCreating(self): ind = 0 for code, currency in CURRENCY.items(): ind = ind + 1 price = Money(ind*1000.0, code) Entity.objects.create(name=currency.name, price=price) count = Entity.objects.all().count() self.assertEqual(len(CURRENCY), count) for code in CURRENCY: count = Entity.objects.filter(price_currency=code).count() self.assertTrue(count == 1)
def __init__(self, choices=None, decimal_places=2, max_digits=12, *args, **kwargs): # Note that we catch args and kwargs that must only go to one field # or the other. The rest of them pass onto the decimal field. choices = choices or list( ((u"%s" % (c.code,), u"%s - %s" % (c.code, c.name)) for i, c in sorted(CURRENCY.items()) if c.code != "XXX") ) self.widget = CurrencySelectWidget(choices) fields = ( forms.DecimalField(*args, decimal_places=decimal_places, max_digits=max_digits, **kwargs), forms.ChoiceField(choices=choices), ) super(MoneyField, self).__init__(fields, *args, **kwargs)
def __init__(self, choices=None, decimal_places=2, max_digits=12, *args, **kwargs): # Note that we catch args and kwargs that must only go to one field # or the other. The rest of them pass onto the decimal field. choices = choices or list( ((u"%s" % (c.code, ), u"%s - %s" % (c.code, c.name)) for i, c in sorted(CURRENCY.items()) if c.code != 'XXX')) self.widget = CurrencySelectWidget(choices) fields = (forms.DecimalField(*args, decimal_places=decimal_places, max_digits=max_digits, **kwargs), forms.ChoiceField(choices=choices)) super(MoneyField, self).__init__(fields, *args, **kwargs)
def __init__( self, choices=None, decimal_places=2, max_digits=12, default_currency=None, min_value=None, *args, **kwargs ): # Note that we catch args and kwargs that must only go to one field # or the other. The rest of them pass onto the decimal field. choices = choices or list( ((u"%s" % (c.code,), self.label_from_currency(c)) for i, c in sorted(CURRENCY.items()) if c.code != "XXX") ) self.widget = CurrencySelectWidget(choices) fields = ( forms.DecimalField( *args, decimal_places=decimal_places, min_value=min_value, max_digits=max_digits, **kwargs ), forms.ChoiceField(choices=choices), ) if default_currency: kwargs.update({"initial": Money("", default_currency)}) super(MoneyField, self).__init__(fields, *args, **kwargs)
from django import forms from money import Money, CURRENCY from decimal import Decimal __all__ = ('InputMoneyWidget', 'CurrencySelect',) CURRENCY_CHOICES = sorted([(c.code, c.name) for i, c in CURRENCY.items() if c.code != 'XXX'], key = lambda x: x[1]) class CurrencySelect(forms.Select): def __init__(self, attrs=None, choices=CURRENCY_CHOICES): super(CurrencySelect, self).__init__(attrs, choices) class InputMoneyWidget(forms.TextInput): def __init__(self, attrs=None, currency_widget=None): self.currency_widget = currency_widget if not self.currency_widget: self.currency_widget = CurrencySelect() 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): amount = value
from django import forms from money import Money, CURRENCY from decimal import Decimal __all__ = ( 'InputMoneyWidget', 'CurrencySelect', ) CURRENCY_CHOICES = sorted([(c.code, c.name) for i, c in CURRENCY.items() if c.code != 'XXX'], key=lambda x: x[1]) class CurrencySelect(forms.Select): def __init__(self, attrs=None, choices=CURRENCY_CHOICES): super(CurrencySelect, self).__init__(attrs, choices) class InputMoneyWidget(forms.TextInput): def __init__(self, attrs=None, currency_widget=None): self.currency_widget = currency_widget if not self.currency_widget: self.currency_widget = CurrencySelect() super(InputMoneyWidget, self).__init__(attrs) def render(self, name, value, attrs=None): amount = '' currency = '' if isinstance(value, Money): amount = value.amount
from django import forms as forms from money import Money, CURRENCY from decimal import Decimal from django.conf import settings __all__ = ('InputMoneyWidget', 'CurrencySelect',) if hasattr(settings, 'MONEY_CURRENCY_CHOICES'): CURRENCY_CHOICES = ((c.code, c.name) for i, c in CURRENCY.items() if c.code in settings.MONEY_CURRENCY_CHOICES) else: CURRENCY_CHOICES = ((c.code, c.name) for i, c in CURRENCY.items() if c.code != 'XXX') class CurrencySelect(forms.Select): def __init__(self, attrs=None, choices=CURRENCY_CHOICES): super(CurrencySelect, self).__init__(attrs, choices) class InputMoneyWidget(forms.TextInput): def __init__(self, attrs=None, currency_widget=None): self.currency_widget = currency_widget if not self.currency_widget: self.currency_widget = CurrencySelect() 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
from main import Currency, db from money import CURRENCY for code, obj in CURRENCY.items(): currency = Currency(name=obj.name, iso_code=obj.code) db.session.add(currency) db.session.commit()
from django import forms from money import Money, CURRENCY from decimal import Decimal import operator __all__ = ('InputMoneyWidget', 'CurrencySelect',) CURRENCY_CHOICES = [(c.code, c.name) for i, c in CURRENCY.items() if c.code != 'XXX'] CURRENCY_CHOICES.sort(key=operator.itemgetter(1)) class CurrencySelect(forms.Select): def __init__(self, attrs=None, choices=CURRENCY_CHOICES): super(CurrencySelect, self).__init__(attrs, choices) class InputMoneyWidget(forms.TextInput): def __init__(self, attrs=None, currency_widget=None): self.currency_widget = currency_widget if not self.currency_widget: self.currency_widget = CurrencySelect() 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]