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.

104 lines
4.5 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Base Phone module for Odoo
  5. # Copyright (C) 2012-2015 Alexis de Lattre <alexis@via.ecp.fr>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, api
  22. import logging
  23. logger = logging.getLogger(__name__)
  24. class ReformatAllPhonenumbers(models.TransientModel):
  25. _name = "reformat.all.phonenumbers"
  26. _inherit = "res.config.installer"
  27. _description = "Reformat all phone numbers"
  28. phonenumbers_not_reformatted = fields.Text(
  29. string="Phone numbers that couldn't be reformatted")
  30. state = fields.Selection([
  31. ('draft', 'Draft'),
  32. ('done', 'Done'),
  33. ], string='State', default='draft')
  34. @api.multi
  35. def run_reformat_all_phonenumbers(self):
  36. self.ensure_one()
  37. logger.info('Starting to reformat all the phone numbers')
  38. phonenumbers_not_reformatted = u''
  39. phoneobjects = self.env['phone.common']._get_phone_fields()
  40. for objname in phoneobjects:
  41. fields = self.env[objname]._phone_fields
  42. obj = self.env[objname]
  43. logger.info(
  44. 'Starting to reformat phone numbers on object %s '
  45. '(fields = %s)', objname, fields)
  46. # search if this object has an 'active' field
  47. if obj._columns.get('active') or objname == 'hr.employee':
  48. # hr.employee inherits from 'resource.resource' and
  49. # 'resource.resource' has an active field
  50. # As I don't know how to detect such cases, I hardcode it here
  51. # If you know a better solution, please tell me
  52. domain = ['|', ('active', '=', True), ('active', '=', False)]
  53. else:
  54. domain = []
  55. all_entries = obj.search(domain)
  56. for entry in all_entries:
  57. print "entry=", entry
  58. init_entry_vals = {}
  59. for field in fields:
  60. init_entry_vals[field] = entry[field]
  61. print "init_entry=", init_entry_vals
  62. entry_vals = init_entry_vals.copy()
  63. # entry is _updated_ by the fonction
  64. # _generic_reformat_phonenumbers()
  65. try:
  66. entry.with_context(raise_if_phone_parse_fails=True).\
  67. _reformat_phonenumbers_write(entry_vals)
  68. except Exception, e:
  69. name = entry.name_get()[0][1]
  70. phonenumbers_not_reformatted += \
  71. "Problem on %s '%s'. Error message: %s\n" % (
  72. obj._description, name, unicode(e))
  73. logger.warning(
  74. "Problem on %s '%s'. Error message: %s",
  75. obj._description, name, unicode(e))
  76. continue
  77. if any([
  78. init_entry_vals.get(field) != entry_vals.get(field) for
  79. field in fields]):
  80. print "entry_vals=", entry_vals
  81. logger.info(
  82. '[%s] Reformating phone number: FROM %s TO %s',
  83. obj._description, unicode(init_entry_vals),
  84. unicode(entry_vals))
  85. entry.write(entry_vals)
  86. if not phonenumbers_not_reformatted:
  87. phonenumbers_not_reformatted = \
  88. 'All phone numbers have been reformatted successfully.'
  89. self.write({
  90. 'phonenumbers_not_reformatted': phonenumbers_not_reformatted,
  91. 'state': 'done',
  92. })
  93. logger.info('End of the phone number reformatting wizard')
  94. action = self.env['ir.actions.act_window'].for_xml_id(
  95. 'base_phone', 'reformat_all_phonenumbers_action')
  96. action['res_id'] = self.id
  97. return action