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.

63 lines
2.6 KiB

  1. # -*- coding: 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 import models
  22. from openerp.report import report_sxw
  23. import phonenumbers
  24. class BasePhoneInstalled(models.AbstractModel):
  25. '''When you use monkey patching, the code is executed when the module
  26. is in the addons_path of the OpenERP server, even is the module is not
  27. installed ! In order to avoid the side-effects it can create,
  28. we create an AbstractModel inside the module and we test the
  29. availability of this Model in the code of the monkey patching below.
  30. At Akretion, we call this the "Guewen trick", in reference
  31. to a trick used by Guewen Baconnier in the "connector" module.
  32. '''
  33. _name = "base.phone.installed"
  34. format_original = report_sxw.rml_parse.format
  35. def format(
  36. self, text, oldtag=None, phone=False, phone_format='international'):
  37. if self.pool.get('base.phone.installed') and phone and text:
  38. # text should already be in E164 format, so we don't have
  39. # to give a country code to phonenumbers.parse()
  40. phone_number = phonenumbers.parse(text)
  41. if phone_format == 'international':
  42. res = phonenumbers.format_number(
  43. phone_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
  44. elif phone_format == 'national':
  45. res = phonenumbers.format_number(
  46. phone_number, phonenumbers.PhoneNumberFormat.NATIONAL)
  47. elif phone_format == 'e164':
  48. res = phonenumbers.format_number(
  49. phone_number, phonenumbers.PhoneNumberFormat.E164)
  50. else:
  51. res = text
  52. else:
  53. res = format_original(self, text, oldtag=oldtag)
  54. return res
  55. report_sxw.rml_parse.format = format