Browse Source

Merge pull request #115 from syleam/10.0-fix-create-write-inheritance

[FIX] We must now inherit from the base model, instead of monkey-patching
pull/109/head
Alexis de Lattre 8 years ago
committed by GitHub
parent
commit
6a1708dd2f
  1. 71
      base_phone/common.py
  2. 86
      base_phone/fields.py
  3. 1
      base_phone/models/__init__.py
  4. 31
      base_phone/models/base.py

71
base_phone/common.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

86
base_phone/fields.py

@ -5,7 +5,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models
from odoo import fields
from operator import attrgetter from operator import attrgetter
import logging import logging
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -59,87 +59,3 @@ class Fax(fields.Char):
class Phone(Fax): class Phone(Fax):
type = 'phone' type = 'phone'
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), Fax):
fields_to_convert.append(key)
return fields_to_convert
original_write = models.Model.write
original_create = models.Model.create
@api.multi
def write(self, vals):
fields_to_convert = get_phone_fields(self, vals)
if fields_to_convert:
for record in self:
loc_vals = convert_all_phone_fields(
record, vals, fields_to_convert)
original_write(record, loc_vals)
return True
else:
return original_write(self, vals)
@api.model
@api.returns('self', lambda value: value.id)
def create(self, vals):
fields_to_convert = get_phone_fields(self, vals)
if fields_to_convert:
vals = convert_all_phone_fields(self, vals, fields_to_convert)
return original_create(self, vals)
models.Model.write = write
models.Model.create = create

1
base_phone/models/__init__.py

@ -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

31
base_phone/models/base.py

@ -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)
Loading…
Cancel
Save