fix_helpdesk_migration.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from odoo import api, SUPERUSER_ID
  4. _logger = logging.getLogger(__name__)
  5. def run(env):
  6. """
  7. Fix script for helpdesk_extras migration logic.
  8. 1. Ensures all templates are migrated to workflow templates.
  9. 2. Ensures all helpdesk.template.field are linked to workflow templates.
  10. 3. Forces form regeneration for ALL teams.
  11. """
  12. _logger.info("=" * 80)
  13. _logger.info("STARTING POST-MIGRATION FIX SCRIPT")
  14. _logger.info("=" * 80)
  15. # 1. Check for unmigrated teams (still have template_id)
  16. teams_with_legacy = env['helpdesk.team'].search([
  17. ('template_id', '!=', False)
  18. ])
  19. if teams_with_legacy:
  20. _logger.info(f"Examples of unmigrated teams: {teams_with_legacy[:5].mapped('name')}")
  21. count_fixed = 0
  22. for team in teams_with_legacy:
  23. try:
  24. # If team has workflow_template_id, just clear template_id
  25. if team.workflow_template_id:
  26. team.write({'template_id': False})
  27. count_fixed += 1
  28. else:
  29. # If not, try to find matching workflow template by name
  30. template = team.template_id
  31. workflow = env['helpdesk.workflow.template'].search([
  32. ('name', '=', template.name)
  33. ], limit=1)
  34. if not workflow:
  35. # Create if doesn't exist
  36. workflow = env['helpdesk.workflow.template'].create({
  37. 'name': template.name,
  38. 'description': template.description,
  39. 'active': template.active,
  40. })
  41. _logger.info(f"Created missing workflow template: {workflow.name}")
  42. team.write({
  43. 'workflow_template_id': workflow.id,
  44. 'template_id': False
  45. })
  46. count_fixed += 1
  47. except Exception as e:
  48. _logger.error(f"Error fixing team {team.name}: {e}")
  49. _logger.info(f"Fixed {count_fixed} teams with legacy template_id.")
  50. else:
  51. _logger.info("No teams with legacy template_id found.")
  52. # 2. Check for orphaned fields
  53. orphaned_fields = env['helpdesk.template.field'].search([
  54. ('workflow_template_id', '=', False),
  55. ('template_id', '!=', False)
  56. ])
  57. if orphaned_fields:
  58. _logger.info(f"Found {len(orphaned_fields)} orphaned fields. Trying to re-link.")
  59. count_relinked = 0
  60. for field in orphaned_fields:
  61. try:
  62. legacy_template = field.template_id
  63. # Find corresponding workflow
  64. workflow = env['helpdesk.workflow.template'].search([
  65. ('name', '=', legacy_template.name)
  66. ], limit=1)
  67. if workflow:
  68. field.write({
  69. 'workflow_template_id': workflow.id,
  70. 'template_id': False
  71. })
  72. count_relinked += 1
  73. else:
  74. # Create workflow if needed (edge case)
  75. workflow = env['helpdesk.workflow.template'].create({
  76. 'name': legacy_template.name,
  77. 'description': legacy_template.description,
  78. 'active': legacy_template.active,
  79. })
  80. field.write({
  81. 'workflow_template_id': workflow.id,
  82. 'template_id': False
  83. })
  84. count_relinked += 1
  85. except Exception as e:
  86. _logger.error(f"Error relinking field {field.id}: {e}")
  87. _logger.info(f"Relinked {count_relinked} orphaned fields.")
  88. else:
  89. _logger.info("No orphaned fields found.")
  90. # 3. FORCE REGENERATION OF FORMS
  91. teams_to_regenerate = env['helpdesk.team'].search([
  92. ('use_website_helpdesk_form', '=', True),
  93. ('workflow_template_id', '!=', False)
  94. ])
  95. _logger.info(f"Found {len(teams_to_regenerate)} teams to regenerate forms for.")
  96. success_count = 0
  97. for team in teams_to_regenerate:
  98. try:
  99. if team.website_form_view_id:
  100. team._regenerate_form_from_template()
  101. success_count += 1
  102. # _logger.info(f"Regenerated form for team: {team.name}")
  103. else:
  104. _logger.warning(f"Team {team.name} has no form view, skipping.")
  105. except Exception as e:
  106. _logger.error(f"Failed to regenerate form for team {team.name}: {e}")
  107. _logger.info(f"Successfully regenerated {success_count}/{len(teams_to_regenerate)} forms.")
  108. env.cr.commit()
  109. _logger.info("CHANGES COMMITTED.")