From fdefb7b9148d7d61c65aee2f0ffd128d549922cf Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 29 Dec 2010 00:43:29 +0100 Subject: [PATCH] Much better function get_name_from_phone_number : - we now handle the fact that phone numbers may contain spaces or special chars such in OpenERP, which is often the case in real life - check input number --- asterisk_click2dial/asterisk_click2dial.py | 36 ++++++++++++++-------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/asterisk_click2dial/asterisk_click2dial.py b/asterisk_click2dial/asterisk_click2dial.py index 3544a65..8f8358a 100644 --- a/asterisk_click2dial/asterisk_click2dial.py +++ b/asterisk_click2dial/asterisk_click2dial.py @@ -26,6 +26,8 @@ import socket import netsvc # Lib to translate error messages from tools.translate import _ +# Lib for regexp +import re class asterisk_server(osv.osv): @@ -304,20 +306,30 @@ class res_partner_address(osv.osv): ''' res = {} logger = netsvc.Logger() - netsvc.Logger().notifyChannel('asterisk_click2dial', netsvc.LOG_DEBUG, u"Call get_name_from_phone_number with number = " + number) - res = self.search(cr, uid, [('phone', 'ilike', number)], context=context) - if len(res) == 0: # if we find nothing, we continue with mobile - res = self.search(cr, uid, [('mobile', 'ilike', number)], context=context) - if len(res) == 1: # if we have a single match - #print 'AST res =', res - partner_address = self.read(cr, uid, res[0], ['name'], context=context)['name'] - logger.notifyChannel('asterisk_click2dial', netsvc.LOG_DEBUG, u"Answer get_name_from_phone_number with name = %s" % partner_address) - return partner_address - # TODO what do we do when we have more than 1 match ? We take the first one ? - # we don't do anything ? - else: + # We check that "number" is really a number + if not isinstance(number, str): + return False + if not number.isdigit(): return False + netsvc.Logger().notifyChannel('asterisk_click2dial', netsvc.LOG_DEBUG, u"Call get_name_from_phone_number with number = %s" % number) + # Get all the partner addresses : + all_ids = self.search(cr, uid, [], context=context) + # For each partner address, we check if the number matches on the "phone" or "mobile" fields + for entry in self.browse(cr, uid, all_ids, context=context): + if entry.phone: + # We use a regexp on the phone field to remove non-digit caracters + if re.sub(r'\D', '', entry.phone).endswith(number): + logger.notifyChannel('asterisk_click2dial', netsvc.LOG_DEBUG, u"Answer get_name_from_phone_number with name = %s" % entry.name) + return entry.name + if entry.mobile: + if re.sub(r'\D', '', entry.mobile).endswith(number): + logger.notifyChannel('asterisk_click2dial', netsvc.LOG_DEBUG, u"Answer get_name_from_phone_number with name = %s" % entry.name) + return entry.name + + logger.notifyChannel('asterisk_click2dial', netsvc.LOG_DEBUG, u"No match for phone number %s" % number) + return False + res_partner_address()