From a2ae219d4ee283b7e7af84a3e7fa3bb8e0a95f23 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Wed, 12 Oct 2016 11:05:28 +0200 Subject: [PATCH] [FIX] web_editor, website: fix customize_template_get Commit e11f23b701cd061ee2090e506e7ff71cc2ed8c33 split the website customize_template_get into web_editor and website ones. This split was not perfect and commit 20861b482a0f84c2946cc14a05648b87b95bdac4 tried to make it work... but did not made it totally and added an indirect dependency to website module in web_editor... As the final result is wrong and inconsistent and that the previous 9.0 implementation was already ugly, this was not possible to make a consistent full-stable fix. This commit tries to make a fix as stable as possible (the main method name did not change, the arguments did not change, their purpose is the same, ...) which makes the code work and be less ugly. However, the function returns are now slightly different (and JS depends on it). It returns only valid view information (not header stuff which is not related to anything and the inherit_id value is now a combination of the inherit view id and name). Also, a new private method has been created. Also, the method contained code which was deprecated since saas-6 but was never removed (and never broke anything "by chance"). In master these methods will be refactored to make more sense. --- addons/web_editor/controllers/main.py | 2 +- addons/web_editor/models/ir_ui_view.py | 49 ++++++------------- addons/web_editor/static/src/js/ace.js | 2 +- addons/website/controllers/main.py | 8 +-- addons/website/models/ir_ui_view.py | 30 ++++-------- .../website/static/src/js/website.editor.js | 17 ++++--- 6 files changed, 38 insertions(+), 70 deletions(-) diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index 923b78492b3..5ea843bcaae 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -226,7 +226,7 @@ class Web_Editor(http.Controller): @http.route('/web_editor/customize_template_get', type='json', auth='user', website=True) def customize_template_get(self, key, full=False, bundles=False): """ Get inherit view's informations of the template ``key``. - returns templates (which can be active or not) + returns templates info (which can be active or not) ``bundles=True`` returns also the asset bundles """ return request.env["ir.ui.view"].customize_template_get(key, full=full, bundles=bundles) diff --git a/addons/web_editor/models/ir_ui_view.py b/addons/web_editor/models/ir_ui_view.py index 64ee45b567d..2b0af052c23 100644 --- a/addons/web_editor/models/ir_ui_view.py +++ b/addons/web_editor/models/ir_ui_view.py @@ -175,42 +175,21 @@ class IrUiView(models.Model): return views_to_return @api.model - def customize_template_get(self, key, full=False, bundles=False): - """ Get inherit view's informations of the template ``key``. - returns templates (which can be active or not) + def _customize_template_get_views(self, key, full=False, bundles=False): + """ Get inherit views of the template ``key``. + returns views (which can be active or not) ``bundles=True`` returns also the asset bundles """ - user = self.env.user - user_groups = set(user.groups_id) + user_groups = set(self.env.user.groups_id) views = self.with_context(active_test=False)._views_get(key, bundles=bundles) + views = views.filtered(lambda v: user_groups.issuperset(v.groups_id)) + return views - view_theme_id = self.env['ir.model.data'].xmlid_to_res_id('website.theme') - done = set() - result = [] - for view in views: - if full or (view.customize_show and view.inherit_id.id != view_theme_id): - if not user_groups.issuperset(view.groups_id): - continue - if view.inherit_id not in done: - result.append({ - 'name': view.inherit_id.name, - 'id': view.id, - 'key': view.key, - 'xml_id': view.xml_id, - 'arch': view.arch, - 'inherit_id': view.inherit_id.id, - 'header': True, - 'active': False - }) - done.add(view.inherit_id) - result.append({ - 'name': view.name, - 'id': view.id, - 'key': view.key, - 'xml_id': view.xml_id, - 'arch': view.arch, - 'inherit_id': view.inherit_id.id, - 'header': False, - 'active': view.active, - }) - return result + @api.model + def customize_template_get(self, key, full=False, bundles=False): + """ Get inherit view's informations of the template ``key``. + returns templates info (which can be active or not) + ``bundles=True`` returns also the asset bundles + """ + views = self._customize_template_get_views(key, full=full, bundles=bundles) + return views.read(['name', 'id', 'key', 'xml_id', 'arch', 'active', 'inherit_id']) diff --git a/addons/web_editor/static/src/js/ace.js b/addons/web_editor/static/src/js/ace.js index 5fbe7589265..56500383851 100644 --- a/addons/web_editor/static/src/js/ace.js +++ b/addons/web_editor/static/src/js/ace.js @@ -149,7 +149,7 @@ var ViewEditor = Widget.extend({ view.children = []; }); _.each(index, function (view) { - var parentId = view.inherit_id; + var parentId = view.inherit_id[0]; if (parentId && index[parentId]) { index[parentId].children.push(view); } else { diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index 0826ac839d7..68e3eab78cc 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -262,10 +262,10 @@ class Website(Home): @http.route('/website/customize_template_get', type='json', auth='user', website=True) def customize_template_get(self, key, full=False, bundles=False): - """ Get inherit view's informations of the template ``key``. By default, only - :returns ``customize_show`` templates (which can be active or not), if - ``full=True`` returns inherit view's informations of the template ``key``. - ``bundles=True`` returns also the asset bundles + """ Get inherit view's informations of the template ``key``. + returns templates info (which can be active or not) + ``full=False`` returns only the customize_show template + ``bundles=True`` returns also the asset bundles """ return request.env["ir.ui.view"].customize_template_get(key, full=full, bundles=bundles) diff --git a/addons/website/models/ir_ui_view.py b/addons/website/models/ir_ui_view.py index acd9256ea52..110ce7e39ee 100644 --- a/addons/website/models/ir_ui_view.py +++ b/addons/website/models/ir_ui_view.py @@ -136,25 +136,13 @@ class View(models.Model): return qcontext @api.model - def customize_template_get(self, key, full=False, bundles=False): - """ Get inherit view's informations of the template ``key``. By default, only - :returns ``customize_show`` templates (which can be active or not), if - ``full=True`` returns inherit view's informations of the template ``key``. - ``bundles=True`` returns also the asset bundles + def _customize_template_get_views(self, key, full=False, bundles=False): + """ Get inherit view's informations of the template ``key``. + returns views (which can be active or not) + ``full=False`` returns only the customize_show template + ``bundles=True`` returns also the asset bundles """ - result = super(View, self).customize_template_get(key, full=full, bundles=bundles) - - imd = self.env['ir.model.data'] - view_theme_id = imd.xmlid_to_res_id('website.theme') - - result_filtered = [] - for x in result: - if not full: - view = self.browse(x['id']) - if not view.customize_show: - continue - if view_theme_id and x['inherit_id'] == view_theme_id: - continue - result_filtered.append(x) - - return result_filtered + views = super(View, self)._customize_template_get_views(key, full=full, bundles=bundles) + if full: + return views + return views.filtered(lambda v: v.customize_show) diff --git a/addons/website/static/src/js/website.editor.js b/addons/website/static/src/js/website.editor.js index 5d2d7706560..a110e6eca3e 100644 --- a/addons/website/static/src/js/website.editor.js +++ b/addons/website/static/src/js/website.editor.js @@ -74,16 +74,17 @@ website.TopBarCustomize = Widget.extend({ key: this.view_name, full: false }).then(function (result) { + var current_group = ""; _.each(result, function (item) { - if (item.header) { - $menu.append(''); - } else { - var $li = $('
  • ', {role: 'presentation'}) - .append($('', {href: '#', 'data-view-id': item.id, role: 'menuitem'}) - .append(qweb.render('web_editor.components.switch', {id: 'switch-' + item.id, label: item.name}))); - $li.find('input').prop('checked', !!item.active); - $menu.append($li); + if (current_group !== item.inherit_id[1]) { + current_group = item.inherit_id[1]; + $menu.append("
  • " + current_group + "
  • "); } + var $li = $('
  • ', {role: 'presentation'}) + .append($('', {href: '#', 'data-view-id': item.id, role: 'menuitem'}) + .append(qweb.render('web_editor.components.switch', {id: 'switch-' + item.id, label: item.name}))); + $li.find('input').prop('checked', !!item.active); + $menu.append($li); }); }); },