Browse Source
[FIX] We must now inherit from the base model, instead of monkey-patching
pull/115/head
[FIX] We must now inherit from the base model, instead of monkey-patching
pull/115/head
Sylvain GARANCHER
8 years ago
4 changed files with 104 additions and 85 deletions
-
71base_phone/common.py
-
86base_phone/fields.py
-
1base_phone/models/__init__.py
-
31base_phone/models/base.py
@ -0,0 +1,71 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Akretion (http://www.akretion.com) |
||||
|
# Sébastien BEAU <sebastien.beau@akretion.com> |
||||
|
# Alexis de Lattre <alexis.delattre@akretion.com> |
||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
||||
|
|
||||
|
|
||||
|
from . import fields as phone_fields |
||||
|
import logging |
||||
|
_logger = logging.getLogger(__name__) |
||||
|
|
||||
|
try: |
||||
|
import phonenumbers |
||||
|
except ImportError: |
||||
|
_logger.debug('Cannot `import phonenumbers`.') |
||||
|
|
||||
|
|
||||
|
def convert_phone_field(value, country_code): |
||||
|
_logger.debug( |
||||
|
'convert_phone_field value=%s country=%s', value, country_code) |
||||
|
try: |
||||
|
res_parse = phonenumbers.parse( |
||||
|
value, country_code) |
||||
|
_logger.debug('res_parse=%s', res_parse) |
||||
|
new_value = phonenumbers.format_number( |
||||
|
res_parse, phonenumbers.PhoneNumberFormat.E164) |
||||
|
_logger.debug('new_value=%s', new_value) |
||||
|
except: |
||||
|
_logger.error( |
||||
|
"Cannot reformat the phone number '%s' to " |
||||
|
"international format with region=%s", |
||||
|
value, country_code) |
||||
|
new_value = value |
||||
|
return new_value |
||||
|
|
||||
|
|
||||
|
def convert_all_phone_fields(self, vals, fields_to_convert): |
||||
|
loc_vals = vals.copy() |
||||
|
for field in fields_to_convert: |
||||
|
country_key = self._fields[field].country_field |
||||
|
partner_key = self._fields[field].partner_field |
||||
|
country = False |
||||
|
if country_key: |
||||
|
if country_key in loc_vals: |
||||
|
country = self.env['res.country'].browse(vals[country_key]) |
||||
|
else: |
||||
|
country = self[country_key] |
||||
|
if partner_key and not country: |
||||
|
if partner_key in loc_vals: |
||||
|
partner = self.env['res.partner'].browse(vals[partner_key]) |
||||
|
else: |
||||
|
partner = self[partner_key] |
||||
|
if partner: |
||||
|
country = partner.country_id |
||||
|
if not country: |
||||
|
country = self.env.user.company_id.country_id |
||||
|
country_code = False |
||||
|
if country: |
||||
|
country_code = country.code.upper() |
||||
|
if loc_vals[field]: |
||||
|
loc_vals[field] = convert_phone_field( |
||||
|
loc_vals[field], country_code) |
||||
|
return loc_vals |
||||
|
|
||||
|
|
||||
|
def get_phone_fields(self, vals): |
||||
|
fields_to_convert = [] |
||||
|
for key in vals: |
||||
|
if isinstance(self._fields.get(key), phone_fields.Fax): |
||||
|
fields_to_convert.append(key) |
||||
|
return fields_to_convert |
@ -1,5 +1,6 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
from . import base |
||||
from . import res_company |
from . import res_company |
||||
from . import res_partner |
from . import res_partner |
||||
from . import phone_common |
from . import phone_common |
@ -0,0 +1,31 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 SYLEAM |
||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
||||
|
|
||||
|
from .. import common |
||||
|
from odoo import models, api |
||||
|
|
||||
|
|
||||
|
class Base(models.AbstractModel): |
||||
|
_inherit = 'base' |
||||
|
|
||||
|
@api.multi |
||||
|
def write(self, vals): |
||||
|
fields_to_convert = common.get_phone_fields(self, vals) |
||||
|
if fields_to_convert: |
||||
|
for record in self: |
||||
|
loc_vals = common.convert_all_phone_fields( |
||||
|
record, vals, fields_to_convert) |
||||
|
super(Base, record).write(loc_vals) |
||||
|
return True |
||||
|
else: |
||||
|
return super(Base, self).write(vals) |
||||
|
|
||||
|
@api.model |
||||
|
@api.returns('self', lambda value: value.id) |
||||
|
def create(self, vals): |
||||
|
fields_to_convert = common.get_phone_fields(self, vals) |
||||
|
if fields_to_convert: |
||||
|
vals = common.convert_all_phone_fields( |
||||
|
self, vals, fields_to_convert) |
||||
|
return super(Base, self).create(vals) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue