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): raise forms.ValidationError(_(u"Unrecognized currency type '%s'." % currency)) return Money(amount=amount, currency=currency)
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 == DEFAULT_CURRENCY_CODE: raise forms.ValidationError( _(u"Unrecognized currency type '%s'." % currency)) return Money(amount=amount, currency=currency)
def to_python(self, value): if value is None: return None if isinstance(value, Money): return value if not isinstance(value, tuple): raise Exception( "Invalid money input, expected amount and currency, got: %s." % value) 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 == DEFAULT_CURRENCY_CODE: raise forms.ValidationError( _(u"Unrecognized currency type '%s'." % currency)) return Money(amount=amount, currency=currency)