You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2012-2018 Akretion France
  3. # @author: Alexis de Lattre <alexis.delattre@akretion.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import models, fields, api
  6. import logging
  7. logger = logging.getLogger(__name__)
  8. class ReformatAllPhonenumbers(models.TransientModel):
  9. _name = "reformat.all.phonenumbers"
  10. _inherit = "res.config.installer"
  11. _description = "Reformat all phone numbers"
  12. phonenumbers_not_reformatted = fields.Text(
  13. string="Phone numbers that couldn't be reformatted")
  14. state = fields.Selection([
  15. ('draft', 'Draft'),
  16. ('done', 'Done'),
  17. ], string='State', default='draft')
  18. def run_reformat_all_phonenumbers(self):
  19. self.ensure_one()
  20. logger.info('Starting to reformat all the phone numbers')
  21. phonenumbers_not_reformatted = ''
  22. phoneobjects = self.env['phone.common']._get_phone_models()
  23. for obj_dict in phoneobjects:
  24. fields = obj_dict['fields']
  25. obj = obj_dict['object']
  26. logger.info(
  27. 'Starting to reformat phone numbers on object %s '
  28. '(fields = %s)', obj._name, fields)
  29. # search if this object has an 'active' field
  30. if obj._fields.get('active') or obj._name == 'hr.employee':
  31. # hr.employee inherits from 'resource.resource' and
  32. # 'resource.resource' has an active field
  33. # As I don't know how to detect such cases, I hardcode it here
  34. # If you know a better solution, please submit a pull request
  35. domain = ['|', ('active', '=', True), ('active', '=', False)]
  36. else:
  37. domain = []
  38. all_entries = obj.search(domain)
  39. for entry in all_entries:
  40. vals = {}
  41. for field in fields:
  42. if entry[field]:
  43. new_phone = entry.phone_format(entry[field])
  44. if new_phone != entry[field]:
  45. vals[field] = new_phone
  46. if vals:
  47. entry.write(vals)
  48. # TODO: the warnings of the phone_format() methods are logged
  49. # in the logfile... it would be great to get them and show them to
  50. # the user... maybe via a rewrite of phone_format()
  51. # with a raise_expcetion=True and catch the exception
  52. if not phonenumbers_not_reformatted:
  53. phonenumbers_not_reformatted = \
  54. 'All phone numbers have been reformatted successfully.'
  55. self.write({
  56. 'phonenumbers_not_reformatted': phonenumbers_not_reformatted,
  57. 'state': 'done',
  58. })
  59. logger.info('End of the phone number reformatting wizard')
  60. action = self.env['ir.actions.act_window'].for_xml_id(
  61. 'base_phone', 'reformat_all_phonenumbers_action')
  62. action['res_id'] = self.id
  63. return action