def setup_default(self, default, default_currency, nullable): if default is None and not nullable: # Backwards compatible fix for non-nullable fields default = 0.0 if isinstance(default, string_types): try: # handle scenario where default is formatted like: # 'amount currency-code' amount, currency = default.split(' ') except ValueError: # value error would be risen if the default is # without the currency part, i.e # 'amount' amount = default currency = default_currency default = Money(Decimal(amount), Currency(code=currency)) elif isinstance(default, (float, Decimal, int)): default = Money(default, default_currency) elif isinstance(default, OldMoney): default.__class__ = Money if not (nullable and default is None) and not isinstance( default, Money): raise ValueError( 'default value must be an instance of Money, is: %s' % default) return default
def setup_default(self, default, default_currency, nullable): if isinstance(default, (str, bytes)): try: # handle scenario where default is formatted like: # 'amount currency-code' amount, currency = default.split(" ") except ValueError: # value error would be risen if the default is # without the currency part, i.e # 'amount' amount = default currency = default_currency default = Money(Decimal(amount), Currency(code=currency)) elif isinstance(default, (float, Decimal, int)): default = Money(default, default_currency) elif isinstance(default, OldMoney): default.__class__ = Money if default is not None and not isinstance(default, Money): raise ValueError("default value must be an instance of Money, is: %s" % default) return default
def setup_default(self, default, default_currency, nullable): if default is None and not nullable: # Backwards compatible fix for non-nullable fields default = 0.0 if isinstance(default, string_types): try: # handle scenario where default is formatted like: # 'amount currency-code' amount, currency = default.split(' ') except ValueError: # value error would be risen if the default is # without the currency part, i.e # 'amount' amount = default currency = default_currency default = Money(Decimal(amount), Currency(code=currency)) elif isinstance(default, (float, Decimal, int)): default = Money(default, default_currency) elif isinstance(default, OldMoney): default.__class__ = Money if not (nullable and default is None) and not isinstance(default, Money): raise ValueError('default value must be an instance of Money, is: %s' % default) return default