def render(self, context): money = self.money.resolve(context) if self.money else None amount = self.amount.resolve(context) if self.amount else None currency = self.currency.resolve(context) if self.currency else None if money is not None: if isinstance(money, Money): money = MoneyPatched._patch_to_current_class(money) else: raise TemplateSyntaxError('The variable "money" must be an ' 'instance of Money.') elif amount is not None and currency is not None: money = MoneyPatched(float(amount), str(currency)) else: raise TemplateSyntaxError('You must define both variables: ' 'amount and currency.') money.use_l10n = self.use_l10n if self.var_name is None: return money # as <var_name> context[self.var_name.token] = money return ''
def testOnOff(self): # with a tag template "money_localize" self.assertTemplate( '{% load djmoney %}{% money_localize money %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) # without a tag template "money_localize" self.assertTemplate( '{{ money }}', '2,30 zł', context={'money': MoneyPatched(2.3, 'PLN')}) with self.settings(USE_L10N=False): # money_localize has a default setting USE_L10N = True self.assertTemplate( '{% load djmoney %}{% money_localize money %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) # without a tag template "money_localize" self.assertTemplate( '{{ money }}', '2.30 zł', context={'money': MoneyPatched(2.3, 'PLN')}) mp = MoneyPatched(2.3, 'PLN') mp.use_l10n = True self.assertTemplate( '{{ money }}', '2,30 zł', context={'money': mp}) self.assertTemplate( '{% load djmoney %}{% money_localize money on %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) with self.settings(USE_L10N=False): self.assertTemplate( '{% load djmoney %}{% money_localize money on %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) self.assertTemplate( '{% load djmoney %}{% money_localize money off %}', '2.30 zł', context={'money': Money(2.3, 'PLN')})
def testOnOff(self): # with a tag template "money_localize" self.assertTemplate('{% load djmoney %}{% money_localize money %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) # without a tag template "money_localize" self.assertTemplate('{{ money }}', '2,30 zł', context={'money': MoneyPatched(2.3, 'PLN')}) with self.settings(USE_L10N=False): # money_localize has a default setting USE_L10N = True self.assertTemplate('{% load djmoney %}{% money_localize money %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) # without a tag template "money_localize" self.assertTemplate('{{ money }}', '2.30 zł', context={'money': MoneyPatched(2.3, 'PLN')}) mp = MoneyPatched(2.3, 'PLN') mp.use_l10n = True self.assertTemplate('{{ money }}', '2,30 zł', context={'money': mp}) self.assertTemplate('{% load djmoney %}{% money_localize money on %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) with self.settings(USE_L10N=False): self.assertTemplate( '{% load djmoney %}{% money_localize money on %}', '2,30 zł', context={'money': Money(2.3, 'PLN')}) self.assertTemplate('{% load djmoney %}{% money_localize money off %}', '2.30 zł', context={'money': Money(2.3, 'PLN')})
def test_forced_l10n(): mp = MoneyPatched(2.3, 'PLN') mp.use_l10n = True assert_template('{{ money }}', '2,30 zł', {'money': mp})