You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2010-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import models, api, _
  5. from odoo.exceptions import UserError
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class PhoneCommon(models.AbstractModel):
  9. _inherit = 'phone.common'
  10. @api.model
  11. def click2dial(self, erp_number):
  12. res = super(PhoneCommon, self).click2dial(erp_number)
  13. if not erp_number:
  14. raise UserError(_('Missing phone number'))
  15. user, ast_server, ast_manager = \
  16. self.env['asterisk.server']._connect_to_asterisk()
  17. ast_number = self.convert_to_dial_number(erp_number)
  18. # Add 'out prefix'
  19. if ast_server.out_prefix:
  20. _logger.debug('Out prefix = %s', ast_server.out_prefix)
  21. ast_number = '%s%s' % (ast_server.out_prefix, ast_number)
  22. _logger.debug('Number to be sent to Asterisk = %s', ast_number)
  23. # The user should have a CallerID
  24. if not user.callerid:
  25. raise UserError(_('No callerID configured for the current user'))
  26. variable = []
  27. if user.asterisk_chan_type == 'SIP':
  28. # We can only have one alert-info header in a SIP request
  29. if user.alert_info:
  30. variable.append(
  31. 'SIPAddHeader=Alert-Info: %s' % user.alert_info)
  32. elif ast_server.alert_info:
  33. variable.append(
  34. 'SIPAddHeader=Alert-Info: %s' % ast_server.alert_info)
  35. if user.variable:
  36. for user_variable in user.variable.split('|'):
  37. variable.append(user_variable.strip())
  38. channel = '%s/%s' % (user.asterisk_chan_type, user.resource)
  39. if user.dial_suffix:
  40. channel += '/%s' % user.dial_suffix
  41. try:
  42. ast_manager.Originate(
  43. channel,
  44. context=ast_server.context,
  45. extension=ast_number,
  46. priority=unicode(ast_server.extension_priority),
  47. timeout=unicode(ast_server.wait_time * 1000),
  48. caller_id=user.callerid,
  49. account=user.cdraccount,
  50. variable=variable)
  51. except Exception, e:
  52. _logger.error(
  53. "Error in the Originate request to Asterisk server %s",
  54. ast_server.ip_address)
  55. _logger.error(
  56. "Here are the details of the error: '%s'", unicode(e))
  57. raise UserError(
  58. _("Click to dial with Asterisk failed.\nHere is the error: "
  59. "'%s'")
  60. % unicode(e))
  61. finally:
  62. ast_manager.Logoff()
  63. res['dialed_number'] = ast_number
  64. return res