From e004ba958249515a4a4d457accbbcdd20e7fbedf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bartek=20G=C3=B3rny?= <bartek@gorny.edu.pl>
Date: Fri, 1 Jun 2007 15:49:51 +0000
Subject: [PATCH] a little improvement in parsing telephone number given as
 text, and in returning number as text

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@14672 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Telephone.py | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/product/ERP5/Document/Telephone.py b/product/ERP5/Document/Telephone.py
index 1d9490b7c7..4ce8087455 100644
--- a/product/ERP5/Document/Telephone.py
+++ b/product/ERP5/Document/Telephone.py
@@ -63,7 +63,11 @@ class Telephone(Coordinate, Base):
 
     # The standard parser is used to read phone numbers
     # written in a standard syntax
-    standard_parser = re.compile('\+(.*)\((.*)\)(.*)\-(.*)')
+    # +[country]([area])[number]/[extension]
+    # or in syntax retured by asText
+    # +[country](0)[area]-[number]/[extension]
+    standard_parser = re.compile('\+(?P<country>\d{,2})\(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
@@ -81,15 +85,19 @@ class Telephone(Coordinate, Base):
         
         if coordinate_text is None:
             coordinate_text = ''
-        if self.standard_parser.match(coordinate_text):
-            (country, temp, area, number) = \
-                self.standard_parser.match(coordinate_text).groups()
-        else:
-            country = area = ''
-            number = 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()
+        self.log(number_dict)
+        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_number = number, 
+                  telephone_extension = extension)
 
     security.declareProtected(Permissions.ModifyPortalContent, '_setText')
     _setText = fromText
@@ -115,9 +123,13 @@ class Telephone(Coordinate, Base):
 
         text += '-'
         telephone_number = self.getTelephoneNumber()
-        if self.telephone_number is not None:
+        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
-- 
2.30.9