1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import models
5
6class IrTranslation(models.Model):
7    _inherit = "ir.translation"
8
9    def _load_module_terms(self, modules, langs, overwrite=False):
10        """ Add missing website specific translation """
11        res = super()._load_module_terms(modules, langs, overwrite=overwrite)
12
13        if not langs or not modules:
14            return res
15
16        if overwrite:
17            conflict_clause = """
18                   ON CONFLICT {}
19                   DO UPDATE SET (name, lang, res_id, src, type, value, module, state, comments) =
20                       (EXCLUDED.name, EXCLUDED.lang, EXCLUDED.res_id, EXCLUDED.src, EXCLUDED.type,
21                        EXCLUDED.value, EXCLUDED.module, EXCLUDED.state, EXCLUDED.comments)
22                WHERE EXCLUDED.value IS NOT NULL AND EXCLUDED.value != ''
23            """;
24        else:
25            conflict_clause = " ON CONFLICT DO NOTHING"
26
27        # Add specific view translations
28        self.env.cr.execute("""
29            INSERT INTO ir_translation(name, lang, res_id, src, type, value, module, state, comments)
30            SELECT DISTINCT ON (specific.id, t.lang, md5(src)) t.name, t.lang, specific.id, t.src, t.type, t.value, t.module, t.state, t.comments
31              FROM ir_translation t
32             INNER JOIN ir_ui_view generic
33                ON t.type = 'model_terms' AND t.name = 'ir.ui.view,arch_db' AND t.res_id = generic.id
34             INNER JOIN ir_ui_view specific
35                ON generic.key = specific.key
36             WHERE t.lang IN %s and t.module IN %s
37               AND generic.website_id IS NULL AND generic.type = 'qweb'
38               AND specific.website_id IS NOT NULL""" + conflict_clause.format(
39                   "(type, name, lang, res_id, md5(src))"
40        ), (tuple(langs), tuple(modules)))
41
42        default_menu = self.env.ref('website.main_menu', raise_if_not_found=False)
43        if not default_menu:
44            return res
45
46        # Add specific menu translations
47        self.env.cr.execute("""
48            INSERT INTO ir_translation(name, lang, res_id, src, type, value, module, state, comments)
49            SELECT DISTINCT ON (s_menu.id, t.lang) t.name, t.lang, s_menu.id, t.src, t.type, t.value, t.module, t.state, t.comments
50              FROM ir_translation t
51             INNER JOIN website_menu o_menu
52                ON t.type = 'model' AND t.name = 'website.menu,name' AND t.res_id = o_menu.id
53             INNER JOIN website_menu s_menu
54                ON o_menu.name = s_menu.name AND o_menu.url = s_menu.url
55             INNER JOIN website_menu root_menu
56                ON s_menu.parent_id = root_menu.id AND root_menu.parent_id IS NULL
57             WHERE t.lang IN %s and t.module IN %s
58               AND o_menu.website_id IS NULL AND o_menu.parent_id = %s
59               AND s_menu.website_id IS NOT NULL""" + conflict_clause.format(
60                   "(type, lang, name, res_id) WHERE type = 'model'"
61        ), (tuple(langs), tuple(modules), default_menu.id))
62
63        return res
64