Skip to content

Commit

Permalink
[MIG] Migrated base_phone to v10.0 (OCA#107)
Browse files Browse the repository at this point in the history
[MIG] Migrated base_phone to v10.0

* Define Phone and Fax as real field types, to avoid the need of defining the widget on each view

* [IMP] Add missing ImportError checks for the phonenumbers module
  • Loading branch information
Sylvain Garancher authored and luisDIXMIT committed Apr 3, 2024
1 parent 137fdb4 commit e3bdec8
Show file tree
Hide file tree
Showing 22 changed files with 276 additions and 329 deletions.
2 changes: 1 addition & 1 deletion base_phone/README.rst
Expand Up @@ -54,7 +54,7 @@ There is no specific usage procedure for this module.

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/{repo_id}/9.0
:target: https://runbot.odoo-community.org/runbot/228/10.0

Known issues / Roadmap
======================
Expand Down
1 change: 1 addition & 0 deletions base_phone/__init__.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from . import fields
from . import controllers
from . import models
from . import wizard
4 changes: 2 additions & 2 deletions base_phone/__manifest__.py
Expand Up @@ -22,7 +22,7 @@

{
'name': 'Base Phone',
'version': '9.0.0.1.0',
'version': '10.0.0.1.0',
'category': 'Phone',
'license': 'AGPL-3',
'summary': 'Validate phone numbers',
Expand All @@ -49,5 +49,5 @@
],
'qweb': ['static/src/xml/*.xml'],
'images': [],
'installable': False,
'installable': True,
}
3 changes: 3 additions & 0 deletions base_phone/controllers/__init__.py
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import main
Expand Up @@ -19,13 +19,11 @@
#
##############################################################################

import openerp
import odoo


class BasePhoneController(openerp.addons.web.http.Controller):
_cp_path = '/base_phone'

