Просмотр исходного кода

Add currency tag where currency can be passed in

Needed when your site starts supporting multicurrency things
master
David Winterbottom 12 лет назад
Родитель
Сommit
d643d75473

+ 19
- 0
oscar/templatetags/currency_tags.py Просмотреть файл

@@ -0,0 +1,19 @@
1
+from django import template
2
+from django.conf import settings
3
+from babel.numbers import format_currency
4
+
5
+register = template.Library()
6
+
7
+
8
+@register.simple_tag()
9
+def currency(value, currency=''):
10
+    """
11
+    Render a price in currency notation
12
+    """
13
+    if not currency:
14
+        currency = settings.OSCAR_DEFAULT_CURRENCY
15
+    kwargs = {'currency': currency}
16
+    locale = getattr(settings, 'OSCAR_CURRENCY_LOCALE', None)
17
+    if locale:
18
+        kwargs['locale'] = locale
19
+    return format_currency(value, **kwargs)

+ 28
- 0
tests/integration/templatetags/test_currency_filters.py Просмотреть файл

@@ -5,6 +5,11 @@ from django.test import TestCase
5 5
 from django import template
6 6
 
7 7
 
8
+def render(template_string, ctx):
9
+    tpl = template.Template(template_string)
10
+    return tpl.render(template.Context(ctx))
11
+
12
+
8 13
 class TestCurrencyFilter(TestCase):
9 14
 
10 15
     def setUp(self):
@@ -28,3 +33,26 @@ class TestCurrencyFilter(TestCase):
28 33
         self.template.render(template.Context({
29 34
             'price': ''
30 35
         }))
36
+
37
+
38
+class TestCurrencyTag(TestCase):
39
+
40
+    def test_renders_uk_price_correctly(self):
41
+        ctx = {'price': D('10.23')}
42
+        out = render("{% load currency_tags %}{% currency price 'GBP' %}", ctx)
43
+        self.assertTrue(u'£10.23' in out)
44
+
45
+    def test_renders_us_price_correctly(self):
46
+        ctx = {
47
+            'price': D('10.23'),
48
+            'currency': 'USD'
49
+        }
50
+        out = render(
51
+            "{% load currency_tags %}{% currency price currency %}", ctx)
52
+        self.assertTrue(u'$10.23' in out)
53
+
54
+    def test_uses_default_currency(self):
55
+        ctx = {'price': D('10.23')}
56
+        out = render(
57
+            "{% load currency_tags %}{% currency price currency %}", ctx)
58
+        self.assertTrue(u'£10.23' in out)

Загрузка…
Отмена
Сохранить