Commit 5af40305 authored by Yusei Tahara's avatar Yusei Tahara

Fix fullwidth number conversion and support minus characters conversion....

Fix fullwidth number conversion and support minus characters conversion. Unicode NFKD does not handle minus characters.
parent 94e9b874
...@@ -236,6 +236,17 @@ class TestFloatField(ERP5TypeTestCase): ...@@ -236,6 +236,17 @@ class TestFloatField(ERP5TypeTestCase):
self.assertEquals(node.get('{%s}value-type' % NSMAP['office']), 'float') self.assertEquals(node.get('{%s}value-type' % NSMAP['office']), 'float')
self.assertEquals(node.get('{%s}value' % NSMAP['office']), str(1000.0)) self.assertEquals(node.get('{%s}value' % NSMAP['office']), str(1000.0))
def test_fullwidth_number_conversion(self):
self.portal.REQUEST.set('field_test_field', '123.45')
self.assertEquals(123.45,
self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))
def test_fullwidth_minus_number_conversion(self):
self.portal.REQUEST.set('field_test_field', '−123.45')
self.assertEquals(-123.45,
self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))
class TestIntegerField(ERP5TypeTestCase): class TestIntegerField(ERP5TypeTestCase):
"""Tests integer field """Tests integer field
""" """
...@@ -246,6 +257,7 @@ class TestIntegerField(ERP5TypeTestCase): ...@@ -246,6 +257,7 @@ class TestIntegerField(ERP5TypeTestCase):
def afterSetUp(self): def afterSetUp(self):
self.field = IntegerField('test_field') self.field = IntegerField('test_field')
self.widget = self.field.widget self.widget = self.field.widget
self.validator = self.field.validator
def test_render_odt(self): def test_render_odt(self):
self.field.values['default'] = 34 self.field.values['default'] = 34
...@@ -269,6 +281,16 @@ class TestIntegerField(ERP5TypeTestCase): ...@@ -269,6 +281,16 @@ class TestIntegerField(ERP5TypeTestCase):
.xpath('%s/text()' % ODG_XML_WRAPPING_XPATH, namespaces=NSMAP)[0] .xpath('%s/text()' % ODG_XML_WRAPPING_XPATH, namespaces=NSMAP)[0]
self.assertEquals('0', test_value) self.assertEquals('0', test_value)
def test_fullwidth_number_conversion(self):
self.portal.REQUEST.set('field_test_field', '1234')
self.assertEquals(1234,
self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))
def test_fullwidth_minus_number_conversion(self):
self.portal.REQUEST.set('field_test_field', 'ー1234')
self.assertEquals(-1234,
self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))
class TestStringField(ERP5TypeTestCase): class TestStringField(ERP5TypeTestCase):
"""Tests string field """Tests string field
...@@ -315,6 +337,7 @@ class TestDateTimeField(ERP5TypeTestCase): ...@@ -315,6 +337,7 @@ class TestDateTimeField(ERP5TypeTestCase):
def afterSetUp(self): def afterSetUp(self):
self.field = DateTimeField('test_field') self.field = DateTimeField('test_field')
self.widget = self.field.widget self.widget = self.field.widget
self.validator = self.field.validator
def test_render_odt(self): def test_render_odt(self):
self.field.values['default'] = DateTime('2010/01/01 00:00:01 UTC') self.field.values['default'] = DateTime('2010/01/01 00:00:01 UTC')
...@@ -339,6 +362,16 @@ class TestDateTimeField(ERP5TypeTestCase): ...@@ -339,6 +362,16 @@ class TestDateTimeField(ERP5TypeTestCase):
node = self.field.render_odt_variable(as_string=False) node = self.field.render_odt_variable(as_string=False)
self.assertTrue(node is not None) self.assertTrue(node is not None)
def test_fullwidth_number_conversion(self):
self.portal.REQUEST.set('subfield_field_test_field_year', '2011')
self.portal.REQUEST.set('subfield_field_test_field_month', '12')
self.portal.REQUEST.set('subfield_field_test_field_day', '15')
self.portal.REQUEST.set('subfield_field_test_field_hour', '02')
self.portal.REQUEST.set('subfield_field_test_field_minute', '18')
self.assertEquals(DateTime('2011/12/15 02:18:00'),
self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))
class TestTextAreaField(ERP5TypeTestCase): class TestTextAreaField(ERP5TypeTestCase):
"""Tests TextArea field """Tests TextArea field
""" """
......
...@@ -270,10 +270,7 @@ class IntegerValidator(StringBaseValidator): ...@@ -270,10 +270,7 @@ class IntegerValidator(StringBaseValidator):
if value == "" and not field.get_value('required'): if value == "" and not field.get_value('required'):
return value return value
try: value = normalizeFullWidthNumber(value)
value = unicodedata.normalize('NFKD', value.decode('UTF8')).encode('ASCII', 'ignore')
except UnicodeDecodeError:
pass
try: try:
if value.find(' ')>0: if value.find(' ')>0:
...@@ -302,10 +299,7 @@ class FloatValidator(StringBaseValidator): ...@@ -302,10 +299,7 @@ class FloatValidator(StringBaseValidator):
if value == "" and not field.get_value('required'): if value == "" and not field.get_value('required'):
return value return value
try: value = normalizeFullWidthNumber(value)
value = unicodedata.normalize('NFKD', value.decode('UTF8')).encode('ASCII', 'ignore')
except UnicodeDecodeError:
pass
input_style = field.get_value('input_style') input_style = field.get_value('input_style')
decimal_separator = '' decimal_separator = ''
...@@ -835,3 +829,25 @@ class SuppressValidator(ValidatorBase): ...@@ -835,3 +829,25 @@ class SuppressValidator(ValidatorBase):
return 0 return 0
SuppressValidatorInstance = SuppressValidator() SuppressValidatorInstance = SuppressValidator()
fullwidth_minus_character_list = (
u'\u2010',
u'\u2011',
u'\u2012',
u'\u2013',
u'\u2014',
u'\u2015',
u'\u2212',
u'\u30fc',
u'\uff0d',
)
def normalizeFullWidthNumber(value):
try:
value = unicodedata.normalize('NFKD', value.decode('UTF8'))
if value[0] in fullwidth_minus_character_list:
value = u'-' + value[1:]
value = value.encode('ASCII', 'ignore')
except UnicodeDecodeError:
pass
return value
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment