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.

145 lines
4.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Akretion (http://www.akretion.com)
  3. # Sébastien BEAU <sebastien.beau@akretion.com>
  4. # Alexis de Lattre <alexis.delattre@akretion.com>
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import api, fields, models
  7. from operator import attrgetter
  8. import logging
  9. _logger = logging.getLogger(__name__)
  10. try:
  11. import phonenumbers
  12. except ImportError:
  13. _logger.debug('Cannot `import phonenumbers`.')
  14. class Fax(fields.Char):
  15. type = 'fax'
  16. _slots = {
  17. 'country_field': None,
  18. 'partner_field': None,
  19. }
  20. def __init__(
  21. self, string=fields.Default, country_field=fields.Default,
  22. partner_field=fields.Default, **kwargs):
  23. super(Fax, self).__init__(
  24. string=string, country_field=country_field,
  25. partner_field=partner_field, **kwargs)
  26. _related_country_field = property(attrgetter('country_field'))
  27. _related_partner_field = property(attrgetter('partner_field'))
  28. def _setup_regular_full(self, model):
  29. super(Fax, self)._setup_regular_full(model)
  30. assert self.country_field in model._fields or \
  31. self.partner_field in model._fields, \
  32. "field %s with unknown country_field and partner_field" % self
  33. def convert_to_cache(self, value, record, validate=True):
  34. res = super(Fax, self).convert_to_cache(
  35. value, record, validate=validate)
  36. # print 'db value', res
  37. if res:
  38. try:
  39. res_parse = phonenumbers.parse(res)
  40. res = phonenumbers.format_number(
  41. res_parse, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  42. no_break_space = u'\u00A0'
  43. res = res.replace(' ', no_break_space)
  44. except:
  45. pass
  46. # print 'cache value', res
  47. return res
  48. class Phone(Fax):
  49. type = 'phone'
  50. def convert_phone_field(value, country_code):
  51. _logger.debug(
  52. 'convert_phone_field value=%s country=%s', value, country_code)
  53. try:
  54. res_parse = phonenumbers.parse(
  55. value, country_code)
  56. _logger.debug('res_parse=%s', res_parse)
  57. new_value = phonenumbers.format_number(
  58. res_parse, phonenumbers.PhoneNumberFormat.E164)
  59. _logger.debug('new_value=%s', new_value)
  60. except:
  61. _logger.error(
  62. "Cannot reformat the phone number '%s' to "
  63. "international format with region=%s",
  64. value, country_code)
  65. new_value = value
  66. return new_value
  67. def convert_all_phone_fields(self, vals, fields_to_convert):
  68. loc_vals = vals.copy()
  69. for field in fields_to_convert:
  70. country_key = self._fields[field].country_field
  71. partner_key = self._fields[field].partner_field
  72. country = False
  73. if country_key:
  74. if country_key in loc_vals:
  75. country = self.env['res.country'].browse(vals[country_key])
  76. else:
  77. country = self[country_key]
  78. if partner_key and not country:
  79. if partner_key in loc_vals:
  80. partner = self.env['res.partner'].browse(vals[partner_key])
  81. else:
  82. partner = self[partner_key]
  83. if partner:
  84. country = partner.country_id
  85. if not country:
  86. country = self.env.user.company_id.country_id
  87. country_code = False
  88. if country:
  89. country_code = country.code.upper()
  90. if loc_vals[field]:
  91. loc_vals[field] = convert_phone_field(
  92. loc_vals[field], country_code)
  93. return loc_vals
  94. def get_phone_fields(self, vals):
  95. fields_to_convert = []
  96. for key in vals:
  97. if isinstance(self._fields.get(key), Fax):
  98. fields_to_convert.append(key)
  99. return fields_to_convert
  100. original_write = models.Model.write
  101. original_create = models.Model.create
  102. @api.multi
  103. def write(self, vals):
  104. fields_to_convert = get_phone_fields(self, vals)
  105. if fields_to_convert:
  106. for record in self:
  107. loc_vals = convert_all_phone_fields(
  108. record, vals, fields_to_convert)
  109. original_write(record, loc_vals)
  110. return True
  111. else:
  112. return original_write(self, vals)
  113. @api.model
  114. @api.returns('self', lambda value: value.id)
  115. def create(self, vals):
  116. fields_to_convert = get_phone_fields(self, vals)
  117. if fields_to_convert:
  118. vals = convert_all_phone_fields(self, vals, fields_to_convert)
  119. return original_create(self, vals)
  120. models.Model.write = write
  121. models.Model.create = create