@openerp.addons.web.http.jsonrequest
class BasePhoneController(odoo.http.Controller):
@odoo.http.route('/base_phone', type='json', auth='none')
def click2dial(self, req, phone_number, click2dial_model, click2dial_id):
res = req.session.model('phone.common').click2dial(
phone_number, {
Expand Down
28 changes: 18 additions & 10 deletions base_phone/fields.py
Expand Up @@ -5,39 +5,43 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).


from openerp import api, fields, models
from odoo import api, fields, models
from operator import attrgetter
import phonenumbers
import logging

_logger = logging.getLogger(__name__)

try:
import phonenumbers
except ImportError:
_logger.debug('Cannot `import phonenumbers`.')


class Phone(fields.Char):
class Fax(fields.Char):
type = 'fax'

_slots = {
'country_field': None,
'partner_field': None,
}

def __init__(
self, string=None, country_field=None, partner_field=None,
**kwargs):
super(Phone, self).__init__(
self, string=fields.Default, country_field=fields.Default,
partner_field=fields.Default, **kwargs):
super(Fax, self).__init__(
string=string, country_field=country_field,
partner_field=partner_field, **kwargs)

_related_country_field = property(attrgetter('country_field'))
_related_partner_field = property(attrgetter('partner_field'))

def _setup_regular_full(self, model):
super(Phone, self)._setup_regular_full(model)
super(Fax, self)._setup_regular_full(model)
assert self.country_field in model._fields or \
self.partner_field in model._fields, \
"field %s with unknown country_field and partner_field" % self

def convert_to_cache(self, value, record, validate=True):
res = super(Phone, self).convert_to_cache(
res = super(Fax, self).convert_to_cache(
value, record, validate=validate)
# print 'db value', res
if res:
Expand All @@ -53,6 +57,10 @@ def convert_to_cache(self, value, record, validate=True):
return res


class Phone(Fax):
type = 'phone'


def convert_phone_field(value, country_code):
_logger.debug(
'convert_phone_field value=%s country=%s', value, country_code)
Expand Down Expand Up @@ -104,7 +112,7 @@ def convert_all_phone_fields(self, vals, fields_to_convert):
def get_phone_fields(self, vals):
fields_to_convert = []
for key in vals:
if isinstance(self._fields.get(key), Phone):
if isinstance(self._fields.get(key), Fax):
fields_to_convert.append(key)
return fields_to_convert

Expand Down
1 change: 0 additions & 1 deletion base_phone/models/__init__.py
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

from . import controller
from . import res_company
from . import res_partner
from . import phone_common
14 changes: 8 additions & 6 deletions base_phone/models/phone_common.py
Expand Up @@ -2,14 +2,16 @@
# © 2010-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, api
from openerp.addons.base_phone.fields import Phone
from odoo import models, api
from .. import fields as phone_fields
import logging
# Lib for phone number reformating -> pip install phonenumbers
import phonenumbers

_logger = logging.getLogger(__name__)

try:
import phonenumbers
except ImportError:
_logger.debug('Cannot `import phonenumbers`.')


class PhoneCommon(models.AbstractModel):
_name = 'phone.common'
Expand Down Expand Up @@ -102,7 +104,7 @@ def _get_phone_models(self):
for (obj, prio) in phoneobj_sorted:
entry = {'object': obj, 'fields': []}
for field in obj._fields:
if isinstance(obj._fields[field], Phone):
if isinstance(obj._fields[field], phone_fields.Phone):
entry['fields'].append(field)
res.append(entry)
# [{'fields': ['fax', 'phone', 'mobile'], 'object': res.partner()},
Expand Down
13 changes: 9 additions & 4 deletions base_phone/models/res_company.py
Expand Up @@ -2,7 +2,8 @@
# © 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, fields
from odoo import models, fields
from .. import fields as phone_fields


class ResCompany(models.Model):
Expand All @@ -11,15 +12,19 @@ class ResCompany(models.Model):
number_of_digits_to_match_from_end = fields.Integer(
string='Number of Digits To Match From End',
default=8,
help="In several situations, OpenERP will have to find a "
help="In several situations, Odoo will have to find a "
"Partner/Lead/Employee/... from a phone number presented by the "
"calling party. As the phone numbers presented by your phone "
"operator may not always be displayed in a standard format, "
"the best method to find the related Partner/Lead/Employee/... "
"in OpenERP is to try to match the end of the phone number in "
"OpenERP with the N last digits of the phone number presented "
"in Odoo is to try to match the end of the phone number in "
"Odoo with the N last digits of the phone number presented "
"by the calling party. N is the value you should enter in this "
"field.")
phone = phone_fields.Phone(
country_field='country_id', partner_field='partner_id')
fax = phone_fields.Fax(
country_field='country_id', partner_field='partner_id')

_sql_constraints = [(
'number_of_digits_to_match_from_end_positive',
Expand Down
6 changes: 3 additions & 3 deletions base_phone/models/res_partner.py
Expand Up @@ -3,8 +3,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from openerp import models, api
from openerp.addons.base_phone import fields
from odoo import models, api
from .. import fields


class ResPartner(models.Model):
Expand All @@ -14,7 +14,7 @@ class ResPartner(models.Model):
phone = fields.Phone(country_field='country_id', partner_field='parent_id')
mobile = fields.Phone(
country_field='country_id', partner_field='parent_id')
fax = fields.Phone(country_field='country_id', partner_field='parent_id')
fax = fields.Fax(country_field='country_id', partner_field='parent_id')

@api.multi
def name_get(self):
Expand Down
14 changes: 6 additions & 8 deletions base_phone/security/phone_security.xml
Expand Up @@ -5,13 +5,11 @@
The licence is in the file __openerp__.py
-->

<openerp>
<data noupdate="1">
<odoo noupdate="1">

<!-- New group dedicated to the "Get CallerID name from OpenERP" feature -->
<record id="group_callerid" model="res.groups">
<field name="name">Phone CallerID</field>
</record>
<!-- New group dedicated to the "Get CallerID name from OpenERP" feature -->
<record id="group_callerid" model="res.groups">
<field name="name">Phone CallerID</field>
</record>

</data>
</openerp>
</odoo>

0 comments on commit e3bdec8

Please sign in to comment.