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):
self.assertEquals(node.get('{%s}value-type' % NSMAP['office']), 'float')
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):
"""Tests integer field
"""
......@@ -246,6 +257,7 @@ class TestIntegerField(ERP5TypeTestCase):
def afterSetUp(self):
self.field = IntegerField('test_field')
self.widget = self.field.widget
self.validator = self.field.validator
def test_render_odt(self):
self.field.values['default'] = 34
......@@ -269,6 +281,16 @@ class TestIntegerField(ERP5TypeTestCase):
.xpath('%s/text()' % ODG_XML_WRAPPING_XPATH, namespaces=NSMAP)[0]
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):
"""Tests string field
......@@ -315,6 +337,7 @@ class TestDateTimeField(ERP5TypeTestCase):
def afterSetUp(self):
self.field = DateTimeField('test_field')
self.widget = self.field.widget
self.validator = self.field.validator
def test_render_odt(self):
self.field.values['default'] = DateTime('2010/01/01 00:00:01 UTC')
......@@ -339,6 +362,16 @@ class TestDateTimeField(ERP5TypeTestCase):
node = self.field.render_odt_variable(as_string=False)
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):
"""Tests TextArea field
"""
......
......@@ -270,10 +270,7 @@ class IntegerValidator(StringBaseValidator):
if value == "" and not field.get_value('required'):
return value
try:
value = unicodedata.normalize('NFKD', value.decode('UTF8')).encode('ASCII', 'ignore')
except UnicodeDecodeError:
pass
value = normalizeFullWidthNumber(value)
try:
if value.find(' ')>0:
......@@ -302,10 +299,7 @@ class FloatValidator(StringBaseValidator):
if value == "" and not field.get_value('required'):
return value
try:
value = unicodedata.normalize('NFKD', value.decode('UTF8')).encode('ASCII', 'ignore')
except UnicodeDecodeError:
pass
value = normalizeFullWidthNumber(value)
input_style = field.get_value('input_style')
decimal_separator = ''
......@@ -835,3 +829,25 @@ class SuppressValidator(ValidatorBase):
return 0
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