# Lib for phone number reformating -> pip install phonenumbers
importphonenumbers
# Lib py-asterisk from http://code.google.com/p/py-asterisk/
# We need a version which has this commit : http://code.google.com/p/py-asterisk/source/detail?r=8d0e1c941cce727c702582f3c9fcd49beb4eeaa4
# We need a version which has this commit :
# 8d0e1c941cce727c702582f3c9fcd49beb4eeaa4
# so a version after Nov 20th, 2012
fromAsteriskimportManager
_logger=logging.getLogger(__name__)
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
"""
_name="asterisk.server"
_description="Asterisk Servers"
_columns={
'name':fields.char('Asterisk server name',size=50,required=True,help="Asterisk server name."),
'active':fields.boolean('Active',help="The active field allows you to hide the Asterisk server without deleting it."),
'ip_address':fields.char('Asterisk IP addr. or DNS',size=50,required=True,help="IP address or DNS name of the Asterisk server."),
'port':fields.integer('Port',required=True,help="TCP port on which the Asterisk Manager Interface listens. Defined in /etc/asterisk/manager.conf on Asterisk."),
'out_prefix':fields.char('Out prefix',size=4,help="Prefix to dial to place outgoing calls. If you don't use a prefix to place outgoing calls, leave empty."),
'national_prefix':fields.char('National prefix',size=4,help="Prefix for national phone calls (don't include the 'out prefix'). For e.g., in France, the phone numbers look like '01 41 98 12 42' : the National prefix is '0'."),
'international_prefix':fields.char('International prefix',required=True,size=4,help="Prefix to add to make international phone calls (don't include the 'out prefix'). For e.g., in France, the International prefix is '00'."),
'country_prefix':fields.char('My country prefix',required=True,size=4,help="Phone prefix of the country where the Asterisk server is located. For e.g. the phone prefix for France is '33'. If the phone number to dial starts with the 'My country prefix', OpenERP will remove the country prefix from the phone number and add the 'out prefix' followed by the 'national prefix'. If the phone number to dial doesn't start with the 'My country prefix', OpenERP will add the 'out prefix' followed by the 'international prefix'."),
'login':fields.char('AMI login',size=30,required=True,help="Login that OpenERP will use to communicate with the Asterisk Manager Interface. Refer to /etc/asterisk/manager.conf on your Asterisk server."),
'password':fields.char('AMI password',size=30,required=True,help="Password that Asterisk will use to communicate with the Asterisk Manager Interface. Refer to /etc/asterisk/manager.conf on your Asterisk server."),
'context':fields.char('Dialplan context',size=50,required=True,help="Asterisk dialplan context from which the calls will be made. Refer to /etc/asterisk/extensions.conf on your Asterisk server."),
'wait_time':fields.integer('Wait time (sec)',required=True,help="Amount of time (in seconds) Asterisk will try to reach the user's phone before hanging up."),
'extension_priority':fields.integer('Extension priority',required=True,help="Priority of the extension in the Asterisk dialplan. Refer to /etc/asterisk/extensions.conf on your Asterisk server."),
'alert_info':fields.char('Alert-Info SIP header',size=255,help="Set Alert-Info header in SIP request to user's IP Phone for the click2dial feature. If empty, the Alert-Info header will not be added. You can use it to have a special ring tone for click2dial (a silent one !) or to activate auto-answer for example."),
'company_id':fields.many2one('res.company','Company',help="Company who uses the Asterisk server."),
'name':fields.char(
'Asterisk server name',
size=50,
required=True,
help="Asterisk server name."
),
'active':fields.boolean(
'Active',
help="The active field allows you to hide the Asterisk server "
"without deleting it."
),
'ip_address':fields.char(
'Asterisk IP addr. or DNS',
size=50,
required=True,
help="IP address or DNS name of the Asterisk server."
),
'port':fields.integer(
'Port',
required=True,
help="TCP port on which the Asterisk Manager Interface listens. "
"Defined in /etc/asterisk/manager.conf on Asterisk."
),
'out_prefix':fields.char(
'Out prefix',
size=4,
help="Prefix to dial to place outgoing calls. "
"If you don't use a prefix to place outgoing calls, "
"leave empty."
),
'company_id':fields.many2one(
'res.company',
'Company',
help="Company who uses the Asterisk server."
),
'national_prefix':fields.char(
'National prefix',
size=4,
help="Prefix for national phone calls "
"(don't include the 'out prefix'). "
"For e.g., in France, the phone numbers look like "
"'01 41 98 12 42' : the National prefix is '0'."
),
'international_prefix':fields.char(
'International prefix',
required=True,
size=4,
help="Prefix to add to make international phone calls "
"(don't include the 'out prefix'). "
"For e.g., in France, the International prefix is '00'."
),
'country_prefix':fields.char(
'My country prefix',
required=True,
size=4,
help="Phone prefix of the country where the Asterisk server is "
"located. For e.g. the phone prefix for France is '33'. "
"If the phone number to dial starts with the "
"'My country prefix', OpenERP will remove the country prefix "
"from the phone number and add the 'out prefix' followed by "
"the 'national prefix'. If the phone number to dial doesn't "
"start with the 'My country prefix', OpenERP will add the "
"'out prefix' followed by the 'international prefix'."
),
'login':fields.char(
'AMI login',
size=30,
required=True,
help="Login that OpenERP will use to communicate with the "
"Asterisk Manager Interface. "
"Refer to /etc/asterisk/manager.conf on your Asterisk server."
),
'password':fields.char(
'AMI password',
size=30,
required=True,
help="Password that Asterisk will use to communicate with the "
"Asterisk Manager Interface. "
"Refer to /etc/asterisk/manager.conf on your Asterisk server."
),
'context':fields.char(
'Dialplan context',
size=50,
required=True,
help="Asterisk dialplan context from which the calls will be "
"made. Refer to /etc/asterisk/extensions.conf on your "
"Asterisk server."
),
'wait_time':fields.integer(
'Wait time (sec)',
required=True,
help="Amount of time (in seconds) Asterisk will try to reach the "
"user's phone before hanging up."
),
'extension_priority':fields.integer(
'Extension priority',
required=True,
help="Priority of the extension in the Asterisk dialplan. "
"Refer to /etc/asterisk/extensions.conf on your Asterisk "
"server."
),
'alert_info':fields.char(
'Alert-Info SIP header',
size=255,
help="Set Alert-Info header in SIP request to user's IP Phone for "
"the click2dial feature. If empty, the Alert-Info header "
"will not be added. You can use it to have a special ring "
"tone for click2dial (a silent one !) or to activate "
"auto-answer for example."
),
}
_defaults={
@ -68,7 +167,10 @@ class asterisk_server(osv.osv):
_("You should set a TCP port between 1 and 65535 for the "
"Asterik server '%s'"%server.name)
)
forcheck_stringin[
dialplan_context,
alert_info,
login,
password]:
ifcheck_string[1]:
try:
string=check_string[1].encode('ascii')
check_string[1].encode('ascii')
exceptUnicodeEncodeError:
raiseosv.except_osv(_('Error :'),_("The '%s' should only have ASCII caracters for the Asterisk server '%s'"%(check_string[0],server.name)))
raiseosv.except_osv(
_('Error :'),
_("The '%s' should only have ASCII caracters for "
"the Asterisk server '%s'"
%(check_string[0],server.name))
)
returnTrue
_constraints=[
(_check_validity,"Error message in raise",['out_prefix','country_prefix','national_prefix','international_prefix','wait_time','extension_priority','port','context','alert_info','login','password']),
@ -109,9 +251,8 @@ class asterisk_server(osv.osv):
'''
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_national_format_msg=_("The phone number is not written in valid national format.")
invalid_format_msg=_("The phone number is not written in valid format.")
invalid_format_msg=_("The phone number is not written in valid "
"format.")
# Let's call the variable tmp_number now
tmp_number=erp_number
@ -129,14 +270,17 @@ class asterisk_server(osv.osv):
help="Asterisk channel type, as used in the Asterisk dialplan. If the user has a regular IP phone, the channel type is 'SIP'."),
'resource':fields.char('Resource name',size=64,
help="Resource name for the channel type selected. For example, if you use 'Dial(SIP/phone1)' in your Asterisk dialplan to ring the SIP phone of this user, then the resource name for this user is 'phone1'. For a SIP phone, the phone number is often used as resource name, but not always."),
'alert_info':fields.char('User-specific Alert-Info SIP header',size=255,help="Set a user-specific Alert-Info header in SIP request to user's IP Phone for the click2dial feature. If empty, the Alert-Info header will not be added. You can use it to have a special ring tone for click2dial (a silent one !) or to activate auto-answer for example."),
'variable':fields.char('User-specific Variable',size=255,help="Set a user-specific 'Variable' field in the Asterisk Manager Interface 'originate' request for the click2dial feature. If you want to have several variable headers, separate them with '|'."),
help="Asterisk server on which the user's phone is connected. If you leave this field empty, it will use the first Asterisk server of the user's company."),
],
'Asterisk channel type',
help="Asterisk channel type, as used in the Asterisk dialplan. If "
"the user has a regular IP phone, the channel type is 'SIP'."
),
'resource':fields.char(
'Resource name',
size=64,
help="Resource name for the channel type selected. For example, "
"if you use 'Dial(SIP/phone1)' in your Asterisk dialplan to "
"ring the SIP phone of this user, then the resource name for "
"this user is 'phone1'. For a SIP phone, the phone number "
"is often used as resource name, but not always."
),
'alert_info':fields.char(
'User-specific Alert-Info SIP header',
size=255,
help="Set a user-specific Alert-Info header in SIP request to "
"user's IP Phone for the click2dial feature. If empty, the "
"Alert-Info header will not be added. You can use it to have "
"a special ring tone for click2dial (a silent one !) or to "
"activate auto-answer for example."
),
'variable':fields.char(
'User-specific Variable',
size=255,
help="Set a user-specific 'Variable' field in the Asterisk "
"Manager Interface 'originate' request for the click2dial "
"feature. If you want to have several variable headers, "
"separate them with '|'."
),
'asterisk_server_id':fields.many2one(
'asterisk.server',
'Asterisk server',
help="Asterisk server on which the user's phone is connected. If "
"you leave this field empty, it will use the first Asterisk "
# We need to raise an exception here because, if we pass None as second arg of phonenumbers.parse(), it will raise an exception when you try to enter a phone number in national format... so it's better to raise the exception here
raiseosv.except_osv(_('Error :'),_("You should set a country on the company '%s'"%user.company_id.name))
#print "user_countrycode=", user_countrycode
# We need to raise an exception here because, if we pass None
# as second arg of phonenumbers.parse(), it will raise an
# exception when you try to enter a phone number in national
# format... so it's better to raise the exception here
raiseosv.except_osv(_('Error :'),_('There is no phone number !'))
eliferp_number_displayandnoterp_number_e164:
raiseosv.except_osv(_('Error :'),_("The phone number isn't stored in the standard E.164 format. Try to run the wizard 'Reformat all phone numbers' from the menu Settings > Configuration > Asterisk."))
geolocalisationfeature:forthat,don't use option --server ; only use
--geoloc
"""
__author__="Alexis de Lattre <alexis.delattre@akretion.com>"
@ -69,18 +71,103 @@ from optparse import OptionParser
default_cid_name="Not in OpenERP"
# Define command line options
option_server={'names':('-s','--server'),'dest':'server','type':'string','help':'DNS or IP address of the OpenERP server. Default = none (will not try to connect to OpenERP)','action':'store','default':False}
option_port={'names':('-p','--port'),'dest':'port','type':'int','help':"Port of OpenERP's XML-RPC interface. Default = 8069",'action':'store','default':8069}
option_ssl={'names':('-e','--ssl'),'dest':'ssl','help':"Use XML-RPC secure i.e. with SSL instead of clear XML-RPC. Default = no, use clear XML-RPC",'action':'store_true','default':False}
option_user={'names':('-u','--user-id'),'dest':'user','type':'int','help':"OpenERP user ID to use when connecting to OpenERP. Default = 2",'action':'store','default':2}
option_password={'names':('-w','--password'),'dest':'password','type':'string','help':"Password of the OpenERP user. Default = 'demo'",'action':'store','default':'demo'}
option_ascii={'names':('-a','--ascii'),'dest':'ascii','help':"Convert name from UTF-8 to ASCII. Default = no, keep UTF-8",'action':'store_true','default':False}
option_geoloc={'names':('-g','--geoloc'),'dest':'geoloc','help':"Try to geolocate phone numbers unknown to OpenERP. This features requires the 'phonenumbers' Python lib. To install it, run 'sudo pip install phonenumbers' Default = no",'action':'store_true','default':False}
option_geoloc_lang={'names':('-l','--geoloc-lang'),'dest':'lang','help':"Language in which the name of the country and city name will be displayed by the geolocalisation database. Use the 2 letters ISO code of the language. Default = 'en'",'action':'store','default':"en"}
option_geoloc_country={'names':('-c','--geoloc-country'),'dest':'country','help':"2 letters ISO code for your country e.g. 'FR' for France. This will be used by the geolocalisation system to parse the phone number of the calling party. Default = 'FR'",'action':'store','default':"FR"}
@ -33,24 +33,56 @@ class wizard_open_calling_partner(osv.osv_memory):
_columns={
# I can't set any field to readonly, because otherwize it would call
# default_get (and thus connect to Asterisk) a second time when the user
# clicks on one of the buttons
'calling_number':fields.char('Calling number',size=30,help="Phone number of calling party that has been obtained from Asterisk."),
'partner_address_id':fields.many2one('res.partner.address','Contact name',help="Partner contact related to the calling number. If there is none and you want to update an existing partner"),
'partner_id':fields.many2one('res.partner','Partner',help="Partner related to the calling number."),
'to_update_partner_address_id':fields.many2one('res.partner.address','Contact to update',help="Partner contact on which the phone or mobile number will be written"),
@ -67,22 +102,35 @@ class wizard_open_calling_partner(osv.osv_memory):
res['partner_address_id']=False
res['to_update_partner_address_id']=False
else:
_logger.debug("Could not get the calling number from Asterisk.")
raiseosv.except_osv(_('Error :'),_("Could not get the calling number from Asterisk. Are you currently on the phone ? If yes, check your setup and look at the OpenERP debug logs."))
_logger.debug(
"Could not get the calling number from Asterisk."
)
raiseosv.except_osv(
_('Error :'),
_("Could not get the calling number from Asterisk. Are you "
"currently on the phone ? If yes, check your setup and "
raiseosv.except_osv(_('Error :'),_("The object '%s' is not found in your OpenERP database, probably because the related module is not installed."%oerp_object._description))