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.

69 lines
2.9 KiB

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