Commit ffd2f0a9 authored by Rafael Monnerat's avatar Rafael Monnerat

The regexp has been moved to a python script and now it can be configured for...

The regexp has been moved to a python script and now it can be configured for each country or region. You must update erp5_base. Added tests and entry at preferences. 
Changes made by Lucas.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21154 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 83983ecd
......@@ -61,14 +61,6 @@ class Telephone(Coordinate, Base):
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
# The standard parser is used to read phone numbers
# written in a standard syntax
# +[country]([area])[number]/[extension]
# or in syntax retured by asText
# +[country](0)[area]-[number]/[extension]
standard_parser = re.compile('\+(?P<country>\d{,3})\(0\)(?P<area>\d+)-(?P<number>[^/]+)(\/(?P<ext>\d+))?')
input_parser = re.compile('(\+(?P<country>\d*))?(\((?P<area>\d*)\))?(?P<number>[^/]*)(\/(?P<ext>\d+))?')
# Declarative properties
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem
......@@ -78,61 +70,99 @@ class Telephone(Coordinate, Base):
security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
def fromText(self, coordinate_text):
""" See ICoordinate.fromText """
method = self._getTypeBasedMethod('fromText')
if method is not None:
return method(text=coordinate_text)
if coordinate_text is None:
coordinate_text = ''
number_match = self.standard_parser.match(coordinate_text)\
or self.input_parser.match(coordinate_text)
if not number_match:
return
number_dict = number_match.groupdict()
country = (number_dict.get('country', '') or '').strip()
area = (number_dict.get('area', '') or '').strip()
number = (number_dict.get('number', '') or '').strip().replace('-', ' ')
extension = (number_dict.get('ext', '') or '').strip()
self.edit(telephone_country = country,
telephone_area = area,
telephone_number = number,
telephone_extension = extension)
""" See ICoordinate.fromText """
method = self._getTypeBasedMethod('fromText')
if method is not None:
return method(text=coordinate_text)
if coordinate_text is None:
coordinate_text = ''
#This regexp get the coordinate text and extract only numbers
onlynumber = ''.join(re.findall('[0-9]', coordinate_text))
ScriptgetRegexp = getattr(self, 'Telephone_getRegexp', None)
#Test if coordinate_text has or not markups.
if len(coordinate_text) > len(onlynumber):
#trying to get a possible contry number to be used by script
country=re.match('((\+|)(?P<country>\d*))',coordinate_text).groupdict().get('country','')
if ScriptgetRegexp is not None:
temp_object = ScriptgetRegexp(index=country)
input_parser = temp_object.input
number_match = re.match(input_parser, coordinate_text)
if not number_match:
return
number_dict = number_match.groupdict()
else:
number_dict={'number':coordinate_text}
country=number_dict.get('country','')
area=number_dict.get('area','')
number=number_dict.get('number','')
ext=number_dict.get('ext','')
if ((country in ['', None]) and \
(area in ['', None]) and \
(number in ['', None]) and \
(ext in ['', None])):
country=area=number=extension=''
else:
#The country and area is trying to get from dict,
#but if it fails must be get from preference
country = (number_dict.get('country') or \
self.portal_preferences.default_site_preference.getPreferredTelephoneDefaultCountryNumber() or \
'').strip()
area = (number_dict.get('area') or \
self.portal_preferences.default_site_preference.getPreferredTelephoneDefaultAreaNumber() or
'').strip()
number = (number_dict.get('number') or '').strip().replace('-', '').replace(' ','')
extension = (number_dict.get('ext') or '').strip()
self.edit(telephone_country = country,
telephone_area = area,
telephone_number = number,
telephone_extension = extension)
security.declareProtected(Permissions.ModifyPortalContent, '_setText')
_setText = fromText
security.declareProtected(Permissions.View, 'asText')
def asText(self):
"""
Returns the telephone number in standard format
"""
script = self._getTypeBasedMethod('asText')
if script is not None:
return script()
text = '+'
telephone_country = self.getTelephoneCountry()
if telephone_country is not None:
text += telephone_country
text += '(0)'
telephone_area = self.getTelephoneArea()
if telephone_area is not None:
text += telephone_area
text += '-'
telephone_number = self.getTelephoneNumber()
if telephone_number is not None:
text += telephone_number
telephone_extension = self.getTelephoneExtension()
if telephone_extension is not None:
text += '/' + telephone_extension
if text == '+(0)-':
text = ''
return text
"""
Returns the telephone number in standard format
"""
script = self._getTypeBasedMethod('asText')
if script is not None:
return script()
telephone_country = self.getTelephoneCountry() or ''
telephone_area = self.getTelephoneArea() or ''
telephone_number = self.getTelephoneNumber() or ''
telephone_extension = self.getTelephoneExtension() or ''
# If country, area, number, extension are blank the method
# should to return blank.
if ((telephone_country == '') and \
(telephone_area == '') and \
(telephone_number == '') and \
(telephone_extension == '')):
return ''
# Trying to get the notation from Telephone_getRegexp script
ScriptgetRegexp = getattr(self, 'Telephone_getRegexp', None)
if ScriptgetRegexp is not None:
temp_object = ScriptgetRegexp(index=telephone_country)
notation = temp_object.notation
else:
notation = ''
if notation not in [None, '']:
notation=notation.replace('<country>',telephone_country)
notation=notation.replace('<area>',telephone_area)
notation=notation.replace('<number>',telephone_number)
notation=notation.replace('<ext>',telephone_extension)
return notation
security.declareProtected(Permissions.AccessContentsInformation,
'asURL')
......
#############################################################################
#
# Copyright (c) 2008 Nexedi SA and Contributors. All Rights Reserved.
# Lucas Carvalho Teixeira <lucas@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
class TelephonePreference:
"""
This property sheet defines configurable default values for
Telephone configuration.
"""
_properties = (
{ 'id' : 'preferred_telephone_default_country_number',
'description' : 'The default country number.',
'type' : 'string',
'preference' : 1,
'mode' : 'w',
'default' : '',
},
{ 'id' : 'preferred_telephone_default_area_number',
'description' : 'The default area number.',
'type' : 'string',
'preference' : 1,
'mode' : 'w',
'default' : '',
},
{ 'id' : 'preferred_telephone_default_region',
'description' : 'The default region.',
'type' : 'string',
'preference' : 1,
'mode' : 'w',
'default' : '',
},
)
......@@ -37,7 +37,6 @@ from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import FileUpload
from AccessControl.SecurityManagement import newSecurityManager
class TestERP5Base(ERP5TypeTestCase):
"""ERP5 Base tests.
......@@ -76,6 +75,7 @@ class TestERP5Base(ERP5TypeTestCase):
self.portal = self.getPortal()
self.portal_categories = self.getCategoryTool()
self.portal_catalog = self.getCatalogTool()
self.portal_preferences = self.getPreferenceTool()
self.createCategories()
# self.login_as_member()
......@@ -863,15 +863,50 @@ class TestERP5Base(ERP5TypeTestCase):
getattr(org.getCreationDate(), slot)(),
'Wrong creation date %s' % org.getCreationDate())
def test_TelephoneAsText(self):
# Test asText method
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
tel.setTelephoneCountry(33)
tel.setTelephoneNumber(123456789)
self.assertEquals('+33(0)-123456789', tel.asText())
tel.setTelephoneArea(2)
tel.setTelephoneNumber(12345678)
tel.setTelephoneExtension(999)
self.assertEquals('+33(0)2-12345678/999', tel.asText())
def test_TelephoneInputList(self):
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
pref = self.portal_preferences.default_site_preference
pref.setPreferredTelephoneDefaultCountryNumber('33')
pref.setPreferredTelephoneDefaultAreaNumber('2')
pref.enable()
inputdict=[
['+33(0)2-27224896/999','+33(0)2-27224896/999'],
['+33(0)2-27224896/','+33(0)2-27224896/'],
['+33(629)02 44 25/222','+33(0)629-024425/222'],
['(22)27224897','+33(0)22-27224897/'],
['12345678','+33(0)2-12345678/'],
['(22) 12345678','+33(0)22-12345678/'],
['66187654321','+33(0)2-66187654321/'],
['(22)-12345678','+33(0)22-12345678/'],
['33 2 098765432/1','+33(0)2-098765432/1']
]
for i in inputdict:
tel.fromText(coordinate_text=i[0])
self.assertEquals(i[1],tel.asText())
def test_TelephoneWhenTheDefaultCountryAndAreaPreferenceIsBlank(self):
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
tel.fromText(coordinate_text='12345678')
self.assertEquals('+(0)-12345678/',tel.asText())
def test_TelephoneAsTextBlankNumber(self):
# Test asText method with blank number
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
self.assertEquals('', tel.asText())
def test_TelephoneUrl(self):
# http://www.rfc-editor.org/rfc/rfc3966.txt
......
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