From e474a444cda2df1186fc67c6dbf72f132612077e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Calonne?= <aurel@nexedi.com>
Date: Mon, 26 Jan 2009 10:25:36 +0000
Subject: [PATCH] defined the city area in telephone numbers add regexp for it
 modify unit test to take into account this new property add new preference
 property to defined preferred city area

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25305 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Telephone.py            | 34 +++++++--
 product/ERP5/PropertySheet/Telephone.py       |  4 +
 .../ERP5/PropertySheet/TelephonePreference.py |  7 ++
 product/ERP5/tests/testERP5Base.py            | 73 ++++++++++---------
 4 files changed, 76 insertions(+), 42 deletions(-)

diff --git a/product/ERP5/Document/Telephone.py b/product/ERP5/Document/Telephone.py
index 84ef07a82f..b79f306ff3 100644
--- a/product/ERP5/Document/Telephone.py
+++ b/product/ERP5/Document/Telephone.py
@@ -32,7 +32,7 @@ from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
 from Products.ERP5Type.Base import Base
 
 from Products.ERP5.Document.Coordinate import Coordinate
-
+from zLOG import LOG
 import re
 
 class Telephone(Coordinate, Base):
@@ -71,6 +71,12 @@ class Telephone(Coordinate, Base):
   # The list is a priority list,
   # be carefull to add a new regex.
   regex_list = [
+    # Country, Area, City, Number, Extension*
+    "\+(?P<country>[\d ]+)(?P<toto>\(0\)|\ |\-)(?P<area>\d+)(\-|\ )(?P<city>\d+)(\-|\ )(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
+
+    # Area, City, Number, Extension*
+    "^(\(0\)|0)?(?P<area>\d+)(\-|\ |\/)(?P<city>\d+)(\-|\ )(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
+
     # Country, Area, Number, Extension*
     # +11(0)1-11111111/111      or +11(0)1-11111111/      or +11(0)1-11111111
     # +11(0)1-1111-1111/111      or +11(0)1-1111-1111/      or +11(0)1-1111-1111
@@ -78,6 +84,7 @@ class Telephone(Coordinate, Base):
     # +11 (0)11 1011 1100/111   or +11 (0)11 1011 1100/   or +11 (0)11 1011 1100
     "\+(?P<country>[\d\ ]*)\(0\)(?P<area>\d+)(\-|\ )(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
 
+
     # Country, Area, Number, Extension*
     # +11-1-11111111/111 or +11-1-11111111/ or +11-1-11111111
     "\+(?P<country>\d+)-(?P<area>\d+)-(?P<number>[\d\ \-]*)(?:\/)?(?P<ext>\d+|)",
@@ -215,7 +222,7 @@ class Telephone(Coordinate, Base):
     # (11-1) 1.111.111/111  or (11-1) 1.111.111/  or (11-1) 1.111.111
     "\((\+|)(?P<country>\d+)\-(?P<area>\d+)\)(\ |\-|)(?P<number>[\d\ \-\.]*)(?:\/)?(?P<ext>\d+|)",
 
-    # Country, area, number and extersion*
+    # Country, area, number and extension*
     # + 111-11-1110111/111 or + 111-11-1110111/ or + 111-11-1110111
     # +111-11-1110111/111  or +111-11-1110111/  or +111-11-1110111
     # +111/1/1111 1100/111 or +111/1/1111 1100/ or +111/1/1111 1100
@@ -263,14 +270,15 @@ class Telephone(Coordinate, Base):
 
     country = number_dict.get('country','')
     area = number_dict.get('area','')
+    city = number_dict.get('city','')
     number = number_dict.get('number','')
     extension = number_dict.get('ext','')
-
     if ((country in ['', None]) and \
         (area in ['', None]) and \
+        (city in ['', None]) and \
         (number in ['', None]) and \
         (extension in ['', None])):
-      country = area = number = extension = ''
+      country = area = city = number = extension = ''
     else:
       # Trying to get the country and area from dict, 
       # but if it fails must be get from preference
@@ -279,9 +287,12 @@ class Telephone(Coordinate, Base):
         country = preference_tool.getPreferredTelephoneDefaultCountryNumber('')
       if area in ['', None]:
         area = preference_tool.getPreferredTelephoneDefaultAreaNumber('')
+      if city in ['', None]:
+        city = preference_tool.getPreferredTelephoneDefaultCityNumber('')
 
       country =  country.strip()
       area = area.strip()
+      city = city.strip()
       number = number.strip()
       extension = extension.strip()
 
@@ -293,6 +304,7 @@ class Telephone(Coordinate, Base):
 
     self.edit(telephone_country = country,
               telephone_area = area,
+              telephone_city = city,
               telephone_number = number, 
               telephone_extension = extension)
    
@@ -310,6 +322,7 @@ class Telephone(Coordinate, Base):
     
     country = self.getTelephoneCountry('')
     area = self.getTelephoneArea('')
+    city = self.getTelephoneCity('')
     number = self.getTelephoneNumber('')
     extension = self.getTelephoneExtension('')
    
@@ -317,6 +330,7 @@ class Telephone(Coordinate, Base):
     # the method should to return blank.
     if ((country == '') and \
         (area == '') and \
+        (city == '') and \
         (number == '') and \
         (extension == '')): 
       return ''
@@ -326,6 +340,10 @@ class Telephone(Coordinate, Base):
     if notation not in [None, '']:
       notation = notation.replace('<country>',country)
       notation = notation.replace('<area>',area)
+      if city == "":
+        notation = notation.replace('<city>-', '')
+      else:        
+        notation = notation.replace('<city>',city)
       notation = notation.replace('<number>',number)
       notation = notation.replace('<ext>',extension)
 
@@ -349,6 +367,10 @@ class Telephone(Coordinate, Base):
     telephone_area = self.getTelephoneArea()
     if telephone_area is not None:
       url_string += telephone_area
+      
+    telephone_city = self.getTelephoneCity()
+    if telephone_city is not None:
+      url_string += telephone_city
 
     telephone_number = self.getTelephoneNumber()
     if telephone_number is not None:
@@ -373,8 +395,8 @@ class Telephone(Coordinate, Base):
       Returns the notation that will be used by asText method.
     """
     # The notation can be changed.
-    # But needs to have <country>, <area>, <number> and <ext>
-    return "+<country>(0)<area>-<number>/<ext>"
+    # But needs to have <country>, <area>, <city>, <number> and <ext>
+    return "+<country>(0)<area>-<city>-<number>/<ext>"
 
   def _getRegexList(self):
     """
diff --git a/product/ERP5/PropertySheet/Telephone.py b/product/ERP5/PropertySheet/Telephone.py
index 10c04133a8..90c66744ba 100644
--- a/product/ERP5/PropertySheet/Telephone.py
+++ b/product/ERP5/PropertySheet/Telephone.py
@@ -42,6 +42,10 @@ class Telephone:
             'description' : 'Area indicative number',
             'type'        : 'string',
             'mode'        : 'w' },
