post-migration.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from odoo import api, SUPERUSER_ID
  4. from odoo.tools import sql
  5. _logger = logging.getLogger(__name__)
  6. def migrate(cr, version):
  7. """Actualizar traducciones para quitar 'Enterprise' de los nombres de módulos"""
  8. try:
  9. env = api.Environment(cr, SUPERUSER_ID, {})
  10. # Verificar si el modelo ir.translation está disponible
  11. if 'ir.translation' not in env.registry:
  12. _logger.warning("Modelo ir.translation no disponible, usando SQL directo")
  13. # Usar SQL directo si el modelo no está disponible
  14. if sql.table_exists(cr, '_ir_translation'):
  15. cr.execute("""
  16. UPDATE _ir_translation
  17. SET value = CASE
  18. WHEN value = 'Web de Enterprise' THEN 'Web'
  19. WHEN value = 'Web Enterprise' THEN 'Web'
  20. WHEN value = 'Contabilidad Analítica Enterprise' THEN 'Contabilidad Analítica'
  21. WHEN value = 'Analytic Accounting Enterprise' THEN 'Contabilidad Analítica'
  22. WHEN value = 'Facturación (Enterprise)' THEN 'Facturación'
  23. WHEN value = 'Facturación Enterprise' THEN 'Facturación'
  24. WHEN value = 'Invoicing (Enterprise)' THEN 'Facturación'
  25. ELSE regexp_replace(
  26. regexp_replace(
  27. regexp_replace(value, 'Enterprise', '', 'gi'),
  28. 'enterprise', '', 'gi'
  29. ),
  30. '\\s+', ' ', 'g'
  31. )
  32. END
  33. WHERE name = 'helpdesk.affected.module,name'
  34. AND lang = 'es_MX'
  35. AND (value ILIKE '%Enterprise%' OR value ILIKE '%enterprise%')
  36. """)
  37. updated_count = cr.rowcount
  38. cr.commit()
  39. _logger.info(f"✅ {updated_count} traducciones actualizadas exitosamente (vía SQL)")
  40. else:
  41. _logger.warning("Tabla _ir_translation no existe, saltando actualización")
  42. return
  43. # Actualizar traducciones en ir.translation
  44. Translation = env['ir.translation']
  45. # Buscar traducciones problemáticas
  46. translations = Translation.search([
  47. ('name', '=', 'helpdesk.affected.module,name'),
  48. ('lang', '=', 'es_MX'),
  49. ('value', 'ilike', 'Enterprise'),
  50. ])
  51. _logger.info(f"Encontradas {len(translations)} traducciones con 'Enterprise' para actualizar")
  52. updates = {
  53. 'Web de Enterprise': 'Web',
  54. 'Web Enterprise': 'Web',
  55. 'Contabilidad Analítica Enterprise': 'Contabilidad Analítica',
  56. 'Analytic Accounting Enterprise': 'Contabilidad Analítica',
  57. 'Facturación (Enterprise)': 'Facturación',
  58. 'Facturación Enterprise': 'Facturación',
  59. 'Invoicing (Enterprise)': 'Facturación',
  60. }
  61. updated_count = 0
  62. for trans in translations:
  63. try:
  64. old_value = trans.value
  65. new_value = updates.get(old_value)
  66. if not new_value:
  67. # Limpieza genérica
  68. new_value = old_value.replace('Enterprise', '').replace('enterprise', '')
  69. new_value = new_value.replace(' de ', ' ').replace(' (', '').replace(')', '')
  70. new_value = ' '.join(new_value.split()).strip()
  71. if old_value != new_value:
  72. trans.write({'value': new_value})
  73. updated_count += 1
  74. _logger.info(f"Actualizado: '{old_value}' → '{new_value}'")
  75. except Exception as e:
  76. _logger.warning(f"Error al actualizar traducción {trans.id}: {e}")
  77. continue
  78. cr.commit()
  79. _logger.info(f"✅ {updated_count} traducciones actualizadas exitosamente")
  80. except Exception as e:
  81. _logger.error(f"Error en migración 18.0.1.0.9: {e}", exc_info=True)
  82. cr.rollback()
  83. # No lanzar la excepción para no bloquear la actualización del módulo