Commit d7d089e8 authored by Rafael Monnerat's avatar Rafael Monnerat

Replaced dictionary per simple list of regex (Lucas)

Improved the code style (Rafael and Lucas)



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21309 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 63dd1305
...@@ -36,437 +36,273 @@ from Products.ERP5.Document.Coordinate import Coordinate ...@@ -36,437 +36,273 @@ from Products.ERP5.Document.Coordinate import Coordinate
import re import re
class Telephone(Coordinate, Base): class Telephone(Coordinate, Base):
"""
A telephone is a coordinate which stores a telephone number
The telephone class may be used by multiple content types (ex. Fax,
Mobile Phone, Fax, Remote Access, etc.).
A telephone is a terminating leaf
in the OFS. It can not contain anything.
Telephone inherits from Base and
from the mix-in Coordinate
A list of I18N telephone codes can be found here::
http://kropla.com/dialcode.htm
"""
meta_type = 'ERP5 Telephone'
portal_type = 'Telephone'
add_permission = Permissions.AddPortalContent
isPortalContent = 1
isRADContent = 1
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
# Declarative properties
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem
, PropertySheet.Telephone
)
# This is a list of regex.
# Each regex into the list handle a single (or should handle) input.
# The list is a priority list,
# be carefull to add a new regex.
regex_list = [
# Country, Area, Number, Extension*
# +33(0)2-27224896/999 or +33(0)2-27224896/ or +33(0)2-27224896
"\+(?P<country>\d+)\(0\)(?P<area>\d+)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# +81-2-27224896/999 or +81-2-27224896/ or +81-2-27224896
"\+(?P<country>\d+)-(?P<area>\d+)-(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing area
# +33(0)-27224896/999" or +33(0)-27224896/ or +33(0)-27224896
"\+(?P<country>\d+)\(0\)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# +55(2)27224896/999 or +55(2)27224896/ or +55(2)27224896
"\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# +221-2-27224896/999 or +221-2-27224896/ or +221-2-27224896
"\+(?P<country>\d+)-(?P<area>\d+)-(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing country
# +(0)2-27224896/999" or +(0)2-27224896/ or +(0)2-27224896
"\+\(0\)(?P<area>\d+)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Missing Country and Area
# +(0)-27224896/999" or +(0)-27224896/ or +(0)-27224896
"\+\(0\)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Area, Number Extension*
# Area between parenthesis.
# (22)27224897/333 or (22)27224897/ or (22)27224897
"\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing country
# +(2)27224896/999" or +(2)27224896/ or +(2)27224896
"\+\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number and Extension*
# Country space area space number slash extension or not
# +33 2 098765432/1 or +33 2 098765432/ or +33 2 098765432
"\+(?P<country>\d+)\ (?P<area>\d+)\ (?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# This regex is to handle two inputs very similar
# but with different behavior
# 631 22 43/999 or 631 22 43 or 631 22 43
# will result in {'area':'', 'number':'631 22 43', 'ext':'999 or empty'}
#
# 631-22 43/999 or 631-22 43 or 631-22 43
# will result in {'area':'631', 'number':'22 43', 'ext':'999 or empty'}
"^(?:0)?((?P<area>\d+)-)?(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# It is a common input in France
# and in Japan but with different behavior.
# 047-336-5411/999 or 047-336-5411/ or 047-336-5411
# will result in {'area':'47', 'number':'336-5411', \
# 'ext':'999 or empty'} <= France
# will result in {'area':'047', 'number':'336-5411',
# 'ext':'999 or empty'} <= Japan
# so we have here two regex:
# To France: "^0(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# To Japan: "^(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
"^0(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# It is a common input in France and in Japan but with different behavior.
# 047(336)5411/999 or 047(336)5411/ or 047(336)5411
# will result in {'area':'47', 'number':'336)5411',
# 'ext':'999 or empty'} <= France
# will result in {'area':'047', 'number':'336)5411',
# 'ext':'999 or empty'} <= Japan
# so we have here two regex:
#To France:
# "^0(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$",
#To Japan:
# "^(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$",
"^0(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$",
# Missing area
# +55()27224896/999" or +55()27224896/ or +55()27224896
"\+(?P<country>\d+)\(\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)"
]
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 = ''
# This regexp get the coordinate text
# and extract number and letters
input_regex_without_markup = '[0-9A-Za-z]'
input_without_markup = ''.join(re.findall(input_regex_without_markup,\
coordinate_text))
# Test if coordinate_text has or not markups.
if len(coordinate_text) > len(input_without_markup):
number_match = None
for regex in self._getRegexList():
possible_number_match = re.match(regex, coordinate_text)
if possible_number_match not in [None]:
number_match = possible_number_match
break
if number_match == None:
from zLOG import LOG, WARNING
msg = "Doesn't exist a regex to handle this telephone: ", \
coordinate_text
LOG('Telephone.fromText', WARNING, msg)
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','')
extension = number_dict.get('ext','')
if ((country in ['', None]) and \
(area in ['', None]) and \
(number in ['', None]) and \
(extension 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
preference_tool = self.portal_preferences
if country in ['', None]:
country = preference_tool.getPreferredTelephoneDefaultCountryNumber('')
if area in ['', None]:
area = preference_tool.getPreferredTelephoneDefaultAreaNumber('')
country = country.strip()
area = area.strip()
number = number.strip()
extension = extension.strip()
# Formating the number.
# Removing any ")", "(", "-" and " "
for token in [")", "(", "-" ," "]:
number = number.replace(token, '')
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()
country = self.getTelephoneCountry('')
area = self.getTelephoneArea('')
number = self.getTelephoneNumber('')
extension = self.getTelephoneExtension('')
# If country, area, number, extension are blank
# the method should to return blank.
if ((country == '') and \
(area == '') and \
(number == '') and \
(extension == '')):
return ''
# Define the notation
notation = self._getNotation()
if notation not in [None, '']:
notation = notation.replace('<country>',country)
notation = notation.replace('<area>',area)
notation = notation.replace('<number>',number)
notation = notation.replace('<ext>',extension)
if extension == '':
notation = notation.replace('/','')
return notation
security.declareProtected(Permissions.AccessContentsInformation,
'asURL')
def asURL(self):
"""Returns a text representation of the Url if defined
or None else.
""" """
A telephone is a coordinate which stores a telephone number telephone_country = self.getTelephoneCountry()
The telephone class may be used by multiple content types (ex. Fax, if telephone_country is not None:
Mobile Phone, Fax, Remote Access, etc.). url_string = '+%s' % telephone_country
else :
url_string = '0'
A telephone is a terminating leaf telephone_area = self.getTelephoneArea()
in the OFS. It can not contain anything. if telephone_area is not None:
url_string += telephone_area
Telephone inherits from Base and telephone_number = self.getTelephoneNumber()
from the mix-in Coordinate if telephone_number is not None:
url_string += telephone_number
A list of I18N telephone codes can be found here:: if url_string == '0':
http://kropla.com/dialcode.htm return None
return 'tel:%s' % (url_string.replace(' ',''))
security.declareProtected(Permissions.View, 'getText')
getText = asText
security.declareProtected(Permissions.View, 'standardTextFormat')
def standardTextFormat(self):
""" """
Returns the standard text formats for telephone numbers
"""
return ("+33(0)6-62 05 76 14",)
meta_type = 'ERP5 Telephone' def _getNotation(self):
portal_type = 'Telephone' """
add_permission = Permissions.AddPortalContent Returns the notation that will be used by asText method.
isPortalContent = 1 """
isRADContent = 1 # The notation can be changed.
# But needs to have <country>, <area>, <number> and <ext>
# Declarative security return "+<country>(0)<area>-<number>/<ext>"
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation) def _getRegexList(self):
"""
# Declarative properties Returns the regex list that will be used by fromText method.
property_sheets = ( PropertySheet.Base """
, PropertySheet.SimpleItem return self.regex_list
, PropertySheet.Telephone
)
# The notation always need to have:
# <country> or <area> or <number> or <ext>
# If uses a different tag it will be ignored.
standard_dict = {
# France
'33' : {
"regex_input_list" : [
# Country, Area, Number, Extension*
# +33(0)2-27224896/999 or +33(0)2-27224896/ or +33(0)2-27224896
"\+(?P<country>\d+)\(0\)(?P<area>\d+)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Missing country
# +(0)2-27224896/999" or +(0)2-27224896/ or +(0)2-27224896
"\+\(0\)(?P<area>\d+)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Missing area
# +33(0)-27224896/999" or +33(0)-27224896/ or +33(0)-27224896
"\+(?P<country>\d+)\(0\)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Missing Country and Area
# +(0)-27224896/999" or +(0)-27224896/ or +(0)-27224896
"\+\(0\)\-(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# The area between parenthesis.
# +33(629)024425/222 or +33(629)024425/ or +33(629)024425
"^\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ ]+)/?(?P<ext>\d+|)$",
# Area, Number Extension*
# Area between parenthesis.
# (22)27224897/333 or (22)27224897/ or (22)27224897
"\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Area, Number, Extension*
# Area followed by '-'
# (22)-12345678/222 or (22)-12345678/ or (22)-12345678
"\((?P<area>\d+)\)\-?(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number and Extension*
# Country space area space number slash extension or not
# +33 2 098765432/1 or +33 2 098765432/ or +33 2 098765432
"\+(?P<country>\d+)\ (?P<area>\d+)\ (?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# This regex is to handle two inputs very similar
# but with different behavior
# 631 22 43/999 or 631 22 43 or 631 22 43
# will result in {'area':'', 'country':'631 22 43', 'ext':'999 or empty'}
#
# 631-22 43/999 or 631-22 43 or 631-22 43
# will result in {'area':'631', 'country':'22 43', 'ext':'999 or empty'}
"^(?:0)?((?P<area>\d+)-)?(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047-336-5411/999 or 047-336-5411/ or 047-336-5411
"^0(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047(336)5411/999 or 047(336)5411/ or 047(336)5411
"^0(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$"
],
"notation" : "+<country>(0)<area>-<number>/<ext>"
},
# Brazil
'55' : {
"regex_input_list" : [
# Country, Area, Number, Extension*
# +55(2)27224896/999 or +55(2)27224896/ or +55(2)27224896
"\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# +55-2-27224896/999 or +55-2-27224896/ or +55-2-27224896
"\+(?P<country>\d+)-(?P<area>\d+)-(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing country
# +(2)27224896/999 or +(2)27224896/ or +(2)27224896
"\+\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing area
# +55()-27224896/999 or +55()-27224896/ or +55()-27224896
# +55()27224896/999 or +55()27224896/ or +55()27224896
"\+(?P<country>\d+)\(\)(?:\-)?(?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# Missing Country and Area
# +()27224896/999 or +()27224896/ or +()27224896
"\+\(\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# The area between parenthesis.
# +55(629)024425/222 or +55(629)024425/ or +55(629)024425
"^\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ ]+)/?(?P<ext>\d+|)$",
# Area, Number Extension*
# Area between parenthesis.
# (22)27224897/333 or (22)27224897/ or (22)27224897
"\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number and Extension*
# Country space area space number slash extension or not
# +55 2 098765432/1 or +55 2 098765432/ or +55 2 098765432
"\+(?P<country>\d+)\ (?P<area>\d+)\ (?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# This regex is to handle two inputs very similar
# but with different behavior
# 631 22 43/999 or 631 22 43 or 631 22 43
# will result in {'area':'', 'country':'631 22 43', 'ext':'999 or empty'}
#
# 631-22 43/999 or 631-22 43 or 631-22 43
# will result in {'area':'631', 'country':'22 43', 'ext':'999 or empty'}
"^((?P<area>\d+)-)?(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047-336-5411/999 or 047-336-5411/ or 047-336-5411
"^(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047(336)5411/999 or 047(336)5411/ or 047(336)5411
"^(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$"
],
"notation" : "+<country>(<area>)<number>/<ext>",
},
# Senegal
'221' : {
"regex_input_list" : [
# Country, Area, Number, Extension*
# +221(2)27224896/999 or +221(2)27224896/ or +221(2)27224896
"\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# +221-2-27224896/999 or +221-2-27224896/ or +221-2-27224896
"\+(?P<country>\d+)-(?P<area>\d+)-(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing country
# +(2)27224896/999" or +(2)27224896/ or +(2)27224896
"\+\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing area
# +221()27224896/999" or +221()27224896/ or +221()27224896
"\+(?P<country>\d+)\(\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing Country and Area
# +()27224896/999" or +()27224896/ or +()27224896
"\+\(\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# The area between parenthesis.
# +221(629)024425/222 or +221(629)024425/ or +221(629)024425
"^\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ ]+)/?(?P<ext>\d+|)$",
# Area, Number Extension*
# Area between parenthesis.
# (22)27224897/333 or (22)27224897/ or (22)27224897
"\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number and Extension*
# Country space area space number slash extension or not
# +221 2 098765432/1 or +221 2 098765432/ or +221 2 098765432
"\+(?P<country>\d+)\ (?P<area>\d+)\ (?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# This regex is to handle two inputs very similar
# but with different behavior
# 631 22 43/999 or 631 22 43 or 631 22 43
# will result in {'area':'', 'country':'631 22 43', 'ext':'999 or empty'}
#
# 631-22 43/999 or 631-22 43 or 631-22 43
# will result in {'area':'631', 'country':'22 43', 'ext':'999 or empty'}
"^((?P<area>\d+)-)?(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047-336-5411/999 or 047-336-5411/ or 047-336-5411
"^(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047(336)5411/999 or 047(336)5411/ or 047(336)5411
"^(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$"
],
"notation" : "+<country>(<area>)<number>/<ext>",
},
# Japan
'81' : {
"regex_input_list" : [
# Country, Area, Number, Extension*
# +81(2)27224896/999 or +81(2)27224896/ or +81(2)27224896
"\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# +81-2-27224896/999 or +81-2-27224896/ or +81-2-27224896
"\+(?P<country>\d+)-(?P<area>\d+)-(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing country
# +(2)27224896/999" or +(2)27224896/ or +(2)27224896
"\+\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Missing Country and Area
# +()27224896/999" or +()27224896/ or +()27224896
"\+\(\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number, Extension*
# The area between parenthesis.
# +81(629)024425/222 or +81(629)024425/ or +81(629)024425
"^\+(?P<country>\d+)\((?P<area>\d+)\)(?P<number>[\d\ ]+)/?(?P<ext>\d+|)$",
# Area, Number Extension*
# Area between parenthesis.
# (22)27224897/333 or (22)27224897/ or (22)27224897
"\((?P<area>\d+)\)(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
# Country, Area, Number and Extension*
# Country space area space number slash extension or not
# +81 2 098765432/1 or +81 2 098765432/ or +81 2 098765432
"\+(?P<country>\d+)\ (?P<area>\d+)\ (?P<number>[\d\ ]*)(?:\/)?(?P<ext>\d+|)",
# This regex is to handle two inputs very similar
# but with different behavior
# 631 22 43/999 or 631 22 43 or 631 22 43
# will result in {'area':'', 'country':'631 22 43', 'ext':'999 or empty'}
#
# 631-22 43/999 or 631-22 43 or 631-22 43
# will result in {'area':'631', 'country':'22 43', 'ext':'999 or empty'}
"^((?P<area>\d+)-)?(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047-336-5411/999 or 047-336-5411/ or 047-336-5411
"^(?P<area>\d+)-(?P<number>[\d\-\ ]*)(?:\/)?(?P<ext>\d+|)$",
# Area, Number, Extension*
# 047(336)5411/999 or 047(336)5411/ or 047(336)5411
"^(?P<area>\d+)\((?P<number>[\d\)\(\ \-]*)(?:\/)?(?P<ext>\d+|)$"
],
"notation" : "+<country>(<area>)<number>/<ext>",
}
}
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 = ''
# This regexp get the coordinate text
# and extract number and letters
input_regex_without_markup = '[0-9A-Za-z]'
input_without_markup = ''.join(re.findall(input_regex_without_markup,\
coordinate_text))
# Test if coordinate_text has or not markups.
if len(coordinate_text) > len(input_without_markup):
# Trying to get the regex list.
input_parser_list = self._getRegexList(coordinate_text=coordinate_text)
number_match = None
for input_parser in input_parser_list:
possible_number_match = re.match(input_parser, coordinate_text)
if possible_number_match not in [None]:
number_match = possible_number_match
break
if number_match == None:
from zLOG import LOG, WARNING
msg = "Doesn't exist a regex to handle this telephone: ", coordinate_text
LOG('*** Telephone ***',WARNING,msg)
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_default_number = self.portal_preferences.getPreferredTelephoneDefaultCountryNumber()
area_default_number = self.portal_preferences.getPreferredTelephoneDefaultAreaNumber()
country = (number_dict.get('country') or \
country_default_number or \
'').strip()
area = (number_dict.get('area') or \
area_default_number or
'').strip()
number = (number_dict.get('number') or '').strip()
# Formating the number.
# Removing any ")", "(", "-" and " "
number = number.replace('-', '')
number = number.replace(' ','')
number = number.replace(')','')
number = number.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()
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
notation = self._getNotation(telephone_country=telephone_country)
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)
if telephone_extension == '':
notation=notation.replace('/','')
return notation
security.declareProtected(Permissions.AccessContentsInformation,
'asURL')
def asURL(self):
"""Returns a text representation of the Url if defined
or None else.
"""
telephone_country = self.getTelephoneCountry()
if telephone_country is not None:
url_string = '+%s' % telephone_country
else :
url_string = '0'
telephone_area = self.getTelephoneArea()
if telephone_area is not None:
url_string += telephone_area
telephone_number = self.getTelephoneNumber()
if telephone_number is not None:
url_string += telephone_number
if url_string == '0':
return None
return 'tel:%s' % (url_string.replace(' ',''))
security.declareProtected(Permissions.View, 'getText')
getText = asText
security.declareProtected(Permissions.View, 'standardTextFormat')
def standardTextFormat(self):
"""
Returns the standard text formats for telephone numbers
"""
return ("+33(0)6-62 05 76 14",)
def _getRegexDict(self, index=None):
"""
Returns a dict with Regex and Notations based from
country or region
"""
# Trying to get the Regex Dict
if index not in self.standard_dict.keys():
index = self.portal_preferences.getPreferredTelephoneDefaultCountryNumber() or '33'
return self.standard_dict.get(index)
def _getRegexList(self, coordinate_text):
"""
Returns the regex list.
"""
# Trying to get a possible contry number
country_regex = '((\+|)(?P<country>\d*))'
country = re.match(country_regex, \
coordinate_text).groupdict().get('country','')
regex_dict = self._getRegexDict(index=country)
input_parser = regex_dict.get('regex_input_list')
return input_parser
def _getNotation(self, telephone_country):
"""
Returns the notation that will be used by asText method.
"""
regex_dict = self._getRegexDict(index=telephone_country)
notation = regex_dict.get('notation')
return notation
...@@ -873,7 +873,7 @@ class TestERP5Base(ERP5TypeTestCase): ...@@ -873,7 +873,7 @@ class TestERP5Base(ERP5TypeTestCase):
tel.setTelephoneExtension(999) tel.setTelephoneExtension(999)
self.assertEquals('+33(0)2-12345678/999', tel.asText()) self.assertEquals('+33(0)2-12345678/999', tel.asText())
def test_TelephoneInputListForFrance(self): def test_TelephoneInputList(self):
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')
pref = self.portal_preferences.default_site_preference pref = self.portal_preferences.default_site_preference
...@@ -925,7 +925,6 @@ class TestERP5Base(ERP5TypeTestCase): ...@@ -925,7 +925,6 @@ class TestERP5Base(ERP5TypeTestCase):
["62 05 76 14/","+33(0)2-62057614"], ["62 05 76 14/","+33(0)2-62057614"],
["62 05 76 14","+33(0)2-62057614"], ["62 05 76 14","+33(0)2-62057614"],
["631 22 43/999","+33(0)2-6312243/999"], ["631 22 43/999","+33(0)2-6312243/999"],
["631 22 43/","+33(0)2-6312243"], ["631 22 43/","+33(0)2-6312243"],
["631 22 43","+33(0)2-6312243"], ["631 22 43","+33(0)2-6312243"],
...@@ -947,269 +946,41 @@ class TestERP5Base(ERP5TypeTestCase): ...@@ -947,269 +946,41 @@ class TestERP5Base(ERP5TypeTestCase):
["+33 662 1244 4112/999","+33(0)662-12444112/999"], ["+33 662 1244 4112/999","+33(0)662-12444112/999"],
["+33 662 1244 4112/","+33(0)662-12444112"], ["+33 662 1244 4112/","+33(0)662-12444112"],
["+33 662 1244 4112","+33(0)662-12444112"], ["+33 662 1244 4112","+33(0)662-12444112"],
]
for i in input_list: ["+55(2)27224896/999","+55(0)2-27224896/999"],
tel.fromText(coordinate_text=i[0]) ["+55(2)27224896/","+55(0)2-27224896"],
self.assertEquals(i[1],tel.asText()) ["+55(2)27224896","+55(0)2-27224896"],
def test_TelephoneInputListForSenegal(self):
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
pref = self.portal_preferences.default_site_preference
pref.setPreferredTelephoneDefaultCountryNumber('221')
pref.setPreferredTelephoneDefaultAreaNumber('')
pref.enable()
input_list = [
["+221(2)27224896/999","+221(2)27224896/999"],
["+221(2)27224896/","+221(2)27224896"],
["+221(2)27224896","+221(2)27224896"],
["+221()27224896/999","+221()27224896/999"], ["+55()27224896/999","+55(0)2-27224896/999"],
["+221()27224896/","+221()27224896"], ["+55()27224896/","+55(0)2-27224896"],
["+221()27224896","+221()27224896"], ["+55()27224896","+55(0)2-27224896"],
["+221()-27224896/999","+221()27224896/999"], ["+55()-27224896/999","+55(0)2-27224896/999"],
["+221()-27224896/","+221()27224896"], ["+55()-27224896/","+55(0)2-27224896"],
["+221()-27224896","+221()27224896"], ["+55()-27224896","+55(0)2-27224896"],
["+(2)27224896/999","+221(2)27224896/999"],
["+(2)27224896/","+221(2)27224896"],
["+(2)27224896","+221(2)27224896"],
["+()27224896/999","+221()27224896/999"],
["+()27224896/","+221()27224896"],
["+()27224896","+221()27224896"],
["+221(629)024425/222","+221(629)024425/222"],
["+221(629)024425/","+221(629)024425"],
["+221(629)024425","+221(629)024425"],
["(22)27224897/333","+221(22)27224897/333"],
["(22)27224897/","+221(22)27224897"],
["(22)27224897","+221(22)27224897"],
["(22)-12345678/222","+221(22)12345678/222"],
["(22)-12345678/","+221(22)12345678"],
["(22)-12345678","+221(22)12345678"],
["+221 2 098765432/1","+221(2)098765432/1"],
["+221 2 098765432/","+221(2)098765432"],
["+221 2 098765432","+221(2)098765432"],
["+221 662 1241 1233/999","+221(662)12411233/999"],
["+221 662 1241 1233/","+221(662)12411233"],
["+221 662 1241 1233","+221(662)12411233"],
["6-62 05 76 14/999","+221(6)62057614/999"],
["6-62 05 76 14/","+221(6)62057614"],
["6-62 05 76 14","+221(6)62057614"],
["62 05 76 14/999","+221()62057614/999"],
["62 05 76 14/","+221()62057614"],
["62 05 76 14","+221()62057614"],
["631 22 43/999","+221()6312243/999"], ["+55(629)024425/222","+55(0)629-024425/222"],
["631 22 43/","+221()6312243"], ["+55(629)024425/","+55(0)629-024425"],
["631 22 43","+221()6312243"], ["+55(629)024425","+55(0)629-024425"],
["631-22 43/999","+221(631)2243/999"], ["+55 2 098765432/1","+55(0)2-098765432/1"],
["631-22 43/","+221(631)2243"], ["+55 2 098765432/","+55(0)2-098765432"],
["631-22 43","+221(631)2243"], ["+55 2 098765432","+55(0)2-098765432"],
["8291212/33","+221()8291212/33"], ["+55 662 1241 1233/999","+55(0)662-12411233/999"],
["+55 662 1241 1233/","+55(0)662-12411233"],
["047-336-5411/999","+221(047)3365411/999"], ["+55 662 1241 1233","+55(0)662-12411233"],
["047-336-5411/","+221(047)3365411"],
["047-336-5411","+221(047)3365411"],
["047(336)5411/999","+221(047)3365411/999"],
["047(336)5411/","+221(047)3365411"],
["047(336)5411","+221(047)3365411"],
["+221 662 1244 4112/999","+221(662)12444112/999"], ["+55-047-12345678/990","+55(0)047-12345678/990"],
["+221 662 1244 4112/","+221(662)12444112"], ["+55-047-12345678/","+55(0)047-12345678"],
["+221 662 1244 4112","+221(662)12444112"], ["+55-047-12345678","+55(0)047-12345678"]
["+221-047-12345678/990","+221(047)12345678/990"],
["+221-047-12345678/","+221(047)12345678"],
["+221-047-12345678","+221(047)12345678"]
]
for i in input_list:
tel.fromText(coordinate_text=i[0])
self.assertEquals(i[1],tel.asText())
def test_TelephoneInputListForBrazil(self):
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
pref = self.portal_preferences.default_site_preference
pref.setPreferredTelephoneDefaultCountryNumber('55')
pref.setPreferredTelephoneDefaultAreaNumber('2')
pref.enable()
input_list = [
["+55(2)27224896/999","+55(2)27224896/999"],
["+55(2)27224896/","+55(2)27224896"],
["+55(2)27224896","+55(2)27224896"],
["+55()27224896/999","+55(2)27224896/999"],
["+55()27224896/","+55(2)27224896"],
["+55()27224896","+55(2)27224896"],
["+55()-27224896/999","+55(2)27224896/999"],
["+55()-27224896/","+55(2)27224896"],
["+55()-27224896","+55(2)27224896"],
["+(2)27224896/999","+55(2)27224896/999"],
["+(2)27224896/","+55(2)27224896"],
["+(2)27224896","+55(2)27224896"],
["+()27224896/999","+55(2)27224896/999"],
["+()27224896/","+55(2)27224896"],
["+()27224896","+55(2)27224896"],
["+55(629)024425/222","+55(629)024425/222"],
["+55(629)024425/","+55(629)024425"],
["+55(629)024425","+55(629)024425"],
["(22)27224897/333","+55(22)27224897/333"],
["(22)27224897/","+55(22)27224897"],
["(22)27224897","+55(22)27224897"],
["(22)-12345678/222","+55(22)12345678/222"],
["(22)-12345678/","+55(22)12345678"],
["(22)-12345678","+55(22)12345678"],
["+55 2 098765432/1","+55(2)098765432/1"],
["+55 2 098765432/","+55(2)098765432"],
["+55 2 098765432","+55(2)098765432"],
["+55 662 1241 1233/999","+55(662)12411233/999"],
["+55 662 1241 1233/","+55(662)12411233"],
["+55 662 1241 1233","+55(662)12411233"],
["6-62 05 76 14/999","+55(6)62057614/999"],
["6-62 05 76 14/","+55(6)62057614"],
["6-62 05 76 14","+55(6)62057614"],
["62 05 76 14/999","+55(2)62057614/999"],
["62 05 76 14/","+55(2)62057614"],
["62 05 76 14","+55(2)62057614"],
["631 22 43/999","+55(2)6312243/999"],
["631 22 43/","+55(2)6312243"],
["631 22 43","+55(2)6312243"],
["631-22 43/999","+55(631)2243/999"],
["631-22 43/","+55(631)2243"],
["631-22 43","+55(631)2243"],
["8291212/33","+55(2)8291212/33"],
["047-336-5411/999","+55(047)3365411/999"],
["047-336-5411/","+55(047)3365411"],
["047-336-5411","+55(047)3365411"],
["047(336)5411/999","+55(047)3365411/999"],
["047(336)5411/","+55(047)3365411"],
["047(336)5411","+55(047)3365411"],
["+55 662 1244 4112/999","+55(662)12444112/999"],
["+55 662 1244 4112/","+55(662)12444112"],
["+55 662 1244 4112","+55(662)12444112"],
["+55-047-12345678/990","+55(047)12345678/990"],
["+55-047-12345678/","+55(047)12345678"],
["+55-047-12345678","+55(047)12345678"]
] ]
for i in input_list: for i in input_list:
tel.fromText(coordinate_text=i[0]) tel.fromText(coordinate_text=i[0])
self.assertEquals(i[1],tel.asText()) self.assertEquals(i[1],tel.asText())
def test_TelephoneInputListForJapan(self):
pers = self.getPersonModule().newContent(portal_type='Person')
tel = pers.newContent(portal_type='Telephone')
pref = self.portal_preferences.default_site_preference
pref.setPreferredTelephoneDefaultCountryNumber('81')
pref.setPreferredTelephoneDefaultAreaNumber('2')
pref.enable()
input_list = [
["+81(2)27224896/999","+81(2)27224896/999"],
["+81(2)27224896/","+81(2)27224896"],
["+81(2)27224896","+81(2)27224896"],
["+(2)27224896/999","+81(2)27224896/999"],
["+(2)27224896/","+81(2)27224896"],
["+(2)27224896","+81(2)27224896"],
["+()27224896/999","+81(2)27224896/999"],
["+()27224896/","+81(2)27224896"],
["+()27224896","+81(2)27224896"],
["+81(629)024425/222","+81(629)024425/222"],
["+81(629)024425/","+81(629)024425"],
["+81(629)024425","+81(629)024425"],
["(22)27224897/333","+81(22)27224897/333"],
["(22)27224897/","+81(22)27224897"],
["(22)27224897","+81(22)27224897"],
["(22)-12345678/222","+81(22)12345678/222"],
["(22)-12345678/","+81(22)12345678"],
["(22)-12345678","+81(22)12345678"],
["+81 2 098765432/1","+81(2)098765432/1"],
["+81 2 098765432/","+81(2)098765432"],
["+81 2 098765432","+81(2)098765432"],
["+81 662 1241 1233/999","+81(662)12411233/999"],
["+81 662 1241 1233/","+81(662)12411233"],
["+81 662 1241 1233","+81(662)12411233"],
["6-62 05 76 14/999","+81(6)62057614/999"],
["6-62 05 76 14/","+81(6)62057614"],
["6-62 05 76 14","+81(6)62057614"],
["62 05 76 14/999","+81(2)62057614/999"],
["62 05 76 14/","+81(2)62057614"],
["62 05 76 14","+81(2)62057614"],
["631 22 43/999","+81(2)6312243/999"],
["631 22 43/","+81(2)6312243"],
["631 22 43","+81(2)6312243"],
["631-22 43/999","+81(631)2243/999"],
["631-22 43/","+81(631)2243"],
["631-22 43","+81(631)2243"],
["8291212/33","+81(2)8291212/33"],
["047-336-5411/999","+81(047)3365411/999"],
["047-336-5411/","+81(047)3365411"],
["047-336-5411","+81(047)3365411"],
["047(336)5411/999","+81(047)3365411/999"],
["047(336)5411/","+81(047)3365411"],
["047(336)5411","+81(047)3365411"],
["+81 662 1244 4112/999","+81(662)12444112/999"],
["+81 662 1244 4112/","+81(662)12444112"],
["+81 662 1244 4112","+81(662)12444112"],
["+81-047-12345678/990","+81(047)12345678/990"],
["+81-047-12345678/","+81(047)12345678"],
["+81-047-12345678","+81(047)12345678"]
]
for i in input_list:
tel.fromText(coordinate_text=i[0])
self.assertEquals(i[1],tel.asText())
def test_TelephoneWhenTheDefaultCountryAndAreaPreferenceIsBlank(self): def test_TelephoneWhenTheDefaultCountryAndAreaPreferenceIsBlank(self):
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')
......
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