Quellcode durchsuchen

Turns out there are empty strings being passed to the currency filter

So we add handling to the filter where we check the value can be
converted to a valid decimal.  If not, return an empty string.

Fixes #316
master
David Winterbottom vor 13 Jahren
Ursprung
Commit
ae13037e48

+ 6
- 2
oscar/templatetags/currency_filters.py Datei anzeigen

@@ -1,3 +1,5 @@
1
+from decimal import Decimal as D, InvalidOperation
2
+
1 3
 from django import template
2 4
 from django.conf import settings
3 5
 from babel.numbers import format_currency
@@ -10,8 +12,10 @@ def currency(value):
10 12
     """
11 13
     Format decimal value as currency
12 14
     """
13
-    if value is None:
14
-        return ''
15
+    try:
16
+        value = D(value)
17
+    except (TypeError, InvalidOperation):
18
+        return u""
15 19
     # Using Babel's currency formatting
16 20
     # http://packages.python.org/Babel/api/babel.numbers-module.html#format_currency
17 21
     kwargs = {

+ 6
- 1
tests/integration/templatetags/test_currency_filters.py Datei anzeigen

@@ -19,7 +19,12 @@ class TestCurrencyFilter(TestCase):
19 19
         }))
20 20
         self.assertTrue(u'£10.23' in out)
21 21
 
22
-    def test_handles_missing_values_gracefully(self):
22
+    def test_handles_none_price_gracefully(self):
23 23
         self.template.render(template.Context({
24 24
             'price': None
25 25
         }))
26
+
27
+    def test_handles_string_price_gracefully(self):
28
+        self.template.render(template.Context({
29
+            'price': ''
30
+        }))

Laden…
Abbrechen
Speichern