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): ...@@ -61,14 +61,6 @@ class Telephone(Coordinate, Base):
security = ClassSecurityInfo() security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation) 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 # Declarative properties
property_sheets = ( PropertySheet.Base property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem , PropertySheet.SimpleItem
...@@ -78,61 +70,99 @@ class Telephone(Coordinate, Base): ...@@ -78,61 +70,99 @@ class Telephone(Coordinate, Base):
security.declareProtected(Permissions.ModifyPortalContent, 'fromText') security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
def fromText(self, coordinate_text): def fromText(self, coordinate_text):
""" See ICoordinate.fromText """ """ See ICoordinate.fromText """
method = self._getTypeBasedMethod('fromText') method = self._getTypeBasedMethod('fromText')
if method is not None: if method is not None:
return method(text=coordinate_text) return method(text=coordinate_text)
if coordinate_text is None: if coordinate_text is None:
coordinate_text = '' coordinate_text = ''
number_match = self.standard_parser.match(coordinate_text)\
or self.input_parser.match(coordinate_text) #This regexp get the coordinate text and extract only numbers
if not number_match: onlynumber = ''.join(re.findall('[0-9]', coordinate_text))
return ScriptgetRegexp = getattr(self, 'Telephone_getRegexp', None)
number_dict = number_match.groupdict()
country = (number_dict.get('country', '') or '').strip() #Test if coordinate_text has or not markups.
area = (number_dict.get('area', '') or '').strip() if len(coordinate_text) > len(onlynumber):
number = (number_dict.get('number', '') or '').strip().replace('-', ' ') #trying to get a possible contry number to be used by script
extension = (number_dict.get('ext', '') or '').strip() country=re.match('((\+|)(?P<country>\d*))',coordinate_text).groupdict().get('country','')
self.edit(telephone_country = country, if ScriptgetRegexp is not None:
telephone_area = area, temp_object = ScriptgetRegexp(index=country)
telephone_number = number, input_parser = temp_object.input
telephone_extension = extension) 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') security.declareProtected(Permissions.ModifyPortalContent, '_setText')
_setText = fromText _setText = fromText
security.declareProtected(Permissions.View, 'asText') security.declareProtected(Permissions.View, 'asText')
def asText(self): def asText(self):
""" """
Returns the telephone number in standard format Returns the telephone number in standard format
""" """
script = self._getTypeBasedMethod('asText') script = self._getTypeBasedMethod('asText')
if script is not None: if script is not None:
return script() return script()
text = '+' telephone_country = self.getTelephoneCountry() or ''
telephone_country = self.getTelephoneCountry() telephone_area = self.getTelephoneArea() or ''
if telephone_country is not None: telephone_number = self.getTelephoneNumber() or ''
text += telephone_country telephone_extension = self.getTelephoneExtension() or ''
text += '(0)' # If country, area, number, extension are blank the method
telephone_area = self.getTelephoneArea() # should to return blank.
if telephone_area is not None: if ((telephone_country == '') and \
text += telephone_area (telephone_area == '') and \
(telephone_number == '') and \
text += '-' (telephone_extension == '')):
telephone_number = self.getTelephoneNumber() return ''
if telephone_number is not None:
text += telephone_number # Trying to get the notation from Telephone_getRegexp script
ScriptgetRegexp = getattr(self, 'Telephone_getRegexp', None)
telephone_extension = self.getTelephoneExtension() if ScriptgetRegexp is not None:
if telephone_extension is not None: temp_object = ScriptgetRegexp(index=telephone_country)
text += '/' + telephone_extension notation = temp_object.notation
else:
if text == '+(0)-': notation = ''
text = ''
return text 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, security.declareProtected(Permissions.AccessContentsInformation,
'asURL') '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 ...@@ -37,7 +37,6 @@ from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ERP5Type.tests.utils import FileUpload from Products.ERP5Type.tests.utils import FileUpload
from AccessControl.SecurityManagement import newSecurityManager from AccessControl.SecurityManagement import newSecurityManager
class TestERP5Base(ERP5TypeTestCase): class TestERP5Base(ERP5TypeTestCase):
"""ERP5 Base tests. """ERP5 Base tests.
...@@ -76,6 +75,7 @@ class TestERP5Base(ERP5TypeTestCase): ...@@ -76,6 +75,7 @@ class TestERP5Base(ERP5TypeTestCase):
self.portal = self.getPortal() self.portal = self.getPortal()
self.portal_categories = self.getCategoryTool() self.portal_categories = self.getCategoryTool()
self.portal_catalog = self.getCatalogTool() self.portal_catalog = self.getCatalogTool()
self.portal_preferences = self.getPreferenceTool()
self.createCategories() self.createCategories()
# self.login_as_member() # self.login_as_member()
...@@ -863,15 +863,50 @@ class TestERP5Base(ERP5TypeTestCase): ...@@ -863,15 +863,50 @@ class TestERP5Base(ERP5TypeTestCase):
getattr(org.getCreationDate(), slot)(), getattr(org.getCreationDate(), slot)(),
'Wrong creation date %s' % org.getCreationDate()) 'Wrong creation date %s' % org.getCreationDate())
def test_TelephoneAsText(self): def test_TelephoneAsText(self):
# Test asText method # Test asText method
pers = self.getPersonModule().newContent(portal_type='Person') pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone') tel = pers.newContent(portal_type='Telephone')
tel.setTelephoneCountry(33) tel.setTelephoneCountry(33)
tel.setTelephoneNumber(123456789) tel.setTelephoneArea(2)
self.assertEquals('+33(0)-123456789', tel.asText()) 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): def test_TelephoneUrl(self):
# http://www.rfc-editor.org/rfc/rfc3966.txt # 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