Browse Source

Port the wizard reformat_all_phonenumbers to the new fields.Phone archi

pull/88/head
Alexis de Lattre 9 years ago
parent
commit
390c0e3b09
  1. 25
      base_phone/models/phone_common.py
  2. 62
      base_phone/wizard/reformat_all_phonenumbers.py

25
base_phone/models/phone_common.py

@ -50,15 +50,14 @@ class PhoneCommon(models.AbstractModel):
end_number_to_match = presented_number end_number_to_match = presented_number
sorted_phonemodels = self._get_phone_models() sorted_phonemodels = self._get_phone_models()
# [('res.parter', 10), ('crm.lead', 20)]
for (obj, prio) in sorted_phonemodels:
for obj_dict in sorted_phonemodels:
obj = obj_dict['object']
pg_search_number = str('%' + end_number_to_match) pg_search_number = str('%' + end_number_to_match)
_logger.debug( _logger.debug(
"Will search phone and mobile numbers in %s ending with '%s'", "Will search phone and mobile numbers in %s ending with '%s'",
obj._name, end_number_to_match) obj._name, end_number_to_match)
domain = [] domain = []
for field in obj._fields:
if isinstance(obj._fields[field], Phone):
for field in obj_dict['fields']:
domain.append((field, '=like', pg_search_number)) domain.append((field, '=like', pg_search_number))
if len(domain) > 1: if len(domain) > 1:
domain = ['|'] * (len(domain) - 1) + domain domain = ['|'] * (len(domain) - 1) + domain
@ -85,7 +84,7 @@ class PhoneCommon(models.AbstractModel):
@api.model @api.model
def _get_phone_models(self): def _get_phone_models(self):
res = []
phoneobj = []
for model_name in self.env.registry.keys(): for model_name in self.env.registry.keys():
senv = False senv = False
try: try:
@ -95,10 +94,20 @@ class PhoneCommon(models.AbstractModel):
if ( if (
hasattr(senv, '_phone_name_sequence') and hasattr(senv, '_phone_name_sequence') and
isinstance(senv._phone_name_sequence, int)): isinstance(senv._phone_name_sequence, int)):
res.append((senv, senv._phone_name_sequence))
phoneobj.append((senv, senv._phone_name_sequence))
phoneobj_sorted = sorted(phoneobj, key=lambda element: element[1])
phonemodels_sorted = sorted(res, key=lambda element: element[1])
return phonemodels_sorted
res = []
for (obj, prio) in phoneobj_sorted:
entry = {'object': obj, 'fields': []}
for field in obj._fields:
if isinstance(obj._fields[field], Phone):
entry['fields'].append(field)
res.append(entry)
# [{'fields': ['fax', 'phone', 'mobile'], 'object': res.partner()},
# {'fields': ['fax', 'phone', 'mobile'], 'object': crm.lead()}]
return res
@api.model @api.model
def click2dial(self, erp_number): def click2dial(self, erp_number):

62
base_phone/wizard/reformat_all_phonenumbers.py

@ -1,23 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
##############################################################################
#
# Base Phone module for Odoo
# Copyright (C) 2012-2015 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/>.
#
##############################################################################
# © 2012-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, api from openerp import models, fields, api
import logging import logging
@ -42,15 +25,15 @@ class ReformatAllPhonenumbers(models.TransientModel):
self.ensure_one() self.ensure_one()
logger.info('Starting to reformat all the phone numbers') logger.info('Starting to reformat all the phone numbers')
phonenumbers_not_reformatted = u'' phonenumbers_not_reformatted = u''
phoneobjects = self.env['phone.common']._get_phone_fields()
for objname in phoneobjects:
fields = self.env[objname]._phone_fields
obj = self.env[objname]
phoneobjects = self.env['phone.common']._get_phone_models()
for obj_dict in phoneobjects:
fields = obj_dict['fields']
obj = obj_dict['object']
logger.info( logger.info(
'Starting to reformat phone numbers on object %s ' 'Starting to reformat phone numbers on object %s '
'(fields = %s)', objname, fields)
'(fields = %s)', obj._name, fields)
# search if this object has an 'active' field # search if this object has an 'active' field
if obj._columns.get('active') or objname == 'hr.employee':
if obj._fields.get('active') or obj._name == 'hr.employee':
# hr.employee inherits from 'resource.resource' and # hr.employee inherits from 'resource.resource' and
# 'resource.resource' has an active field # 'resource.resource' has an active field
# As I don't know how to detect such cases, I hardcode it here # As I don't know how to detect such cases, I hardcode it here
@ -61,32 +44,11 @@ class ReformatAllPhonenumbers(models.TransientModel):
all_entries = obj.search(domain) all_entries = obj.search(domain)
for entry in all_entries: for entry in all_entries:
init_entry_vals = {}
vals = {}
for field in fields: for field in fields:
init_entry_vals[field] = entry[field]
entry_vals = init_entry_vals.copy()
# entry is _updated_ by the fonction
# _generic_reformat_phonenumbers()
try:
entry.with_context(raise_if_phone_parse_fails=True).\
_reformat_phonenumbers_write(entry_vals)
except Exception, e:
name = entry.name_get()[0][1]
phonenumbers_not_reformatted += \
"Problem on %s '%s'. Error message: %s\n" % (
obj._description, name, unicode(e))
logger.warning(
"Problem on %s '%s'. Error message: %s",
obj._description, name, unicode(e))
continue
if any([
init_entry_vals.get(field) != entry_vals.get(field) for
field in fields]):
logger.info(
'[%s] Reformating phone number: FROM %s TO %s',
obj._description, unicode(init_entry_vals),
unicode(entry_vals))
entry.write(entry_vals)
vals[field] = entry[field]
if any([value for value in vals.values()]):
entry.write(vals)
if not phonenumbers_not_reformatted: if not phonenumbers_not_reformatted:
phonenumbers_not_reformatted = \ phonenumbers_not_reformatted = \
'All phone numbers have been reformatted successfully.' 'All phone numbers have been reformatted successfully.'

Loading…
Cancel
Save