From 42e34dfdcdaee4250c41e8a84b5e923930ceb3f2 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 20 May 2016 00:48:26 +0200 Subject: [PATCH] Port crm_phone to v9 Code cleanup, fixes, small enhancements The code to extract the calling party number from the list of channels is now inheritable --- asterisk_click2dial/asterisk_click2dial.py | 54 ++++++---- asterisk_click2dial/asterisk_server_view.xml | 5 +- asterisk_click2dial/res_users_view.xml | 4 +- .../static/src/js/asterisk_click2dial.js | 4 +- asterisk_click2dial_crm/__init__.py | 21 ---- asterisk_click2dial_crm/__openerp__.py | 48 --------- base_phone/res_company_view.xml | 9 +- base_phone/res_partner_view.xml | 11 +- base_phone/res_users_view.xml | 9 +- base_phone/web_phone.xml | 9 +- base_phone/wizard/number_not_found.py | 12 +-- base_phone/wizard/number_not_found_view.xml | 11 +- crm_phone/__openerp__.py | 30 +----- crm_phone/crm_phone.py | 38 ++----- crm_phone/crm_view.xml | 39 +------ crm_phone/res_users_view.xml | 9 +- crm_phone/wizard/__init__.py | 19 ---- crm_phone/wizard/create_crm_phonecall.py | 32 ++---- crm_phone/wizard/number_not_found.py | 101 ++++++------------ crm_phone/wizard/number_not_found_view.xml | 14 ++- 20 files changed, 137 insertions(+), 342 deletions(-) delete mode 100644 asterisk_click2dial_crm/__init__.py delete mode 100644 asterisk_click2dial_crm/__openerp__.py diff --git a/asterisk_click2dial/asterisk_click2dial.py b/asterisk_click2dial/asterisk_click2dial.py index adce5a9..36b613b 100644 --- a/asterisk_click2dial/asterisk_click2dial.py +++ b/asterisk_click2dial/asterisk_click2dial.py @@ -5,6 +5,7 @@ from openerp import models, fields, api, _ from openerp.exceptions import UserError, ValidationError import logging +from pprint import pformat try: # pip install py-Asterisk @@ -167,37 +168,44 @@ class AsteriskServer(models.Model): "the Asterisk Manager Interface.")) @api.model - def _get_calling_number(self): + def _get_calling_number_from_channel(self, chan, user): + '''Method designed to be inherited to work with + very old or very new versions of Asterisk''' + sip_account = user.asterisk_chan_type + '/' + user.resource + internal_number = user.internal_number + # 4 = Ring + # 6 = Up + if ( + chan.get('ChannelState') in ('4', '6') and ( + chan.get('ConnectedLineNum') == internal_number or + chan.get('EffectiveConnectedLineNum') == internal_number or + sip_account in chan.get('BridgedChannel', ''))): + _logger.debug( + "Found a matching Event with channelstate = %s", + chan.get('ChannelState')) + return chan.get('CallerIDNum') + # Compatibility with Asterisk 1.4 + if ( + chan.get('State') == 'Up' and + sip_account in chan.get('Link', '')): + _logger.debug("Found a matching Event in 'Up' state") + return chan.get('CallerIDNum') + return False + @api.model + def _get_calling_number(self): user, ast_server, ast_manager = self._connect_to_asterisk() calling_party_number = False try: list_chan = ast_manager.Status() # from pprint import pprint # pprint(list_chan) - _logger.debug("Result of Status AMI request: %s", list_chan) + _logger.debug("Result of Status AMI request:") + _logger.debug(pformat(list_chan)) for chan in list_chan.values(): - sip_account = user.asterisk_chan_type + '/' + user.resource - # 4 = Ring - if ( - chan.get('ChannelState') == '4' and - chan.get('ConnectedLineNum') == user.internal_number): - _logger.debug("Found a matching Event in 'Ring' state") - calling_party_number = chan.get('CallerIDNum') - break - # 6 = Up - if ( - chan.get('ChannelState') == '6' and - sip_account in chan.get('BridgedChannel', '')): - _logger.debug("Found a matching Event in 'Up' state") - calling_party_number = chan.get('CallerIDNum') - break - # Compatibility with Asterisk 1.4 - if ( - chan.get('State') == 'Up' and - sip_account in chan.get('Link', '')): - _logger.debug("Found a matching Event in 'Up' state") - calling_party_number = chan.get('CallerIDNum') + calling_party_number = self._get_calling_number_from_channel( + chan, user) + if calling_party_number: break except Exception, e: _logger.error( diff --git a/asterisk_click2dial/asterisk_server_view.xml b/asterisk_click2dial/asterisk_server_view.xml index d095a82..1226f9c 100644 --- a/asterisk_click2dial/asterisk_server_view.xml +++ b/asterisk_click2dial/asterisk_server_view.xml @@ -1,8 +1,7 @@ diff --git a/asterisk_click2dial/res_users_view.xml b/asterisk_click2dial/res_users_view.xml index e133c56..31f74c9 100644 --- a/asterisk_click2dial/res_users_view.xml +++ b/asterisk_click2dial/res_users_view.xml @@ -1,7 +1,7 @@ diff --git a/asterisk_click2dial/static/src/js/asterisk_click2dial.js b/asterisk_click2dial/static/src/js/asterisk_click2dial.js index 4a19f5a..d00dda9 100644 --- a/asterisk_click2dial/static/src/js/asterisk_click2dial.js +++ b/asterisk_click2dial/static/src/js/asterisk_click2dial.js @@ -5,12 +5,12 @@ odoo.define('asterisk_click2dial.click2dial', function (require) { "use strict"; -var _t = core._t; var UserMenu = require('web.UserMenu'); var WebClient = require('web.WebClient'); var web_client = require('web.web_client'); var Widget = require('web.Widget'); var core = require('web.core'); +var _t = core._t; var click2dial = {}; @@ -71,7 +71,7 @@ click2dial.OpenCaller = Widget.extend({ this._super.apply(this, arguments); this.update_promise.then(function() { var asterisk_button = new click2dial.OpenCaller(); - //console.log(this); + // attach the phone logo/button to the systray asterisk_button.appendTo($('.oe_systray')); }); }, diff --git a/asterisk_click2dial_crm/__init__.py b/asterisk_click2dial_crm/__init__.py deleted file mode 100644 index 676d5d5..0000000 --- a/asterisk_click2dial_crm/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# Asterisk click2dial CRM module for OpenERP -# Copyright (c) 2012-2014 Akretion (http://www.akretion.com) -# @author: Alexis de Lattre -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## diff --git a/asterisk_click2dial_crm/__openerp__.py b/asterisk_click2dial_crm/__openerp__.py deleted file mode 100644 index 459cd98..0000000 --- a/asterisk_click2dial_crm/__openerp__.py +++ /dev/null @@ -1,48 +0,0 @@ -# -*- encoding: utf-8 -*- -############################################################################## -# -# Asterisk click2dial CRM module for OpenERP -# Copyright (c) 2012-2014 Akretion (http://www.akretion.com) -# @author: Alexis de Lattre -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## - -{ - "name": "Asterisk Click2dial CRM", - "version": "8.0.0.1.0", - "author": "Akretion,Odoo Community Association (OCA)", - "website": "http://www.akretion.com", - "license": "AGPL-3", - "category": "Phone", - "description": """ -Asterisk Click2dial CRM -======================= - -This module is *EMPTY* ; so you should uninstall it now. -The code that used to be in this module has been moved to the module -*crm_phone* that is available in the same GitHub repository -https://github.com/OCA/connector-telephony - -This module will be removed from the repository in the near future. -""", - "depends": [ - 'asterisk_click2dial', - 'crm_phone', - ], - "data": [], - 'installable': False, - "application": False, -} diff --git a/base_phone/res_company_view.xml b/base_phone/res_company_view.xml index 5921a4c..9a1e962 100644 --- a/base_phone/res_company_view.xml +++ b/base_phone/res_company_view.xml @@ -1,12 +1,11 @@ - + @@ -30,4 +29,4 @@ - + diff --git a/base_phone/res_partner_view.xml b/base_phone/res_partner_view.xml index b3b0d22..b58ad6b 100644 --- a/base_phone/res_partner_view.xml +++ b/base_phone/res_partner_view.xml @@ -1,11 +1,10 @@ - + @@ -31,8 +30,8 @@ - {'search_default_customer':1, 'raise_if_phone_parse_fails': True} + {'search_default_customer': 1, 'raise_if_phone_parse_fails': True} - + diff --git a/base_phone/res_users_view.xml b/base_phone/res_users_view.xml index 37043bb..f266e02 100644 --- a/base_phone/res_users_view.xml +++ b/base_phone/res_users_view.xml @@ -1,11 +1,10 @@ - + @@ -37,4 +36,4 @@ - + diff --git a/base_phone/web_phone.xml b/base_phone/web_phone.xml index 0187a42..01bfb52 100644 --- a/base_phone/web_phone.xml +++ b/base_phone/web_phone.xml @@ -1,11 +1,10 @@ - + @@ -21,4 +20,4 @@ - + diff --git a/base_phone/wizard/number_not_found.py b/base_phone/wizard/number_not_found.py index 510a2c1..32daadd 100644 --- a/base_phone/wizard/number_not_found.py +++ b/base_phone/wizard/number_not_found.py @@ -59,8 +59,8 @@ class NumberNotFound(models.TransientModel): if not res: res = {} if res.get('calling_number'): - convert = self.env['res.partner']._generic_reformat_phonenumbers( - None, {'phone': res.get('calling_number')}) + convert = self.env['res.partner']._reformat_phonenumbers_create( + {'phone': res.get('calling_number')}) parsed_num = phonenumbers.parse(convert.get('phone')) res['e164_number'] = phonenumbers.format_number( parsed_num, phonenumbers.PhoneNumberFormat.INTERNATIONAL) @@ -93,13 +93,13 @@ class NumberNotFound(models.TransientModel): @api.multi def update_partner(self): + self.ensure_one() wiz = self[0] if not wiz.to_update_partner_id: raise exceptions.Warning( _('Error'), _('Select the Partner to Update.')) - self.env['res.partner'].write( - wiz.to_update_partner_id.id, + wiz.to_update_partner_id.write( {wiz.number_type: wiz.e164_number}) action = { 'name': _('Partner: %s' % wiz.to_update_partner_id.name), @@ -115,5 +115,5 @@ class NumberNotFound(models.TransientModel): @api.onchange('to_update_partner_id') def onchange_to_update_partner(self): - self.current_partner_phone = self.to_update_partner.phone or False - self.current_partner_mobile = self.to_update_partner.mobile or False + self.current_partner_phone = self.to_update_partner_id.phone or False + self.current_partner_mobile = self.to_update_partner_id.mobile or False diff --git a/base_phone/wizard/number_not_found_view.xml b/base_phone/wizard/number_not_found_view.xml index 24d951e..0df07e0 100644 --- a/base_phone/wizard/number_not_found_view.xml +++ b/base_phone/wizard/number_not_found_view.xml @@ -1,17 +1,18 @@ - + + number.not.found.form number.not.found -
+