Преглед на файлове

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 преди 13 години
родител
ревизия
ae13037e48
променени са 2 файла, в които са добавени 12 реда и са изтрити 3 реда
  1. 6
    2
      oscar/templatetags/currency_filters.py
  2. 6
    1
      tests/integration/templatetags/test_currency_filters.py

+ 6
- 2
oscar/templatetags/currency_filters.py Целия файл

1
+from decimal import Decimal as D, InvalidOperation
2
+
1
 from django import template
3
 from django import template
2
 from django.conf import settings
4
 from django.conf import settings
3
 from babel.numbers import format_currency
5
 from babel.numbers import format_currency
10
     """
12
     """
11
     Format decimal value as currency
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
     # Using Babel's currency formatting
19
     # Using Babel's currency formatting
16
     # http://packages.python.org/Babel/api/babel.numbers-module.html#format_currency
20
     # http://packages.python.org/Babel/api/babel.numbers-module.html#format_currency
17
     kwargs = {
21
     kwargs = {

+ 6
- 1
tests/integration/templatetags/test_currency_filters.py Целия файл

19
         }))
19
         }))
20
         self.assertTrue(u'£10.23' in out)
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
         self.template.render(template.Context({
23
         self.template.render(template.Context({
24
             'price': None
24
             'price': None
25
         }))
25
         }))
26
+
27
+    def test_handles_string_price_gracefully(self):
28
+        self.template.render(template.Context({
29
+            'price': ''
30
+        }))

Loading…
Отказ
Запис