[Erp5-report] r21154 - in /erp5/trunk/products/ERP5: Document/ PropertySheet/ tests/
nobody at svn.erp5.org
nobody at svn.erp5.org
Tue May 27 00:05:57 CEST 2008
Author: rafael
Date: Tue May 27 00:05:56 2008
New Revision: 21154
URL: http://svn.erp5.org?rev=21154&view=rev
Log:
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.
Added:
erp5/trunk/products/ERP5/PropertySheet/TelephonePreference.py
Modified:
erp5/trunk/products/ERP5/Document/Telephone.py
erp5/trunk/products/ERP5/tests/testERP5Base.py
Modified: erp5/trunk/products/ERP5/Document/Telephone.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/Telephone.py?rev=21154&r1=21153&r2=21154&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Document/Telephone.py (original)
+++ erp5/trunk/products/ERP5/Document/Telephone.py Tue May 27 00:05:56 2008
@@ -61,14 +61,6 @@
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 @@
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')
Added: erp5/trunk/products/ERP5/PropertySheet/TelephonePreference.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/PropertySheet/TelephonePreference.py?rev=21154&view=auto
==============================================================================
--- erp5/trunk/products/ERP5/PropertySheet/TelephonePreference.py (added)
+++ erp5/trunk/products/ERP5/PropertySheet/TelephonePreference.py Tue May 27 00:05:56 2008
@@ -1,0 +1,58 @@
+#############################################################################
+#
+# Copyright (c) 2008 Nexedi SA and Contributors. All Rights Reserved.
+# Lucas Carvalho Teixeira <lucas at 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' : '',
+ },
+ )
Modified: erp5/trunk/products/ERP5/tests/testERP5Base.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/tests/testERP5Base.py?rev=21154&r1=21153&r2=21154&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/tests/testERP5Base.py (original)
+++ erp5/trunk/products/ERP5/tests/testERP5Base.py Tue May 27 00:05:56 2008
@@ -37,7 +37,6 @@
from Products.ERP5Type.tests.utils import FileUpload
from AccessControl.SecurityManagement import newSecurityManager
-
class TestERP5Base(ERP5TypeTestCase):
"""ERP5 Base tests.
@@ -76,6 +75,7 @@
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 @@
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
More information about the Erp5-report
mailing list