Browse Source

Merge pull request #18 from akretion/8.0-fix-bug-17-no-raise

Fix bug #17: don't raise by default on write/create
pull/16/merge
Alexis de Lattre 10 years ago
parent
commit
3cad044fe1
  1. 3
      asterisk_click2dial/asterisk_click2dial.py
  2. 1
      base_phone/__openerp__.py
  3. 27
      base_phone/base_phone.py
  4. 4
      base_phone/res_partner_view.xml
  5. 25
      base_phone/static/src/js/phone_widget.js
  6. 6
      base_phone/static/src/xml/phone.xml
  7. 37
      base_phone/test/phonenum.yml
  8. 9
      base_phone/wizard/reformat_all_phonenumbers.py

3
asterisk_click2dial/asterisk_click2dial.py

@ -347,7 +347,8 @@ class res_users(orm.Model):
help="Caller ID used for the calls initiated by this user."), help="Caller ID used for the calls initiated by this user."),
# You'd probably think: Asterisk should reuse the callerID of sip.conf! # You'd probably think: Asterisk should reuse the callerID of sip.conf!
# But it cannot, cf # But it cannot, cf
# http://lists.digium.com/pipermail/asterisk-users/2012-January/269787.html
# http://lists.digium.com/pipermail/asterisk-users/
# 2012-January/269787.html
'cdraccount': fields.char( 'cdraccount': fields.char(
'CDR Account', size=50, 'CDR Account', size=50,
help="Call Detail Record (CDR) account used for billing this " help="Call Detail Record (CDR) account used for billing this "

1
base_phone/__openerp__.py

@ -77,6 +77,7 @@ for any help or question about this module.
], ],
'qweb': ['static/src/xml/*.xml'], 'qweb': ['static/src/xml/*.xml'],
'demo': ['base_phone_demo.xml'], 'demo': ['base_phone_demo.xml'],
'test': ['test/phonenum.yml'],
'images': [], 'images': [],
'installable': True, 'installable': True,
} }

27
base_phone/base_phone.py

@ -63,6 +63,8 @@ class phone_common(orm.AbstractModel):
def _generic_reformat_phonenumbers(self, cr, uid, vals, phonefields=None, def _generic_reformat_phonenumbers(self, cr, uid, vals, phonefields=None,
context=None): context=None):
"""Reformat phone numbers in E.164 format i.e. +33141981242""" """Reformat phone numbers in E.164 format i.e. +33141981242"""
if context is None:
context = {}
if phonefields is None: if phonefields is None:
phonefields = [ phonefields = [
'phone', 'partner_phone', 'work_phone', 'fax', 'phone', 'partner_phone', 'work_phone', 'fax',
@ -89,6 +91,12 @@ class phone_common(orm.AbstractModel):
try: try:
res_parse = phonenumbers.parse( res_parse = phonenumbers.parse(
vals.get(field), user_countrycode) vals.get(field), user_countrycode)
vals[field] = phonenumbers.format_number(
res_parse, phonenumbers.PhoneNumberFormat.E164)
if init_value != vals[field]:
_logger.info(
"%s initial value: '%s' updated value: '%s'"
% (field, init_value, vals[field]))
except Exception, e: except Exception, e:
# I do BOTH logger and raise, because: # I do BOTH logger and raise, because:
# raise is usefull when the record is created/written # raise is usefull when the record is created/written
@ -96,19 +104,14 @@ class phone_common(orm.AbstractModel):
# logger is usefull when the record is created/written # logger is usefull when the record is created/written
# via the webservices # via the webservices
_logger.error( _logger.error(
"Cannot reformat the phone number %s to "
"Cannot reformat the phone number '%s' to "
"international format" % vals.get(field)) "international format" % vals.get(field))
raise orm.except_orm(
_('Error:'),
_("Cannot reformat the phone number '%s' to "
"international format. Error message: %s")
% (vals.get(field), e))
vals[field] = phonenumbers.format_number(
res_parse, phonenumbers.PhoneNumberFormat.E164)
if init_value != vals[field]:
_logger.info(
"%s initial value: '%s' updated value: '%s'"
% (field, init_value, vals[field]))
if context.get('raise_if_phone_parse_fails'):
raise orm.except_orm(
_('Error:'),
_("Cannot reformat the phone number '%s' to "
"international format. Error message: %s")
% (vals.get(field), e))
return vals return vals
def get_name_from_phone_number( def get_name_from_phone_number(

4
base_phone/res_partner_view.xml

@ -56,5 +56,9 @@
</field> </field>
</record> </record>
<record id="base.action_partner_form" model="ir.actions.act_window">
<field name="context">{'search_default_customer':1, 'raise_if_phone_parse_fails': True}</field>
</record>
</data> </data>
</openerp> </openerp>

25
base_phone/static/src/js/phone_widget.js

@ -27,9 +27,15 @@ openerp.base_phone = function (instance) {
href = 'tel:' + phone_num; href = 'tel:' + phone_num;
href_text = formatInternational('', phone_num) || ''; href_text = formatInternational('', phone_num) || '';
} }
this.$el.find('a.oe_form_uri').attr('href', href).text(href_text);
if (href_text) {
this.$el.find('a.oe_form_uri').attr('href', href).text(href_text);
this.$el.find('span.oe_form_char_content').text('');
} else {
this.$el.find('a.oe_form_uri').attr('href', '').text('');
this.$el.find('span.oe_form_char_content').text(phone_num || '');
}
var click2dial_text = ''; var click2dial_text = '';
if (phone_num && !this.options.dial_button_invisible) {
if (href_text && !this.options.dial_button_invisible) {
click2dial_text = _t('Dial'); click2dial_text = _t('Dial');
} }
this.$el.find('#click2dial') this.$el.find('#click2dial')
@ -92,10 +98,19 @@ openerp.base_phone = function (instance) {
this._super(); this._super();
} else { } else {
var fax_num = this.get('value'); var fax_num = this.get('value');
//console.log('BASE_PHONE fax_num = %s', fax_num);
var href = '#';
var href_text = '';
if (fax_num) { if (fax_num) {
this.$el.find('a.oe_form_uri')
.attr('href', 'fax:' + fax_num)
.text(formatInternational('', fax_num) || '');
href = 'fax:' + fax_num;
href_text = formatInternational('', fax_num) || '';
}
if (href_text) {
this.$el.find('a.oe_form_uri').attr('href', href).text(href_text);
this.$el.find('span.oe_form_char_content').text('');
} else {
this.$el.find('a.oe_form_uri').attr('href', '').text('');
this.$el.find('span.oe_form_char_content').text(fax_num || '');
} }
} }
}, },

