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.

73 lines
3.8 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Asterisk Click2dial module for OpenERP
  5. # Copyright (C) 2012-2013 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. from openerp.tools.translate import _
  23. import logging
  24. _logger = logging.getLogger(__name__)
  25. class reformat_all_phonenumbers(orm.TransientModel):
  26. _name = "reformat.all.phonenumbers"
  27. _description = "Reformat all phone numbers"
  28. _columns = {
  29. 'phonenumbers_not_reformatted': fields.text("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(cr, uid, ['|', ('active', '=', True), ('active', '=', False)], context=context),
  37. 'phonefields': ['phone', 'fax', 'mobile'],
  38. 'namefield': 'name',
  39. }
  40. }
  41. return res
  42. def run_reformat_all_phonenumbers(self, cr, uid, ids, context=None):
  43. _logger.info('Starting to reformat all the phone numbers')
  44. phonenumbers_not_reformatted = ''
  45. toreformat_dict = self._extend_reformat_phonenumbers(cr, uid, context=context)
  46. for obj, prop in toreformat_dict.items():
  47. for entry in obj.read(cr, uid, prop['allids'], [prop['namefield']] + prop['phonefields'], context=context):
  48. init_entry = entry.copy()
  49. # entry is _updated_ by the fonction _generic_reformat_phonenumbers()
  50. try:
  51. obj._generic_reformat_phonenumbers(cr, uid, entry, context=context)
  52. except Exception, e:
  53. #raise orm.except_orm(_('Error :'), _("Problem on entry '%s'. Error message: %s" % (init_entry.get(prop['namefield']), e[1])))
  54. phonenumbers_not_reformatted += "Problem on %s '%s'. Error message: %s" % (obj._description, init_entry.get(prop['namefield']), e[1]) + "\n"
  55. _logger.warning("Problem on %s '%s'. Error message: %s" % (obj._description, init_entry.get(prop['namefield']), e[1]))
  56. continue
  57. if any([init_entry.get(field) != entry.get(field) for field in prop['phonefields']]):
  58. entry.pop('id')
  59. entry.pop(prop['namefield'])
  60. _logger.info('[%s] Reformating phone number: FROM %s TO %s' % (obj._description, unicode(init_entry), unicode(entry)))
  61. obj.write(cr, uid, init_entry['id'], entry, context=context)
  62. if not phonenumbers_not_reformatted:
  63. phonenumbers_not_reformatted = 'All phone numbers have been reformatted successfully.'
  64. self.write(cr, uid, ids[0], {'phonenumbers_not_reformatted': phonenumbers_not_reformatted}, context=context)
  65. _logger.info('End of the phone number reformatting wizard')
  66. return True