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.8 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Base Phone module for OpenERP
  5. # Copyright (C) 2014 Alexis de Lattre <alexis@via.ecp.fr>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm
  22. from openerp.report import report_sxw
  23. import logging
  24. _logger = logging.getLogger(__name__)
  25. try:
  26. import phonenumbers
  27. except ImportError:
  28. _logger.debug('Cannot import phonenumbers')
  29. class base_phone_installed(orm.AbstractModel):
  30. '''When you use monkey patching, the code is executed when the module
  31. is in the addons_path of the OpenERP server, even is the module is not
  32. installed ! In order to avoid the side-effects it can create,
  33. we create an AbstractModel inside the module and we test the
  34. availability of this Model in the code of the monkey patching below.
  35. At Akretion, we call this the "Guewen trick", in reference
  36. to a trick used by Guewen Baconnier in the "connector" module.
  37. '''
  38. _name = "base.phone.installed"
  39. format_original = report_sxw.rml_parse.format
  40. def format(
  41. self, text, oldtag=None, phone=False, phone_format='international'):
  42. if self.pool.get('base.phone.installed') and phone and text:
  43. # text should already be in E164 format, so we don't have
  44. # to give a country code to phonenumbers.parse()
  45. try:
  46. phone_number = phonenumbers.parse(text)
  47. if phone_format == 'international':
  48. res = phonenumbers.format_number(
  49. phone_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  50. elif phone_format == 'national':
  51. res = phonenumbers.format_number(
  52. phone_number, phonenumbers.PhoneNumberFormat.NATIONAL)
  53. elif phone_format == 'e164':
  54. res = phonenumbers.format_number(
  55. phone_number, phonenumbers.PhoneNumberFormat.E164)
  56. else:
  57. res = text
  58. except:
  59. res = text
  60. else:
  61. res = format_original(self, text, oldtag=oldtag)
  62. return res
  63. report_sxw.rml_parse.format = format