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.

61 lines
1.9 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 fields
  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'