From 94269758c5727c75264833f316e280fbda1e7fda Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Sat, 22 Sep 2012 00:34:27 +0200 Subject: [PATCH] Now uses the phonenumber lib to reformat numbers in "clean" format and store them in E.164 format. Start to modify the code to take advantage of this great lib. Thanks to Ludovic Gasc for pointing this lib to me during PyconFR 2012 in Paris ! Fix a bug with Open Calling Partner when CallerID has non-ASCII chars. Move all code of wizard to wizard directory. IMPORTANT : when you upgrade to this revision, run the wizard "Reformat all phone numbers" from Settings > Configuration > Asterisk. --- asterisk_click2dial/__init__.py | 2 +- asterisk_click2dial/__openerp__.py | 2 + asterisk_click2dial/asterisk_click2dial.py | 380 ++++++------------ asterisk_click2dial/asterisk_server_view.xml | 3 +- asterisk_click2dial/res_partner_view.xml | 56 --- asterisk_click2dial/wizard/__init__.py | 23 ++ .../wizard/open_calling_partner.py | 218 ++++++++++ .../wizard/open_calling_partner_view.xml | 68 ++++ .../wizard/reformat_all_phonenumbers.py | 63 +++ .../wizard/reformat_all_phonenumbers_view.xml | 39 ++ 10 files changed, 539 insertions(+), 315 deletions(-) create mode 100644 asterisk_click2dial/wizard/__init__.py create mode 100644 asterisk_click2dial/wizard/open_calling_partner.py create mode 100644 asterisk_click2dial/wizard/open_calling_partner_view.xml create mode 100644 asterisk_click2dial/wizard/reformat_all_phonenumbers.py create mode 100644 asterisk_click2dial/wizard/reformat_all_phonenumbers_view.xml diff --git a/asterisk_click2dial/__init__.py b/asterisk_click2dial/__init__.py index 49993e0..36872e9 100644 --- a/asterisk_click2dial/__init__.py +++ b/asterisk_click2dial/__init__.py @@ -20,4 +20,4 @@ ############################################################################## import asterisk_click2dial - +import wizard diff --git a/asterisk_click2dial/__openerp__.py b/asterisk_click2dial/__openerp__.py index d49e8c1..3c14565 100644 --- a/asterisk_click2dial/__openerp__.py +++ b/asterisk_click2dial/__openerp__.py @@ -55,6 +55,8 @@ A detailed documentation for this module is available on the Akretion Web site : 'asterisk_server_view.xml', 'res_users_view.xml', 'res_partner_view.xml', + 'wizard/open_calling_partner_view.xml', + 'wizard/reformat_all_phonenumbers_view.xml', 'security/asterisk_server_security.xml', ], 'demo_xml': ['asterisk_click2dial_demo.xml'], diff --git a/asterisk_click2dial/asterisk_click2dial.py b/asterisk_click2dial/asterisk_click2dial.py index f33622c..bc38751 100644 --- a/asterisk_click2dial/asterisk_click2dial.py +++ b/asterisk_click2dial/asterisk_click2dial.py @@ -26,8 +26,8 @@ import socket import logging # Lib to translate error messages from tools.translate import _ -# Lib for regexp -import re +# Lib for phone number reformating -> pip install phonenumbers +import phonenumbers _logger = logging.getLogger(__name__) @@ -44,7 +44,6 @@ class asterisk_server(osv.osv): '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'."), - 'national_format_allowed': fields.boolean('National format allowed ?', help="Do we allow to use click2dial on phone numbers written in national format, e.g. 01 41 98 12 42, or only in the international format, e.g. +33 1 41 98 12 42 ?"), '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."), @@ -120,53 +119,40 @@ class asterisk_server(osv.osv): if not tmp_number: raise osv.except_osv(error_title_msg, invalid_format_msg) - # treat (0) as a special condition as we dont want an extra 0 to be inserted in the number - # FIXME all 0s after the country prefix should be stripped off - tmp_number = tmp_number.replace('(0)','') - - # First, we remove all stupid caracters and spaces - for char_to_remove in [' ', '.', '(', ')', '[', ']', '-', '/']: - tmp_number = tmp_number.replace(char_to_remove, '') - # Before starting to use prefix, we convert empty prefix whose value # is False to an empty string - country_prefix = (ast_server.country_prefix or '') - national_prefix = (ast_server.national_prefix or '') - international_prefix = (ast_server.international_prefix or '') - out_prefix = (ast_server.out_prefix or '') - + country_prefix = ast_server.country_prefix or '' + national_prefix = ast_server.national_prefix or '' + international_prefix = ast_server.international_prefix or '' + out_prefix = ast_server.out_prefix or '' + + # Maybe one day we will use + # phonenumbers.format_out_of_country_calling_number(phonenumbers.parse(' Configuration > Asterisk") + # If I raise an exception here, it won't be possible to install + # the module on a DB with bad phone numbers + #raise osv.except_osv(_('Error :'), _("Cannot reformat the phone number '%s' to E.164 format. Error message: %s" % (addr.get(fromfield), e))) + res = False + result[addr['id']][tofield] = res + #print "RESULT _format_phonenumber_to_e164", result + return result + + + _columns = { + 'phone_e164': fields.function(_format_phonenumber_to_e164, type='char', size=64, string='Phone in E.164 format', readonly=True, multi="e164", store={ + 'res.partner.address': (lambda self, cr, uid, ids, c={}: ids, ['phone'], 10), + }), + 'mobile_e164': fields.function(_format_phonenumber_to_e164, type='char', size=64, string='Mobile in E.164 format', readonly=True, multi="e164", store={ + 'res.partner.address': (lambda self, cr, uid, ids, c={}: ids, ['mobile'], 10), + }), + 'fax_e164': fields.function(_format_phonenumber_to_e164, type='char', size=64, string='Fax in E.164 format', readonly=True, multi="e164", store={ + 'res.partner.address': (lambda self, cr, uid, ids, c={}: ids, ['fax'], 10), + }), + } + + def _reformat_phonenumbers(self, cr, uid, vals, context=None): + """Reformat phone numbers in international format i.e. +33141981242""" + phonefields = ['phone', 'fax', 'mobile'] + if any([vals.get(field) for field in phonefields]): + user = self.pool.get('res.users').browse(cr, uid, uid, context=context) + # country_id on res.company is a fields.function that looks at + # company_id.partner_id.addres(default).country_id + if user.company_id.country_id: + user_countrycode = user.company_id.country_id.code + else: + # 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 + raise osv.except_osv(_('Error :'), _("You should set a country on the company '%s'" % user.company_id.name)) + #print "user_countrycode=", user_countrycode + for field in phonefields: + if vals.get(field): + try: + res_parse = phonenumbers.parse(vals.get(field), user_countrycode) + except Exception, e: + raise osv.except_osv(_('Error :'), _("Cannot reformat the phone number '%s' to international format. Error message: %s" % (vals.get(field), e))) + #print "res_parse=", res_parse + vals[field] = phonenumbers.format_number(res_parse, phonenumbers.PhoneNumberFormat.INTERNATIONAL) + return vals + + + def create(self, cr, uid, vals, context=None): + vals_reformated = self._reformat_phonenumbers(cr, uid, vals, context=context) + return super(res_partner_address, self).create(cr, uid, vals_reformated, context=context) + + + def write(self, cr, uid, ids, vals, context=None): + vals_reformated = self._reformat_phonenumbers(cr, uid, vals, context=context) + return super(res_partner_address, self).write(cr, uid, ids, vals_reformated, context=context) + + + def dial(self, cr, uid, ids, phone_field=['phone', 'phone_e164'], context=None): '''Read the number to dial and call _connect_to_asterisk the right way''' - erp_number = self.read(cr, uid, ids, [phone_field], context=context)[0][phone_field] + erp_number_read = self.read(cr, uid, ids[0], phone_field, context=context) + erp_number_e164 = erp_number_read[phone_field[1]] + erp_number_display = erp_number_read[phone_field[0]] # Check if the number to dial is not empty - if not erp_number: + if not erp_number_display: raise osv.except_osv(_('Error :'), _('There is no phone number !')) - options = {'erp_number': erp_number} + elif erp_number_display and not erp_number_e164: + raise osv.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.")) + options = {'erp_number': erp_number_e164} return self.pool.get('asterisk.server')._connect_to_asterisk(cr, uid, method='dial', options=options, context=context) def action_dial_phone(self, cr, uid, ids, context=None): '''Function called by the button 'Dial' next to the 'phone' field in the partner address view''' - return self.dial(cr, uid, ids, phone_field='phone', context=context) + return self.dial(cr, uid, ids, phone_field=['phone', 'phone_e164'], context=context) def action_dial_mobile(self, cr, uid, ids, context=None): '''Function called by the button 'Dial' next to the 'mobile' field in the partner address view''' - return self.dial(cr, uid, ids, phone_field='mobile', context=context) + return self.dial(cr, uid, ids, phone_field=['mobile', 'mobile_e164'], context=context) def get_name_from_phone_number(self, cr, uid, number, context=None): @@ -452,7 +514,6 @@ class res_partner_address(osv.osv): def get_partner_from_phone_number(self, cr, uid, number, context=None): - res = {} # We check that "number" is really a number if not isinstance(number, str): return False @@ -460,214 +521,21 @@ class res_partner_address(osv.osv): return False _logger.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.debug(u"Answer get_name_from_phone_number with name = %s" % entry.name) - return (entry.id, entry.partner_id.id, entry.name) - if entry.mobile: - if re.sub(r'\D', '', entry.mobile).endswith(number): - _logger.debug(u"Answer get_name_from_phone_number with name = %s" % entry.name) - return (entry.id, entry.partner_id.id, entry.name) - - _logger.debug(u"No match for phone number %s" % number) - return False - -res_partner_address() - - -class wizard_open_calling_partner(osv.osv_memory): - _name = "wizard.open.calling.partner" - _description = "Open calling partner" - - _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"), - 'current_phone': fields.related('to_update_partner_address_id', 'phone', type='char', relation='res.partner.address', string='Current phone'), - 'current_mobile': fields.related('to_update_partner_address_id', 'mobile', type='char', relation='res.partner.address', string='Current mobile'), - } - - - def default_get(self, cr, uid, fields, context=None): - '''Thanks to the default_get method, we are able to query Asterisk and - get the corresponding partner when we launch the wizard''' - res = {} - calling_number = self.pool.get('asterisk.server')._connect_to_asterisk(cr, uid, method='get_calling_number', context=context) - #To test the code without Asterisk server - #calling_number = "0141981242" - if calling_number: - res['calling_number'] = calling_number - # We match only on the end of the phone number - if len(calling_number) >= 9: - number_to_search = calling_number[-9:len(calling_number)] - else: - number_to_search = calling_number - partner = self.pool.get('res.partner.address').get_partner_from_phone_number(cr, uid, number_to_search, context=context) - if partner: - res['partner_address_id'] = partner[0] - res['partner_id'] = partner[1] - else: - res['partner_id'] = False - res['partner_address_id'] = False - res['to_update_partner_address_id'] = False - else: - _logger.debug("Could not get the calling number from Asterisk.") - raise osv.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.")) - - return res - - - def open_filtered_object(self, cr, uid, ids, oerp_object, context=None): - '''Returns the action that opens the list view of the 'oerp_object' - given as argument filtered on the partner''' - # This module only depends on "base" - # and I don't want to add a dependancy on "sale" or "account" - # So I just check here that the model exists, to avoid a crash - if not self.pool.get('ir.model').search(cr, uid, [('model', '=', oerp_object._name)], context=context): - raise osv.except_osv(_('Error :'), _("The object '%s' is not found in your OpenERP database, probably because the related module is not installed." % oerp_object._description)) - - partner = self.read(cr, uid, ids[0], ['partner_id'], context=context)['partner_id'] - if partner: - action = { - 'name': oerp_object._description, - 'view_type': 'form', - 'view_mode': 'tree,form', - 'res_model': oerp_object._name, - 'type': 'ir.actions.act_window', - 'nodestroy': False, # close the pop-up wizard after action - 'target': 'current', - 'domain': [('partner_id', '=', partner[0])], - } - return action + # We try to match a phone or mobile number with the same end + pg_seach_number = '%' + number + res_ids = self.search(cr, uid, ['|', ('phone_e164', 'ilike', pg_seach_number), ('mobile_e164', 'ilike', pg_seach_number)], context=context) + # TODO : use is_number_match() of the phonenumber lib ? + if len(res_ids) > 1: + _logger.warning(u"There are several partners addresses (IDS = %s) with the same phone number %s" % (str(res_ids), number)) + if res_ids: + entry = self.read(cr, uid, res_ids[0], ['name', 'partner_id'], context=context) + _logger.debug(u"Answer get_partner_from_phone_number with name = %s" % entry['name']) + return (entry['id'], entry['partner_id'] and entry['partner_id'][0] or False, entry['name']) else: + _logger.debug(u"No match for phone number %s" % number) return False - - def open_sale_orders(self, cr, uid, ids, context=None): - '''Function called by the related button of the wizard''' - return self.open_filtered_object(cr, uid, ids, self.pool.get('sale.order'), context=context) - - - def open_invoices(self, cr, uid, ids, context=None): - '''Function called by the related button of the wizard''' - return self.open_filtered_object(cr, uid, ids, self.pool.get('account.invoice'), context=context) - - - def simple_open(self, cr, uid, ids, object_name='res.partner', context=None): - if object_name == 'res.partner': - field = 'partner_id' - label = 'Partner' - elif object_name == 'res.partner.address': - field = 'partner_address_id' - label = 'Contact' - else: - raise osv.except_osv(_('Error :'), "This object '%s' is not supported" % object_name) - record_to_open = self.read(cr, uid, ids[0], [field], context=context)[field] - if record_to_open: - return { - 'name': label, - 'view_type': 'form', - 'view_mode': 'form,tree', - 'res_model': object_name, - 'type': 'ir.actions.act_window', - 'nodestroy': False, # close the pop-up wizard after action - 'target': 'current', - 'res_id': record_to_open[0], - } - else: - return False - - - def open_partner(self, cr, uid, ids, context=None): - '''Function called by the related button of the wizard''' - return self.simple_open(cr, uid, ids, object_name='res.partner', context=context) - - - def open_partner_address(self, cr, uid, ids, context=None): - '''Function called by the related button of the wizard''' - return self.simple_open(cr, uid, ids, object_name='res.partner.address', context=context) - - - def create_partner_address(self, cr, uid, ids, phone_type='phone', context=None): - '''Function called by the related button of the wizard''' - calling_number = self.read(cr, uid, ids[0], ['calling_number'], context=context)['calling_number'] - user = self.pool.get('res.users').browse(cr, uid, uid, context=context) - ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, user, context=context) - # Convert the number to the international format - number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, calling_number, ast_server, context=context) - - new_partner_address_id = self.pool.get('res.partner.address').create(cr, uid, {phone_type: number_to_write}, context=context) - action = { - 'name': 'Create new contact', - 'view_type': 'form', - 'view_mode': 'form,tree', - 'res_model': 'res.partner.address', - 'type': 'ir.actions.act_window', - 'nodestroy': False, - 'target': 'current', - 'res_id': new_partner_address_id, - } - return action - - - def create_partner_address_phone(self, cr, uid, ids, context=None): - return self.create_partner_address(cr, uid, ids, phone_type='phone', context=context) - - - def create_partner_address_mobile(self, cr, uid, ids, context=None): - return self.create_partner_address(cr, uid, ids, phone_type='mobile', context=context) - - - def update_partner_address(self, cr, uid, ids, phone_type='mobile', context=None): - cur_wizard = self.browse(cr, uid, ids[0], context=context) - if not cur_wizard.to_update_partner_address_id: - raise osv.except_osv(_('Error :'), _("Select the contact to update.")) - user = self.pool.get('res.users').browse(cr, uid, uid, context=context) - ast_server = self.pool.get('asterisk.server')._get_asterisk_server_from_user(cr, uid, user, context=context) - number_to_write = self.pool.get('asterisk.server')._convert_number_to_international_format(cr, uid, cur_wizard.calling_number, ast_server, context=context) - self.pool.get('res.partner.address').write(cr, uid, cur_wizard.to_update_partner_address_id.id, {phone_type: number_to_write}, context=context) - action = { - 'name': 'Contact: ' + cur_wizard.to_update_partner_address_id.name, - 'view_type': 'form', - 'view_mode': 'form,tree', - 'res_model': 'res.partner.address', - 'type': 'ir.actions.act_window', - 'nodestroy': False, - 'target': 'current', - 'res_id': cur_wizard.to_update_partner_address_id.id - } - return action - - - def update_partner_address_phone(self, cr, uid, ids, context=None): - return self.update_partner_address(cr, uid, ids, phone_type='phone', context=context) - - - def update_partner_address_mobile(self, cr, uid, ids, context=None): - return self.update_partner_address(cr, uid, ids, phone_type='mobile', context=context) - - - def onchange_to_update_partner_address(self, cr, uid, ids, to_update_partner_address_id, context=None): - res = {} - res['value'] = {} - if to_update_partner_address_id: - to_update_partner_address = self.pool.get('res.partner.address').browse(cr, uid, to_update_partner_address_id, context=context) - res['value'].update({'current_phone': to_update_partner_address.phone, - 'current_mobile': to_update_partner_address.mobile}) - else: - res['value'].update({'current_phone': False, 'current_mobile': False}) - return res - -wizard_open_calling_partner() +res_partner_address() # This module supports multi-company diff --git a/asterisk_click2dial/asterisk_server_view.xml b/asterisk_click2dial/asterisk_server_view.xml index c61e210..23511fb 100644 --- a/asterisk_click2dial/asterisk_server_view.xml +++ b/asterisk_click2dial/asterisk_server_view.xml @@ -43,7 +43,6 @@ - @@ -76,7 +75,7 @@ - + diff --git a/asterisk_click2dial/res_partner_view.xml b/asterisk_click2dial/res_partner_view.xml index e8702cb..3a94d96 100644 --- a/asterisk_click2dial/res_partner_view.xml +++ b/asterisk_click2dial/res_partner_view.xml @@ -80,62 +80,6 @@ - - - - view_open_calling_partner - wizard.open.calling.partner - form - -
- - - - - -