[Erp5-report] r28652 - in /erp5/trunk/products: ERP5Form/tests/ Formulator/

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Aug 27 11:18:17 CEST 2009


Author: jerome
Date: Thu Aug 27 11:18:17 2009
New Revision: 28652

URL: http://svn.erp5.org?rev=28652&view=rev
Log:
support more styles (thousand separator/decimal separator) for float fields.

Modified:
    erp5/trunk/products/ERP5Form/tests/testFields.py
    erp5/trunk/products/Formulator/Validator.py
    erp5/trunk/products/Formulator/Widget.py

Modified: erp5/trunk/products/ERP5Form/tests/testFields.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5Form/tests/testFields.py?rev=28652&r1=28651&r2=28652&view=diff
==============================================================================
--- erp5/trunk/products/ERP5Form/tests/testFields.py [utf8] (original)
+++ erp5/trunk/products/ERP5Form/tests/testFields.py [utf8] Thu Aug 27 11:18:17 2009
@@ -26,6 +26,8 @@
 #
 ##############################################################################
 
+# TODO: Some tests from this file can be merged into Formulator
+
 import unittest
 
 # Make it possible to use Globals.get_request
@@ -52,6 +54,7 @@
 
 from Acquisition import aq_base
 from Products.Formulator.FieldRegistry import FieldRegistry
+from Products.Formulator.Validator import ValidationError
 from Products.Formulator.StandardFields import FloatField
 from Products.Formulator.StandardFields import StringField
 from Products.Formulator.StandardFields import DateTimeField
@@ -93,11 +96,29 @@
   def setUp(self):
     self.field = FloatField('test_field')
     self.widget = self.field.widget
-
-  def test_format_thousand_separator(self):
+    self.validator = self.field.validator
+
+  def test_format_thousand_separator_point(self):
     self.field.values['input_style'] = '-1 234.5'
     self.assertEquals('1 000.0', self.widget.format_value(self.field, 1000))
-
+  
+  def test_format_thousand_separator_coma(self):
+    self.field.values['input_style'] = '-1 234,5'
+    self.assertEquals('1 000,0', self.widget.format_value(self.field, 1000))
+
+  def test_format_thousand_separator_point_coma(self):
+    self.field.values['input_style'] = '-1.234,5'
+    self.assertEquals('1.000,0', self.widget.format_value(self.field, 1000))
+
+  def test_format_thousand_separator_coma_point(self):
+    self.field.values['input_style'] = '-1,234.5'
+    self.assertEquals('1,000.0', self.widget.format_value(self.field, 1000))
+
+  def test_format_thousand_separator_first_separator(self):
+    # test for an edge case bug bug, ",100,000.0" was displayed (with leading coma)
+    self.field.values['input_style'] = '-1,234.5'
+    self.assertEquals('100,000.0', self.widget.format_value(self.field, 100000))
+  
   def test_format_percent_style(self):
     self.field.values['input_style'] = '-12.3%'
     self.assertEquals('10.0%', self.widget.format_value(self.field, 0.1))
@@ -138,6 +159,52 @@
     self.assertEquals('10000000000000000000.00',
                       self.field.render(10000000000000000000))
 
