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.

100 lines
4.2 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. }
  31. def _extend_reformat_phonenumbers(self, cr, uid, context=None):
  32. '''This function is designed to be inherited
  33. to extend the functionnality to objects other than res.partner'''
  34. res = {
  35. self.pool['res.partner']: {
  36. 'allids': self.pool['res.partner'].search(
  37. cr, uid, [
  38. '|',
  39. ('active', '=', True),
  40. ('active', '=', False)
  41. ], context=context),
  42. 'phonefields': ['phone', 'fax', 'mobile'],
  43. 'namefield': 'name',
  44. }
  45. }
  46. return res
  47. def run_reformat_all_phonenumbers(self, cr, uid, ids, context=None):
  48. _logger.info('Starting to reformat all the phone numbers')
  49. phonenumbers_not_reformatted = ''
  50. toreformat_dict = self._extend_reformat_phonenumbers(
  51. cr, uid, context=context)
  52. for obj, prop in toreformat_dict.items():
  53. for entry in obj.read(
  54. cr, uid, prop['allids'],
  55. [prop['namefield']] + prop['phonefields'],
  56. context=context):
  57. init_entry = entry.copy()
  58. # entry is _updated_ by the fonction
  59. # _generic_reformat_phonenumbers()
  60. try:
  61. obj._generic_reformat_phonenumbers(
  62. cr, uid, entry, context=context)
  63. except Exception, e:
  64. phonenumbers_not_reformatted += \
  65. "Problem on %s '%s'. Error message: %s\n" % (
  66. obj._description,
  67. init_entry.get(prop['namefield']), e[1])
  68. _logger.warning(
  69. "Problem on %s '%s'. Error message: %s" % (
  70. obj._description,
  71. init_entry.get(prop['namefield']), e[1]))
  72. continue
  73. if any(
  74. [init_entry.get(field)
  75. != entry.get(field) for field
  76. in prop['phonefields']]):
  77. entry.pop('id')
  78. entry.pop(prop['namefield'])
  79. _logger.info(
  80. '[%s] Reformating phone number: FROM %s TO %s' % (
  81. obj._description, unicode(init_entry),
  82. unicode(entry)))
  83. obj.write(
  84. cr, uid, init_entry['id'], entry, context=context)
  85. if not phonenumbers_not_reformatted:
  86. phonenumbers_not_reformatted = \
  87. 'All phone numbers have been reformatted successfully.'
  88. self.write(
  89. cr, uid, ids[0],
  90. {'phonenumbers_not_reformatted': phonenumbers_not_reformatted},
  91. context=context)
  92. _logger.info('End of the phone number reformatting wizard')
  93. return True