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.

109 lines
4.8 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Base Phone module for OpenERP
  5. # Copyright (C) 2012-2014 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.osv import orm, fields
  22. import logging
  23. logger = logging.getLogger(__name__)
  24. class reformat_all_phonenumbers(orm.TransientModel):
  25. _name = "reformat.all.phonenumbers"
  26. _description = "Reformat all phone numbers"
  27. _columns = {
  28. 'phonenumbers_not_reformatted': fields.text(
  29. "Phone numbers that couldn't be reformatted"),
  30. 'state': fields.selection([
  31. ('draft', 'Draft'),
  32. ('done', 'Done'),
  33. ], string='State', default='draft'),
  34. }
  35. _defaults = {
  36. 'state': 'draft',
  37. }
  38. def run_reformat_all_phonenumbers(self, cr, uid, ids, context=None):
  39. logger.info('Starting to reformat all the phone numbers')
  40. phonenumbers_not_reformatted = ''
  41. phoneobjects = self.pool['phone.common']._get_phone_fields(
  42. cr, uid, context=context)
  43. ctx_raise = dict(context, raise_if_phone_parse_fails=True)
  44. for objname in phoneobjects:
  45. fields = self.pool[objname]._phone_fields
  46. obj = self.pool[objname]
  47. logger.info(
  48. 'Starting to reformat phone numbers on object %s '
  49. '(fields = %s)' % (objname, fields))
  50. # search if this object has an 'active' field
  51. if obj._columns.get('active') or objname == 'hr.employee':
  52. # hr.employee inherits from 'resource.resource' and
  53. # 'resource.resource' has an active field
  54. # As I don't know how to detect such cases, I hardcode it here
  55. # If you know a better solution, please tell me
  56. domain = ['|', ('active', '=', True), ('active', '=', False)]
  57. else:
  58. domain = []
  59. all_ids = obj.search(cr, uid, domain, context=context)
  60. for entry in obj.read(
  61. cr, uid, all_ids, fields, context=context):
  62. init_entry = entry.copy()
  63. # entry is _updated_ by the fonction
  64. # _generic_reformat_phonenumbers()
  65. try:
  66. obj._generic_reformat_phonenumbers(
  67. cr, uid, [entry['id']], entry, context=ctx_raise)
  68. except Exception, e:
  69. name = obj.name_get(
  70. cr, uid, [init_entry['id']], context=context)[0][1]
  71. phonenumbers_not_reformatted += \
  72. "Problem on %s '%s'. Error message: %s\n" % (
  73. obj._description, name, unicode(e))
  74. logger.warning(
  75. "Problem on %s '%s'. Error message: %s" % (
  76. obj._description, name, unicode(e)))
  77. continue
  78. if any(
  79. [init_entry.get(field)
  80. != entry.get(field) for field
  81. in fields]):
  82. entry.pop('id')
  83. logger.info(
  84. '[%s] Reformating phone number: FROM %s TO %s' % (
  85. obj._description, unicode(init_entry),
  86. unicode(entry)))
  87. obj.write(
  88. cr, uid, init_entry['id'], entry, context=context)
  89. if not phonenumbers_not_reformatted:
  90. phonenumbers_not_reformatted = \
  91. 'All phone numbers have been reformatted successfully.'
  92. self.write(
  93. cr, uid, ids[0], {
  94. 'phonenumbers_not_reformatted': phonenumbers_not_reformatted,
  95. 'state': 'done',
  96. }, context=context)
  97. logger.info('End of the phone number reformatting wizard')
  98. action = self.pool['ir.actions.act_window'].for_xml_id(
  99. cr, uid, 'base_phone', 'reformat_all_phonenumbers_action',
  100. context=context)
  101. action['res_id'] = ids[0]
  102. return action