|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | # Part of Odoo. See LICENSE file for full copyright and licensing details.
|
3 | 3 |
|
| 4 | +from collections import defaultdict |
4 | 5 | import logging
|
5 | 6 |
|
6 | 7 | from odoo import api, models
|
@@ -88,20 +89,33 @@ def _process_fiscal_pos_translations(self, company_id, langs, field):
|
88 | 89 |
|
89 | 90 | def _get_template_from_model(self, company_id, model):
|
90 | 91 | """ Find the records and their matching template """
|
91 |
| - # genereated records have an external id with the format <company id>_<template id> |
92 |
| - out_data = self.env['ir.model.data'].search([('model', '=', model), ('name', '=like', str(company_id)+'\_%')]) |
93 |
| - out_ids = self.env[model].browse(out_data.mapped('res_id')) |
94 |
| - |
95 |
| - # templates and records may have been created in a different order |
96 |
| - # reorder them based on external id names |
97 |
| - in_xml_id_names = [xml_id.partition(str(company_id) + '_')[-1] for xml_id in out_data.mapped('name')] |
98 |
| - in_xml_id_names = {name: index for index, name in enumerate(in_xml_id_names)} |
99 |
| - |
100 |
| - in_xml_ids = self.env['ir.model.data'].search([('model', '=', model+'.template'), ('name', 'in', list(in_xml_id_names))]) |
101 |
| - in_xml_ids = in_xml_ids.sorted(key=lambda x: in_xml_id_names[x.name]) |
102 |
| - |
103 |
| - in_ids = self.env[model+'.template'].browse(in_xml_ids.mapped('res_id')) |
104 |
| - return (in_ids, out_ids) |
| 92 | + # generated records have an external id with the format <company id>_<template xml id> |
| 93 | + grouped_out_data = defaultdict(lambda: self.env['ir.model.data']) |
| 94 | + for imd in self.env['ir.model.data'].search([ |
| 95 | + ('model', '=', model), |
| 96 | + ('name', '=like', str(company_id) + '_%') |
| 97 | + ]): |
| 98 | + grouped_out_data[imd.module] += imd |
| 99 | + |
| 100 | + in_records = self.env[model + '.template'] |
| 101 | + out_records = self.env[model] |
| 102 | + for module, out_data in grouped_out_data.items(): |
| 103 | + # templates and records may have been created in a different order |
| 104 | + # reorder them based on external id names |
| 105 | + expected_in_xml_id_names = {xml_id.name.partition(str(company_id) + '_')[-1]: xml_id for xml_id in out_data} |
| 106 | + |
| 107 | + in_xml_ids = self.env['ir.model.data'].search([ |
| 108 | + ('model', '=', model + '.template'), |
| 109 | + ('module', '=', module), |
| 110 | + ('name', 'in', list(expected_in_xml_id_names)) |
| 111 | + ]) |
| 112 | + in_xml_ids = {xml_id.name: xml_id for xml_id in in_xml_ids} |
| 113 | + |
| 114 | + for name, xml_id in expected_in_xml_id_names.items(): |
| 115 | + in_records += self.env[model + '.template'].browse(in_xml_ids[name].res_id) |
| 116 | + out_records += self.env[model].browse(xml_id.res_id) |
| 117 | + |
| 118 | + return (in_records, out_records) |
105 | 119 |
|
106 | 120 | class BaseLanguageInstall(models.TransientModel):
|
107 | 121 | """ Install Language"""
|
|
0 commit comments