Browse Source

Add monkey-patching of format function to add phone=True and phone_format='xxx' arguments.

pull/7/head
Alexis de Lattre 10 years ago
parent
commit
1c98624148
  1. 1
      base_phone/__init__.py
  2. 14
      base_phone/__openerp__.py
  3. 2
      base_phone/base_phone.py
  4. 63
      base_phone/report_sxw_format.py

1
base_phone/__init__.py

@ -21,3 +21,4 @@
from . import base_phone
from . import wizard
from . import report_sxw_format

14
base_phone/__openerp__.py

@ -30,7 +30,19 @@
Base Phone
==========
This module validate phone numbers using the phonenumbers Python library, which is a port of the lib used in Android smartphones. It also adds tel: links on phone numbers.
This module validate phone numbers using the *phonenumbers* Python library, which is a port of the library used in Android smartphones. For example, if your user is linked to a French company and you update the form view of a partner with a badly written French phone number such as '01-55-42-12-42', Odoo will automatically update the phone number to E.164 format '+33155421242' and display in the form view of the partner the readable equivalent '+33 1 55 42 12 42'.
This module also adds *tel:* links on phone numbers and *fax:* links on fax numbers. If you have a softphone or a client software on your PC that is associated with *tel:* links, the softphone should propose you to dial the phone number when you click on such a link.
This module also updates the format() function for reports and adds 2 arguments :
* *phone* : should be True for a phone number, False (default) otherwize.
* *phone_format* : it can have 3 possible values :
* *international* (default) : the report will display '+33 1 55 42 12 42'
* *national* : the report will display '01 55 42 12 42'
* *e164* : the report will display '+33155421242'
For example, in the Sale Order report, to display the phone number of the Salesman, you can write : o.user_id and o.user_id.phone and format(o.user_id.phone, phone=True, phone_format='national') or ''
This module is independant from the Asterisk connector.

2
base_phone/base_phone.py

@ -80,7 +80,6 @@ class phone_common(orm.AbstractModel):
_('Error :'),
_("You should set a country on the company '%s'")
% user.company_id.name)
#print "user_countrycode=", user_countrycode
for field in phonefields:
if vals.get(field):
init_value = vals.get(field)
@ -93,7 +92,6 @@ class phone_common(orm.AbstractModel):
_("Cannot reformat the phone number '%s' to "
"international format. Error message: %s")
% (vals.get(field), e))
#print "res_parse=", res_parse
vals[field] = phonenumbers.format_number(
res_parse, phonenumbers.PhoneNumberFormat.E164)
if init_value != vals[field]:

63
base_phone/report_sxw_format.py

@ -0,0 +1,63 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Base Phone module for OpenERP
# Copyright (C) 2014 Alexis de Lattre <alexis@via.ecp.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
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:
# text should already be in E164 format, so we don't have
# to give a country code to phonenumbers.parse()
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
else:
res = format_original(self, text, oldtag=oldtag)
return res
report_sxw.rml_parse.format = format
Loading…
Cancel
Save