+        {   'id'          : 'telephone_city',
+            'description' : 'City indicative number',
+            'type'        : 'string',
+            'mode'        : 'w' },
         {   'id'          : 'telephone_number',
             'description' : 'Telephone number minus indicatives',
             'type'        : 'string',
diff --git a/product/ERP5/PropertySheet/TelephonePreference.py b/product/ERP5/PropertySheet/TelephonePreference.py
index 8bbba998bf..d15a434d98 100644
--- a/product/ERP5/PropertySheet/TelephonePreference.py
+++ b/product/ERP5/PropertySheet/TelephonePreference.py
@@ -48,6 +48,13 @@ class TelephonePreference:
       'mode'        : 'w',
       'default'     : '',
     },
+    { 'id'          : 'preferred_telephone_default_city_number',
+      'description' : 'The default city number.',
+      'type'        : 'string',
+      'preference'  : 1,
+      'mode'        : 'w',
+      'default'     : '',
+    },
     { 'id'          : 'preferred_telephone_default_region',
       'description' : 'The default region.',
       'type'        : 'string',
diff --git a/product/ERP5/tests/testERP5Base.py b/product/ERP5/tests/testERP5Base.py
index bb802dcc8e..6acca1b5f7 100644
--- a/product/ERP5/tests/testERP5Base.py
+++ b/product/ERP5/tests/testERP5Base.py
@@ -914,7 +914,7 @@ class TestERP5Base(ERP5TypeTestCase):
       ['+11(0)-1111111/111', '+11(0)-1111111/111'],
       ['+11(0)-1111111/', '+11(0)-1111111'],
       ['+11(0)-1111111', '+11(0)-1111111'],
-      ['+11 111 1111 1111/111', '+11(0)111-11111111/111'],
+      ['+11 111 1111 1111/111', '+11(0)111-1111-1111/111'],
       ['+11(1)11111111/', '+11(0)1-11111111'],
       ['+11(1)11111111', '+11(0)1-11111111'],
       ['+11()11111111/111', '+11(0)-11111111/111'],
@@ -948,29 +948,29 @@ class TestERP5Base(ERP5TypeTestCase):
       ['(11)-11111111', '+(0)11-11111111'],
       ['+11 1 011111111/1', '+11(0)1-011111111/1'],
       ['+11 1 011111111', '+11(0)1-011111111'],
-      ['+11 111 1111 1111/', '+11(0)111-11111111'],
-      ['1-11 01 11 11/111', '+(0)1-11011111/111'],
-      ['1-11 01 11 11/', '+(0)1-11011111'],
-      ['1-11 01 11 11', '+(0)1-11011111'],
-      ['11 01 11 11/111', '+(0)-11011111/111'],
-      ['11 01 11 11/', '+(0)-11011111'],
-      ['11 01 11 11', '+(0)-11011111'],
-      ['111 11 11/111', '+(0)-1111111/111'],
-      ['111 11 11/', '+(0)-1111111'],
-      ['111 11 11', '+(0)-1111111'],
-      ['111-11 11/111', '+(0)111-1111/111'],
-      ['111-11 11/', '+(0)111-1111'],
-      ['111-11 11', '+(0)111-1111'],
+      ['+11 111 1111 1111/', '+11(0)111-1111-1111'],
+      ['1-11 01 11 11/111', '+(0)1-11-011111/111'],
+      ['1-11 01 11 11/', '+(0)1-11-011111'],
+      ['1-11 01 11 11', '+(0)1-11-011111'],
+      ['11 01 11 11/111', '+(0)11-01-1111/111'],
+      ['11 01 11 11/', '+(0)11-01-1111'],
+      ['11 01 11 11', '+(0)11-01-1111'],
+      ['111 11 11/111', '+(0)111-11-11/111'],
+      ['111 11 11/', '+(0)111-11-11'],
+      ['111 11 11', '+(0)111-11-11'],
+      ['111-11 11/111', '+(0)111-11-11/111'],
+      ['111-11 11/', '+(0)111-11-11'],
+      ['111-11 11', '+(0)111-11-11'],
       ['1111111/11', '+(0)-1111111/11'],
-      ['011-111-1111/111', '+(0)11-1111111/111'],
-      ['011-111-1111/', '+(0)11-1111111'],
-      ['011-111-1111', '+(0)11-1111111'],
+      ['011-111-1111/111', '+(0)11-111-1111/111'],
+      ['011-111-1111/', '+(0)11-111-1111'],
+      ['011-111-1111', '+(0)11-111-1111'],
       ['011(111)1111/111', '+(0)11-1111111/111'],
       ['011(111)1111/', '+(0)11-1111111'],
       ['011(111)1111', '+(0)11-1111111'],
-      ['111/111-1111/111', '+(0)111-1111111/111'],
-      ['111/111-1111/', '+(0)111-1111111'],
-      ['111/111-1111', '+(0)111-1111111'],
+      ['111/111-1111/111', '+(0)111-111-1111/111'],
+      ['111/111-1111/', '+(0)111-111-1111'],
+      ['111/111-1111', '+(0)111-111-1111'],
       ['+11 111111111/111', '+11(0)-111111111/111'],
       ['+11 111111111/', '+11(0)-111111111'],
       ['+11 111111111', '+11(0)-111111111'],
@@ -983,12 +983,12 @@ class TestERP5Base(ERP5TypeTestCase):
       ['111.111.1111/111', '+(0)111-1111111/111'],
       ['111.111.1111/', '+(0)111-1111111'],
       ['111.111.1111', '+(0)111-1111111'],
-      ['+ 11 (0)1-11 11 01 01/111', '+11(0)1-11110101/111'],
-      ['+ 11 (0)1-11 11 01 01/', '+11(0)1-11110101'],
-      ['+ 11 (0)1-11 11 01 01', '+11(0)1-11110101'],
-      ['+11-1 11 11 01 11/111', '+11(0)1-11110111/111'],
-      ['+11-1 11 11 01 11/', '+11(0)1-11110111'],
-      ['+11-1 11 11 01 11', '+11(0)1-11110111'],
+      ['+ 11 (0)1-11 11 01 01/111', '+11(0)1-11-110101/111'],
+      ['+ 11 (0)1-11 11 01 01/', '+11(0)1-11-110101'],
+      ['+ 11 (0)1-11 11 01 01', '+11(0)1-11-110101'],
+      ['+11-1 11 11 01 11/111', '+11(0)1-11-110111/111'],
+      ['+11-1 11 11 01 11/', '+11(0)1-11-110111'],
+      ['+11-1 11 11 01 11', '+11(0)1-11-110111'],
       ['+111 (0) 1 111 11011/111', '+111(0)1-11111011/111'],
       ['+111 (0) 1 111 11011/', '+111(0)1-11111011'],
       ['+111 (0) 1 111 11011', '+111(0)1-11111011'],
@@ -1001,12 +1001,12 @@ class TestERP5Base(ERP5TypeTestCase):
       ['+111 101011111/111', '+111(0)-101011111/111'],
       ['+111 101011111/', '+111(0)-101011111'],
       ['+111 101011111', '+111(0)-101011111'],
-      ['+11 (0)11 1011 1100/111', '+11(0)11-10111100/111'],
-      ['+11 (0)11 1011 1100/', '+11(0)11-10111100'],
-      ['+11 (0)11 1011 1100', '+11(0)11-10111100'],
-      ['+11 (0)10 1101 1111/111', '+11(0)10-11011111/111'],
-      ['+11 (0)10 1101 1111/', '+11(0)10-11011111'],
-      ['+11 (0)10 1101 1111', '+11(0)10-11011111'],
+      ['+11 (0)11 1011 1100/111', '+11(0)11-1011-1100/111'],
+      ['+11 (0)11 1011 1100/', '+11(0)11-1011-1100'],
+      ['+11 (0)11 1011 1100', '+11(0)11-1011-1100'],
+      ['+11 (0)10 1101 1111/111', '+11(0)10-1101-1111/111'],
+      ['+11 (0)10 1101 1111/', '+11(0)10-1101-1111'],
+      ['+11 (0)10 1101 1111', '+11(0)10-1101-1111'],
       ['(111 11) 111111/111', '+111(0)11-111111/111'],
       ['(111 11) 111111/', '+111(0)11-111111'],
       ['(111 11) 111111', '+111(0)11-111111'],
@@ -1058,9 +1058,9 @@ class TestERP5Base(ERP5TypeTestCase):
       ['+10 (111) 110 11 11/111', '+10(0)111-1101111/111'],
       ['+10 (111) 110 11 11/', '+10(0)111-1101111'],
       ['+10 (111) 110 11 11', '+10(0)111-1101111'],
-      ['+ 111 1 1101 101/111', '+111(0)1-1101101/111'],
-      ['+ 111 1 1101 101/', '+111(0)1-1101101'],
-      ['+ 111 1 1101 101', '+111(0)1-1101101'],
+      ['+ 111 1 1101 101/111', '+111(0)1-1101-101/111'],
+      ['+ 111 1 1101 101/', '+111(0)1-1101-101'],
+      ['+ 111 1 1101 101', '+111(0)1-1101-101'],
       ['+11 1111-1111/111', '+11(0)-11111111/111'],
       ['+11 1111-1111/', '+11(0)-11111111'],
       ['+11 1111-1111', '+11(0)-11111111'],
@@ -1076,11 +1076,12 @@ class TestERP5Base(ERP5TypeTestCase):
       ['+111/1/1111 1100/111', '+111(0)1-11111100/111'],
       ['+111/1/1111 1100/', '+111(0)1-11111100'],
       ['+111/1/1111 1100', '+111(0)1-11111100'],
-      ['+11(0)11-1111-1111/111', '+11(0)11-11111111/111'],
+      ['+11(0)11-1111-1111/111', '+11(0)11-1111-1111/111'],
     ]
 
     for i in input_list:
       tel.fromText(coordinate_text=i[0])
+      print i
       self.assertEquals(i[1],tel.asText())
 
   def test_TelephoneWhenTheDefaultCountryAndAreaPreferenceIsBlank(self):
-- 
2.30.9