From c5806fe236a98f076022b549ded080c2cad12310 Mon Sep 17 00:00:00 2001 From: Tran Truong Son Date: Wed, 7 Jun 2023 02:32:08 +0000 Subject: [PATCH] [FIX] website: avoid creating duplicate key for configurator image **Description of the issue/feature this PR addresses:** - Increase user experience in creating website themes - Eliminate redundant creation of 1 record leading to unnecessary model `ir_model_data` contraints error **Recreate the Situation** - Install the website module - Choose `a business website` in line 1, `fashion designer` in line 2, `get lead` in line 3 on `configurator - step 2` - Choose random paletee on `configurator - step 3` - `Build my website` on `configurator - step 4` - Suppose in this step that we have a bad connection to the api `https://website.api.odoo.com`, the error `duplicate key value violates unique constraint` will appear. **Reason** - When a user creates a website, the `configurator_apply` function is called to the api `https://website.api.odoo.com`. If the connection time exceeds 5s, the `configurator_apply` function will be called again. Maximum of 3 times - Each time `configurator_apply` function runs, will create records of model `ir_model_data` at `set_images` function. This function will create records with a name field like `configurator_1_s_cover_default_image`. - If this function runs again for the second time, it will also generate a `configurator_1_s_cover_default_image` for the second time. This will result in a `duplicate key value violates unique constraint` error. **Before commit:** - When creating a website theme by industry, if the user's connection to odoo's api has a delay, it will cause an api contraints error of model `ir_model_data` **After commit:** - Check if the record of model `ir_model_data` has been created before, if created then skip closes odoo/odoo#128959 X-original-commit: 56ccf4efeb946e2e0b6ef54b977b6d8b070306ee Signed-off-by: Benoit Socias (bso) --- addons/website/models/website.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/addons/website/models/website.py b/addons/website/models/website.py index 927160afdcb..3a031afd75a 100644 --- a/addons/website/models/website.py +++ b/addons/website/models/website.py @@ -515,7 +515,15 @@ class Website(models.Model): page_view_id.save(value=''.join(rendered_snippets), xpath="(//div[hasclass('oe_structure')])[last()]") def set_images(images): + names = self.env['ir.model.data'].search([ + ('name', '=ilike', f'configurator\\_{website.id}\\_%'), + ('module', '=', 'website'), + ('model', '=', 'ir.attachment') + ]).mapped('name') for name, url in images.items(): + extn_identifier = 'configurator_%s_%s' % (website.id, name.split('.')[1]) + if extn_identifier in names: + continue try: response = requests.get(url, timeout=3) response.raise_for_status() @@ -531,7 +539,7 @@ class Website(models.Model): 'public': True, }) self.env['ir.model.data'].create({ - 'name': 'configurator_%s_%s' % (website.id, name.split('.')[1]), + 'name': extn_identifier, 'module': 'website', 'model': 'ir.attachment', 'res_id': attachment.id,