From 653e49695e051e4de96d65dde59eb923fe0eb4a6 Mon Sep 17 00:00:00 2001 From: Jeremy Kersten Date: Fri, 24 Sep 2021 09:15:37 +0000 Subject: [PATCH] [FIX] website: svg preview has been moved in theme repo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now, iap only return images urls needed to process the svg. We don't need to 'download' svg preprocess, we use the svg locally and make the substitution ourself. It simplify the maintenance: Preview of theme are in theme repository It make it blazing fast: Response contains a list of string, and not more a big string, the svg. closes odoo/odoo#77199 X-original-commit: 103ec4debb539b04b1783be20f1ccf3e18ab377e Related: odoo/design-themes#503 Signed-off-by: Jérémy Kersten (jke) Signed-off-by: Quentin Smetz (qsm) --- .../tests/test_configurator.py | 2 +- addons/website/models/website.py | 13 ++++--- .../models/website_configurator_feature.py | 37 ++++++++++++++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/addons/test_website_modules/tests/test_configurator.py b/addons/test_website_modules/tests/test_configurator.py index a4f8de827e0..5666929fbb3 100644 --- a/addons/test_website_modules/tests/test_configurator.py +++ b/addons/test_website_modules/tests/test_configurator.py @@ -27,7 +27,7 @@ class TestConfigurator(odoo.tests.HttpCase): {"id": 4, "label": "abortion clinic"}, {"id": 5, "label": "abrasives supplier"}, {"id": 6, "label": "abundant life church"}]} - elif '/api/website/1/configurator/recommended_themes' in endpoint: + elif '/api/website/2/configurator/recommended_themes' in endpoint: return [] elif '/api/website/1/configurator/custom_resources/' in endpoint: return { diff --git a/addons/website/models/website.py b/addons/website/models/website.py index 9d2c829246d..bba49c8a7a6 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -333,11 +333,14 @@ class Website(models.Model): domain = [('name', '=like', 'theme%'), ('name', 'not in', ['theme_default', 'theme_common'])] client_themes = request.env['ir.module.module'].search(domain).mapped('name') client_themes_img = dict([(t, http.addons_manifest[t].get('images_preview_theme', {})) for t in client_themes]) - params = { - 'palette': palette, - 'client_themes': client_themes_img, - } - return self._website_api_rpc('/api/website/1/configurator/recommended_themes/%s' % industry_id, params) + themes_suggested = self._website_api_rpc( + '/api/website/2/configurator/recommended_themes/%s' % industry_id, + {'client_themes': client_themes_img} + ) + process_svg = self.env['website.configurator.feature']._process_svg + for theme in themes_suggested: + theme['svg'] = process_svg(theme['name'], palette, theme.pop('image_urls')) + return themes_suggested @api.model def configurator_skip(self): diff --git a/addons/website/models/website_configurator_feature.py b/addons/website/models/website_configurator_feature.py index 75aac966a1b..ce59015dd6f 100644 --- a/addons/website/models/website_configurator_feature.py +++ b/addons/website/models/website_configurator_feature.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models, _ +import re + +from odoo import api, fields, models, tools, _ from odoo.exceptions import ValidationError +from odoo.modules.module import get_resource_path class WebsiteConfiguratorFeature(models.Model): @@ -27,3 +30,35 @@ class WebsiteConfiguratorFeature(models.Model): def _check_module_xor_page_view(self): if bool(self.module_id) == bool(self.page_view_id): raise ValidationError(_("One and only one of the two fields 'page_view_id' and 'module_id' should be set")) + + @staticmethod + def _process_svg(theme, colors, image_mapping): + svg = None + preview_svg = get_resource_path(theme, 'static', 'description', theme + '.svg') + if not preview_svg: + return False + with tools.file_open(preview_svg, 'r') as file: + svg = file.read() + + default_colors = { + 'color1': '#3AADAA', + 'color2': '#7C6576', + 'color3': '#F6F6F6', + 'color4': '#FFFFFF', + 'color5': '#383E45', + 'menu': '#MENU_COLOR', + 'footer': '#FOOTER_COLOR', + } + color_mapping = {default_colors[color_key]: color_value for color_key, color_value in colors.items() if color_key in default_colors.keys()} + color_regex = '(?i)%s' % '|'.join('(%s)' % color for color in color_mapping.keys()) + image_regex = '(?i)%s' % '|'.join('(%s)' % image for image in image_mapping.keys()) + + def subber_maker(mapping): + def subber(match): + key = match.group() + return mapping[key] if key in mapping else key + return subber + + svg = re.sub(color_regex, subber_maker(color_mapping), svg) + svg = re.sub(image_regex, subber_maker(image_mapping), svg) + return svg