| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- # -*- coding: utf-8 -*-
- import logging
- from odoo import api, SUPERUSER_ID
- _logger = logging.getLogger(__name__)
- def run(env):
- """
- Fix script for helpdesk_extras migration logic.
- 1. Ensures all templates are migrated to workflow templates.
- 2. Ensures all helpdesk.template.field are linked to workflow templates.
- 3. Forces form regeneration for ALL teams.
- """
- _logger.info("=" * 80)
- _logger.info("STARTING POST-MIGRATION FIX SCRIPT")
- _logger.info("=" * 80)
- # 1. Check for unmigrated teams (still have template_id)
- teams_with_legacy = env['helpdesk.team'].search([
- ('template_id', '!=', False)
- ])
-
- if teams_with_legacy:
- _logger.info(f"Examples of unmigrated teams: {teams_with_legacy[:5].mapped('name')}")
- count_fixed = 0
- for team in teams_with_legacy:
- try:
- # If team has workflow_template_id, just clear template_id
- if team.workflow_template_id:
- team.write({'template_id': False})
- count_fixed += 1
- else:
- # If not, try to find matching workflow template by name
- template = team.template_id
- workflow = env['helpdesk.workflow.template'].search([
- ('name', '=', template.name)
- ], limit=1)
-
- if not workflow:
- # Create if doesn't exist
- workflow = env['helpdesk.workflow.template'].create({
- 'name': template.name,
- 'description': template.description,
- 'active': template.active,
- })
- _logger.info(f"Created missing workflow template: {workflow.name}")
- team.write({
- 'workflow_template_id': workflow.id,
- 'template_id': False
- })
- count_fixed += 1
- except Exception as e:
- _logger.error(f"Error fixing team {team.name}: {e}")
- _logger.info(f"Fixed {count_fixed} teams with legacy template_id.")
- else:
- _logger.info("No teams with legacy template_id found.")
- # 2. Check for orphaned fields
- orphaned_fields = env['helpdesk.template.field'].search([
- ('workflow_template_id', '=', False),
- ('template_id', '!=', False)
- ])
-
- if orphaned_fields:
- _logger.info(f"Found {len(orphaned_fields)} orphaned fields. Trying to re-link.")
- count_relinked = 0
- for field in orphaned_fields:
- try:
- legacy_template = field.template_id
- # Find corresponding workflow
- workflow = env['helpdesk.workflow.template'].search([
- ('name', '=', legacy_template.name)
- ], limit=1)
-
- if workflow:
- field.write({
- 'workflow_template_id': workflow.id,
- 'template_id': False
- })
- count_relinked += 1
- else:
- # Create workflow if needed (edge case)
- workflow = env['helpdesk.workflow.template'].create({
- 'name': legacy_template.name,
- 'description': legacy_template.description,
- 'active': legacy_template.active,
- })
- field.write({
- 'workflow_template_id': workflow.id,
- 'template_id': False
- })
- count_relinked += 1
- except Exception as e:
- _logger.error(f"Error relinking field {field.id}: {e}")
- _logger.info(f"Relinked {count_relinked} orphaned fields.")
- else:
- _logger.info("No orphaned fields found.")
- # 3. FORCE REGENERATION OF FORMS
- teams_to_regenerate = env['helpdesk.team'].search([
- ('use_website_helpdesk_form', '=', True),
- ('workflow_template_id', '!=', False)
- ])
-
- _logger.info(f"Found {len(teams_to_regenerate)} teams to regenerate forms for.")
-
- success_count = 0
- for team in teams_to_regenerate:
- try:
- if team.website_form_view_id:
- team._regenerate_form_from_template()
- success_count += 1
- # _logger.info(f"Regenerated form for team: {team.name}")
- else:
- _logger.warning(f"Team {team.name} has no form view, skipping.")
- except Exception as e:
- _logger.error(f"Failed to regenerate form for team {team.name}: {e}")
- _logger.info(f"Successfully regenerated {success_count}/{len(teams_to_regenerate)} forms.")
-
- env.cr.commit()
- _logger.info("CHANGES COMMITTED.")
|