6
base_phone/static/src/xml/phone.xml

@ -10,7 +10,7 @@
<t t-name="FieldPhone"> <t t-name="FieldPhone">
<span class="oe_form_field oe_form_field_email" t-att-style="widget.node.attrs.style"> <span class="oe_form_field oe_form_field_email" t-att-style="widget.node.attrs.style">
<t t-if="widget.get('effective_readonly')"> <t t-if="widget.get('effective_readonly')">
<a href="#" class="oe_form_uri"/>
<a href="#" class="oe_form_uri"/><span class="oe_form_char_content"/>
<!-- <!--
You should add the following "<a .. />" in your IPBX-specific You should add the following "<a .. />" in your IPBX-specific
module (such as asterisk_click2dial) via <t t-extend="FieldPhone"> module (such as asterisk_click2dial) via <t t-extend="FieldPhone">
@ -33,7 +33,9 @@
<t t-name="FieldFax"> <t t-name="FieldFax">
<span class="oe_form_field oe_form_field_email" t-att-style="widget.node.attrs.style"> <span class="oe_form_field oe_form_field_email" t-att-style="widget.node.attrs.style">
<a t-if="widget.get('effective_readonly')" href="#" class="oe_form_uri"/>
<t t-if="widget.get('effective_readonly')">
<a href="#" class="oe_form_uri"/><span class="oe_form_char_content"/>
</t>
<t t-if="!widget.get('effective_readonly')"> <t t-if="!widget.get('effective_readonly')">
<div> <div>
<input type="text" <input type="text"

37
base_phone/test/phonenum.yml

@ -0,0 +1,37 @@
-
Write french phone numbers in national format
-
!record {model: res.partner, id: partner1}:
name: Pierre Paillet
mobile: 06 42 77 42 66
fax: (0) 1 45 42 12 42
-
Write swiss phone numbers in international format
-
!record {model: res.partner, id: partner2}:
name: Joël Grand-Guillaume
parent_id: base.res_partner_12
phone: +41 21 619 10 10
mobile: +41 79 606 42 42
-
Write invalid phone number
-
!record {model: res.partner, id: partner3}:
name: Jean Badphone
phone: 42
-
Check that valid phone numbers have been converted to E.164
-
!python {model: res.partner}: |
partner1 = self.browse(cr, uid, ref('partner1'), context=context)
assert partner1.mobile == '+33642774266', 'Mobile number not written in E.164 format (partner1)'
assert partner1.fax == '+33145421242', 'Fax number not written in E.164 format (partner1)'
partner2 = self.browse(cr, uid, ref('partner2'), context=context)
assert partner2.phone == '+41216191010', 'Phone number not written in E.164 format (partner2)'
assert partner2.mobile == '+41796064242', 'Mobile number not written in E.164 format (partner2)'
-
Check that invalid phone numbers are kept unchanged
-
!python {model: res.partner}: |
partner3 = self.browse(cr, uid, ref('partner3'), context=context)
assert partner3.phone == '42', 'Invalid phone numbers should not be changed'

9
base_phone/wizard/reformat_all_phonenumbers.py

@ -39,6 +39,7 @@ class reformat_all_phonenumbers(orm.TransientModel):
phonenumbers_not_reformatted = '' phonenumbers_not_reformatted = ''
toreformat_dict = self.pool['phone.common']._get_phone_fields( toreformat_dict = self.pool['phone.common']._get_phone_fields(
cr, uid, context=context) cr, uid, context=context)
ctx_raise = dict(context, raise_if_phone_parse_fails=True)
for objname, prop in toreformat_dict.iteritems(): for objname, prop in toreformat_dict.iteritems():
fields = [] fields = []
obj = self.pool[objname] obj = self.pool[objname]
@ -66,7 +67,7 @@ class reformat_all_phonenumbers(orm.TransientModel):
# _generic_reformat_phonenumbers() # _generic_reformat_phonenumbers()
try: try:
obj._generic_reformat_phonenumbers( obj._generic_reformat_phonenumbers(
cr, uid, entry, context=context)
cr, uid, entry, context=ctx_raise)
except Exception, e: except Exception, e:
name = obj.name_get( name = obj.name_get(
cr, uid, [init_entry['id']], context=context)[0][1] cr, uid, [init_entry['id']], context=context)[0][1]
@ -98,4 +99,8 @@ class reformat_all_phonenumbers(orm.TransientModel):
{'phonenumbers_not_reformatted': phonenumbers_not_reformatted}, {'phonenumbers_not_reformatted': phonenumbers_not_reformatted},
context=context) context=context)
logger.info('End of the phone number reformatting wizard') logger.info('End of the phone number reformatting wizard')
return True
action = self.pool['ir.actions.act_window'].for_xml_id(
cr, uid, 'base_phone', 'reformat_all_phonenumbers_action',
context=context)
action['res_id'] = ids[0]
return action
Loading…
Cancel
Save