From d64b4adf1e8fa1c414e163f117eaa87040875df8 Mon Sep 17 00:00:00 2001 From: Romain Derie Date: Sun, 5 Nov 2023 14:29:19 +0100 Subject: [PATCH] [IMP] website: allow fallback images when industry image not set When creating a new website through the configurator, the images that will be used on the website will be the one specified by the selected industry. We recently introduced 2 new images with commit [1] for which the industries have no images set as we did not have time to do it. It would require going through the ~4000 industries and finding a relevant image on Unsplash for it. This commit is simply using another existing set image instead. Indeed, if an industry hasn't set an image, the theme one will be used instead, which is less ideal. Step to reproduce: - Create a new website - Select "Garden furniture store" - Select the Orchid theme (the one in the middle to this day) - Drag & drop the Banner snippet, 2 out of the 3 images used in this snippet will use the theme images (images about flowers) instead of images of furniture (related to the selected industry) [1]: https://github.com/odoo/odoo/commit/3cbdf754ff8fac8a77887c4307b658cb86b0be1d closes odoo/odoo#141117 Signed-off-by: Quentin Smetz (qsm) --- addons/website/models/website.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/addons/website/models/website.py b/addons/website/models/website.py index a03a5620c13..30aa9975ddf 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -737,6 +737,41 @@ class Website(models.Model): 'noupdate': True, }) + def fallback_create_missing_industry_image(image_name, fallback_img_name): + """ If an industry did not specify an image, this method allows that + specific image to be using the same image as another fallback one. + """ + image_name = f'website.{image_name}' + if ( + image_name not in images.keys() + and f'website.{fallback_img_name}' in images.keys() + ): + extn_identifier = 'configurator_%s_%s' % (website.id, image_name.split('.')[1]) + if extn_identifier not in names: + attachment = self.env['ir.attachment'].create({ + 'name': image_name, + 'website_id': website.id, + 'key': image_name, + 'type': 'binary', + 'raw': self.env.ref(f'website.configurator_{website.id}_{fallback_img_name}').raw, + 'public': True, + }) + self.env['ir.model.data'].create({ + 'name': extn_identifier, + 'module': 'website', + 'model': 'ir.attachment', + 'res_id': attachment.id, + 'noupdate': True, + }) + + try: + # TODO: Remove this try/except, safety net because it was merged + # to close to OXP. + fallback_create_missing_industry_image('s_banner_default_image_2', 's_image_text_default_image') + fallback_create_missing_industry_image('s_banner_default_image_3', 's_product_list_default_image_1') + except Exception: + pass + return {'url': redirect_url, 'website_id': website.id} # ----------------------------------------------------------