# Lib required to open a socket (needed to communicate with Asterisk server)
# Lib required to open a socket (needed to communicate with Asterisk server)
importsocket
importsocket
# Lib required to print logs
# Lib required to print logs
importnetsvc
importlogging
# Lib to translate error messages
# Lib to translate error messages
fromtools.translateimport_
fromtools.translateimport_
# Lib for regexp
# Lib for regexp
importre
importre
_logger=logging.getLogger(__name__)
classasterisk_server(osv.osv):
classasterisk_server(osv.osv):
'''Asterisk server object, to store all the parameters of the Asterisk IPBXs'''
'''Asterisk server object, to store all the parameters of the Asterisk IPBXs'''
@ -106,7 +107,6 @@ class asterisk_server(osv.osv):
theOpenERPnumbers.
theOpenERPnumbers.
'''
'''
logger=netsvc.Logger()
error_title_msg=_("Invalid phone number")
error_title_msg=_("Invalid phone number")
invalid_international_format_msg=_("The phone number is not written in valid international format. Example of valid international format : +33 1 41 98 12 42")
invalid_international_format_msg=_("The phone number is not written in valid international format. Example of valid international format : +33 1 41 98 12 42")
invalid_national_format_msg=_("The phone number is not written in valid national format.")
invalid_national_format_msg=_("The phone number is not written in valid national format.")
@ -114,7 +114,7 @@ class asterisk_server(osv.osv):
# Let's call the variable tmp_number now
# Let's call the variable tmp_number now
tmp_number=erp_number
tmp_number=erp_number
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,'Number before reformat = %s'%tmp_number)
_logger.debug('Number before reformat = %s'%tmp_number)
# Check if empty
# Check if empty
ifnottmp_number:
ifnottmp_number:
@ -135,24 +135,24 @@ class asterisk_server(osv.osv):
iftmp_number[0]=='+':
iftmp_number[0]=='+':
# Remove the starting '+' of the number
# Remove the starting '+' of the number
tmp_number=tmp_number.replace('+','')
tmp_number=tmp_number.replace('+','')
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,'Number after removal of special char = %s'%tmp_number)
_logger.debug('Number after removal of special char = %s'%tmp_number)
# At this stage, 'tmp_number' should only contain digits
# At this stage, 'tmp_number' should only contain digits
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,'National prefix = %s - Number with national prefix = %s'%(national_prefix,tmp_number))
_logger.debug('National prefix = %s - Number with national prefix = %s'%(national_prefix,tmp_number))
else:
else:
# If the number is an international number,
# If the number is an international number,
# add 'international prefix'
# add 'international prefix'
tmp_number=international_prefix+tmp_number
tmp_number=international_prefix+tmp_number
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,'International prefix = %s - Number with international prefix = %s'%(international_prefix,tmp_number))
_logger.debug('International prefix = %s - Number with international prefix = %s'%(international_prefix,tmp_number))
# National format, allowed
# National format, allowed
elifast_server.national_format_allowed:
elifast_server.national_format_allowed:
@ -166,7 +166,7 @@ class asterisk_server(osv.osv):
# Add 'out prefix' to all numbers
# Add 'out prefix' to all numbers
tmp_number=out_prefix+tmp_number
tmp_number=out_prefix+tmp_number
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,'Out prefix = %s - Number to be sent to Asterisk = %s'%(out_prefix,tmp_number))
_logger.debug('Out prefix = %s - Number to be sent to Asterisk = %s'%(out_prefix,tmp_number))
returntmp_number
returntmp_number
@ -187,7 +187,6 @@ class asterisk_server(osv.osv):
andsendinstructionstoDialtoAsterisk.That's the important function !
andsendinstructionstoDialtoAsterisk.That's the important function !
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,'Successfull logout from Asterisk : %s'%logout_answer)
_logger.debug('Successfull logout from Asterisk : %s'%logout_answer)
else:
else:
logger.notifyChannel('click2dial',netsvc.LOG_WARNING,'Logout from Asterisk failed : %s'%logout_answer)
_logger.warning('Logout from Asterisk failed : %s'%logout_answer)
# we catch only network problems here
# we catch only network problems here
exceptsocket.error:
exceptsocket.error:
logger.notifyChannel('click2dial',netsvc.LOG_WARNING,"Click2dial failed : unable to connect to Asterisk")
_logger.warning("Click2dial failed : unable to connect to Asterisk")
raiseosv.except_osv(_('Error :'),_("The connection from OpenERP to the Asterisk server failed. Please check the configuration on OpenERP and on Asterisk."))
raiseosv.except_osv(_('Error :'),_("The connection from OpenERP to the Asterisk server failed. Please check the configuration on OpenERP and on Asterisk."))
finally:
finally:
sock.close()
sock.close()
logger.notifyChannel('click2dial',netsvc.LOG_INFO,"Asterisk Click2Dial from %s/%s to %s"%(user.asterisk_chan_type,user.internal_number,ast_number))
_logger.info("Asterisk Click2Dial from %s/%s to %s"%(user.asterisk_chan_type,user.internal_number,ast_number))
returnTrue
returnTrue
@ -359,14 +356,13 @@ class res_partner_address(osv.osv):
request.
request.
'''
'''
res={}
res={}
logger=netsvc.Logger()
# We check that "number" is really a number
# We check that "number" is really a number
ifnotisinstance(number,str):
ifnotisinstance(number,str):
returnFalse
returnFalse
ifnotnumber.isdigit():
ifnotnumber.isdigit():
returnFalse
returnFalse
netsvc.Logger().notifyChannel('click2dial',netsvc.LOG_DEBUG,u"Call get_name_from_phone_number with number = %s"%number)
_logger.debug(u"Call get_name_from_phone_number with number = %s"%number)
# Get all the partner addresses :
# Get all the partner addresses :
all_ids=self.search(cr,uid,[],context=context)
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 each partner address, we check if the number matches on the "phone" or "mobile" fields
@ -374,14 +370,14 @@ class res_partner_address(osv.osv):
ifentry.phone:
ifentry.phone:
# We use a regexp on the phone field to remove non-digit caracters
# We use a regexp on the phone field to remove non-digit caracters
ifre.sub(r'\D','',entry.phone).endswith(number):
ifre.sub(r'\D','',entry.phone).endswith(number):
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,u"Answer get_name_from_phone_number with name = %s"%entry.name)
_logger.debug(u"Answer get_name_from_phone_number with name = %s"%entry.name)
returnentry.name
returnentry.name
ifentry.mobile:
ifentry.mobile:
ifre.sub(r'\D','',entry.mobile).endswith(number):
ifre.sub(r'\D','',entry.mobile).endswith(number):
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,u"Answer get_name_from_phone_number with name = %s"%entry.name)
_logger.debug(u"Answer get_name_from_phone_number with name = %s"%entry.name)
returnentry.name
returnentry.name
logger.notifyChannel('click2dial',netsvc.LOG_DEBUG,u"No match for phone number %s"%number)
_logger.debug(u"No match for phone number %s"%number)