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.

63 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2012-2016 Akretion (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, api
  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. @api.multi
  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 = u''
  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 tell me
  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. vals[field] = entry[field]
  43. if any([value for value in vals.values()]):
  44. entry.write(vals)
  45. if not phonenumbers_not_reformatted:
  46. phonenumbers_not_reformatted = \
  47. 'All phone numbers have been reformatted successfully.'
  48. self.write({
  49. 'phonenumbers_not_reformatted': phonenumbers_not_reformatted,
  50. 'state': 'done',
  51. })
  52. logger.info('End of the phone number reformatting wizard')
  53. action = self.env['ir.actions.act_window'].for_xml_id(
  54. 'base_phone', 'reformat_all_phonenumbers_action')
  55. action['res_id'] = self.id
  56. return action