+  def test_validate_thousand_separator_point(self):
+    self.field.values['input_style'] = '-1 234.5'
+    request.set('field_test_field', '1 000.0')
+    self.assertEquals(1000,
+        self.validator.validate(self.field, 'field_test_field', request))
+  
+  def test_validate_thousand_separator_coma(self):
+    self.field.values['input_style'] = '-1 234,5'
+    request.set('field_test_field', '1 000,0')
+    self.assertEquals(1000,
+        self.validator.validate(self.field, 'field_test_field', request))
+
+  def test_validate_thousand_separator_point_coma(self):
+    self.field.values['input_style'] = '-1.234,5'
+    request.set('field_test_field', '1.000,0')
+    self.assertEquals(1000,
+        self.validator.validate(self.field, 'field_test_field', request))
+
+  def test_validate_thousand_separator_coma_point(self):
+    self.field.values['input_style'] = '-1,234.5'
+    request.set('field_test_field', '1,000.0')
+    self.assertEquals(1000,
+        self.validator.validate(self.field, 'field_test_field', request))
+
+  def test_validate_percent_style(self):
+    self.field.values['input_style'] = '-12.3%'
+    request.set('field_test_field', '10.0%')
+    self.assertEquals(0.1,
+        self.validator.validate(self.field, 'field_test_field', request))
+
+  def test_validate_not_float(self):
+    request.set('field_test_field', 'not_float')
+    self.assertRaises(ValidationError,
+        self.validator.validate, self.field, 'field_test_field', request)
+
+  def test_validate_two_comma(self):
+    self.field.values['input_style'] = '-1.234,5'
+    request.set('field_test_field', '1,000,0')
+    self.assertRaises(ValidationError,
+        self.validator.validate, self.field, 'field_test_field', request)
+
+  def test_validate_two_dots(self):
+    self.field.values['input_style'] = '-1,234.5'
+    request.set('field_test_field', '1.000.0')
+    self.assertRaises(ValidationError,
+        self.validator.validate, self.field, 'field_test_field', request)
 
 class TestStringField(unittest.TestCase):
   """Tests string field

Modified: erp5/trunk/products/Formulator/Validator.py
URL: http://svn.erp5.org/erp5/trunk/products/Formulator/Validator.py?rev=28652&r1=28651&r2=28652&view=diff
==============================================================================
--- erp5/trunk/products/Formulator/Validator.py [utf8] (original)
+++ erp5/trunk/products/Formulator/Validator.py [utf8] Thu Aug 27 11:18:17 2009
@@ -294,16 +294,36 @@
     value = StringBaseValidator.validate(self, field, key, REQUEST)
     if value == "" and not field.get_value('required'):
       return value
-    value = value.replace(' ','')
+
     input_style = field.get_value('input_style')
-    if value.find(',') >= 0:
-      value = value.replace(',','.')
-    if value.find('%')>=0:
-      value = value.replace('%','')
+    decimal_separator = ''
+    decimal_point = '.'
+
+    if input_style == "-1234.5":
+      decimal_point = '.'
+    elif input_style == '-1 234.5':
+      decimal_separator = ' '
+      decimal_point = '.'
+    elif input_style == '-1 234,5':
+      decimal_separator = ' '
+      decimal_point = ','
+    elif input_style == '-1.234,5':
+      decimal_separator = '.'
+      decimal_point = ','
+    elif input_style == '-1,234.5':
+      decimal_separator = ','
+      decimal_point = '.'
+
+    value = value.replace(decimal_separator,'')
+    input_style = field.get_value('input_style')
+    if value.find(decimal_point) >= 0:
+      value = value.replace(decimal_point, '.')
+    if value.find('%') >= 0:
+      value = value.replace('%', '')
     try:
       value = float(value)
       if input_style.find('%')>=0:
-        value = value/100
+        value = value / 100
     except ValueError:
       self.raise_error('not_float', field)
     return value

Modified: erp5/trunk/products/Formulator/Widget.py
URL: http://svn.erp5.org/erp5/trunk/products/Formulator/Widget.py?rev=28652&r1=28651&r2=28652&view=diff
==============================================================================
--- erp5/trunk/products/Formulator/Widget.py [utf8] (original)
+++ erp5/trunk/products/Formulator/Widget.py [utf8] Thu Aug 27 11:18:17 2009
@@ -1271,8 +1271,11 @@
       "The type of float we should enter. "),
                                   default="-1234.5",
                                   items=[("-1234.5",  "-1234.5"),
-                                        ("-1 234.5", "-1 234.5"),
-                                        ("-12.3%", "-12.3%"),],
+                                         ("-1 234.5", "-1 234.5"),
+                                         ("-1 234,5", "-1 234,5"),
+                                         ("-1.234,5", "-1.234,5"),
+                                         ("-1,234.5", "-1,234.5"),
+                                         ("-12.3%", "-12.3%"),],
                                   required=1,
                                   size=1)
 
@@ -1309,17 +1312,38 @@
           value = '%f' % float(original_value)
       value_list = value.split('.')
       integer = value_list[0]
-      if input_style.find(' ')>=0:
+      
+      decimal_separator = ''
+      decimal_point = '.'
+      
+      if input_style == "-1234.5":
+        decimal_point = '.'
+      elif input_style == '-1 234.5':
+        decimal_separator = ' '
+        decimal_point = '.'
+      elif input_style == '-1 234,5':
+        decimal_separator = ' '
+        decimal_point = ','
+      elif input_style == '-1.234,5':
+        decimal_separator = '.'
+        decimal_point = ','
+      elif input_style == '-1,234.5':
+        decimal_separator = ','
+        decimal_point = '.'
+
+      if input_style.find(decimal_separator) >= 0:
         integer = value_list[0]
-        i = len(integer)%3
+        i = len(integer) % 3
         value = integer[:i]
         while i != len(integer):
-          value += ' ' + integer[i:i+3]
+          value += decimal_separator + integer[i:i+3]
           i += 3
+        if value[0] == decimal_separator:
+          value = value[1:]
       else:
         value = value_list[0]
       if precision != 0:
-        value += '.'
+        value += decimal_point
       if precision not in (None,''):
         for i in range(0,precision):
           if i < len(value_list[1]):




More information about the Erp5-report mailing list