123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from openerp.osv import orm
- from openerp.report import report_sxw
- import phonenumbers
- class base_phone_installed(orm.AbstractModel):
- '''When you use monkey patching, the code is executed when the module
- is in the addons_path of the OpenERP server, even is the module is not
- installed ! In order to avoid the side-effects it can create,
- we create an AbstractModel inside the module and we test the
- availability of this Model in the code of the monkey patching below.
- At Akretion, we call this the "Guewen trick", in reference
- to a trick used by Guewen Baconnier in the "connector" module.
- '''
- _name = "base.phone.installed"
- format_original = report_sxw.rml_parse.format
- def format(
- self, text, oldtag=None, phone=False, phone_format='international'):
- if self.pool.get('base.phone.installed') and phone and text:
-
-
- try:
- phone_number = phonenumbers.parse(text)
- if phone_format == 'international':
- res = phonenumbers.format_number(
- phone_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
- elif phone_format == 'national':
- res = phonenumbers.format_number(
- phone_number, phonenumbers.PhoneNumberFormat.NATIONAL)
- elif phone_format == 'e164':
- res = phonenumbers.format_number(
- phone_number, phonenumbers.PhoneNumberFormat.E164)
- else:
- res = text
- except:
- res = text
- else:
- res = format_original(self, text, oldtag=oldtag)
- return res
- report_sxw.rml_parse.format = format
|