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.

109 lines
4.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2010-2018 Akretion France
  3. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import api, fields, models, _
  6. from odoo.exceptions import UserError, ValidationError
  7. class ResUsers(models.Model):
  8. _inherit = "res.users"
  9. internal_number = fields.Char(
  10. string='Internal Number', copy=False,
  11. help="User's internal phone number.")
  12. dial_suffix = fields.Char(
  13. string='User-specific Dial Suffix',
  14. help="User-specific dial suffix such as aa=2wb for SCCP auto answer.")
  15. callerid = fields.Char(
  16. string='Caller ID', copy=False,
  17. help="Caller ID used for the calls initiated by this user.")
  18. # You'd probably think: Asterisk should reuse the callerID of sip.conf!
  19. # But it cannot, cf
  20. # http://lists.digium.com/pipermail/asterisk-users/
  21. # 2012-January/269787.html
  22. cdraccount = fields.Char(
  23. string='CDR Account',
  24. help="Call Detail Record (CDR) account used for billing this user.")
  25. asterisk_chan_type = fields.Selection([
  26. ('SIP', 'SIP'),
  27. ('PJSIP', 'PJSIP'),
  28. ('IAX2', 'IAX2'),
  29. ('DAHDI', 'DAHDI'),
  30. ('Zap', 'Zap'),
  31. ('Skinny', 'Skinny'),
  32. ('MGCP', 'MGCP'),
  33. ('mISDN', 'mISDN'),
  34. ('H323', 'H323'),
  35. ('SCCP', 'SCCP'),
  36. # Local works for click2dial, but it won't work in
  37. # _get_calling_number() when trying to identify the
  38. # channel of the user, so it's better not to propose it
  39. # ('Local', 'Local'),
  40. ], string='Asterisk Channel Type', default='SIP',
  41. help="Asterisk channel type, as used in the Asterisk dialplan. "
  42. "If the user has a regular IP phone, the channel type is 'SIP'.")
  43. resource = fields.Char(
  44. string='Resource Name', copy=False,
  45. help="Resource name for the channel type selected. For example, "
  46. "if you use 'Dial(SIP/phone1)' in your Asterisk dialplan to ring "
  47. "the SIP phone of this user, then the resource name for this user "
  48. "is 'phone1'. For a SIP phone, the phone number is often used as "
  49. "resource name, but not always.")
  50. alert_info = fields.Char(
  51. string='User-specific Alert-Info SIP Header',
  52. help="Set a user-specific Alert-Info header in SIP request to "
  53. "user's IP Phone for the click2dial feature. If empty, the "
  54. "Alert-Info header will not be added. You can use it to have a "
  55. "special ring tone for click2dial (a silent one !) or to "
  56. "activate auto-answer for example.")
  57. variable = fields.Char(
  58. string='User-specific Variable',
  59. help="Set a user-specific 'Variable' field in the Asterisk "
  60. "Manager Interface 'originate' request for the click2dial "
  61. "feature. If you want to have several variable headers, separate "
  62. "them with '|'.")
  63. asterisk_server_id = fields.Many2one(
  64. 'asterisk.server', string='Asterisk Server',
  65. help="Asterisk server on which the user's phone is connected. "
  66. "If you leave this field empty, it will use the first Asterisk "
  67. "server of the user's company.")
  68. @api.constrains('resource', 'internal_number', 'callerid')
  69. def _check_validity(self):
  70. for user in self:
  71. strings_to_check = [
  72. (_('Resource Name'), user.resource),
  73. (_('Internal Number'), user.internal_number),
  74. (_('Caller ID'), user.callerid),
  75. ]
  76. for check_string in strings_to_check:
  77. if check_string[1]:
  78. try:
  79. check_string[1].encode('ascii')
  80. except UnicodeEncodeError:
  81. raise ValidationError(_(
  82. "The '%s' for the user '%s' should only have "
  83. "ASCII caracters")
  84. % (check_string[0], user.name))
  85. def get_asterisk_server_from_user(self):
  86. '''Returns an asterisk.server recordset'''
  87. self.ensure_one()
  88. # We check if the user has an Asterisk server configured
  89. if self.asterisk_server_id:
  90. ast_server = self.asterisk_server_id
  91. else:
  92. asterisk_servers = self.env['asterisk.server'].search([
  93. '|', ('company_id', '=', self.company_id.id),
  94. ('company_id', '=', False)], order='company_id')
  95. # If the user doesn't have an asterisk server,
  96. # we take the first one of the user's company
  97. if not asterisk_servers:
  98. raise UserError(_(
  99. "No Asterisk server configured for the company '%s'.")
  100. % self.company_id.name)
  101. else:
  102. ast_server = asterisk_servers[0]
  103. return ast_server