From 199792e75332a35b30c15ab0efcdc4750f6bf937 Mon Sep 17 00:00:00 2001
From: stcc-odoo <stcc@odoo.com>
Date: Thu, 25 May 2023 14:00:37 +0000
Subject: [PATCH] [FIX] base, website_form: prevent assetsbundle commit in
 savepoint

Steps to reproduce:

- Install industry_fsm_report, website
- Create Project P, add column/stage PC.
- Edit stage, Email Template = Task: Intervention Schduled.
- Edit the template > Advanced Settings > Optional report to print and
  attach = Worksheet Report (PDF). Save everything.
- Go to website, "contact us" page > Edit the form, Action = Create a
  task, Project = P > Save

Issue:

When you first submit the form, it will fail, but the task will be
created and visible in project P. By instinct, the user will submit the
form again, so the task will be duplicated. The second form submit will
return a success message.

When submitting a form, we first generate a savepoint (added in
commit [1]).
Since this is the first interaction with the report system, during the
handling of the form, the assetsbundle will be generated (see keyword
'commit_assetsbundle'), which will cause a commit.
Finally, assuming no other error is raised, we try to delete the
savepoint.
However, since a commit was executed, then the savepoint will no longer
exist, which will cause an error status to be returned.

Solution:

When submitting a form, pass `commit_assetsbundle=False` to the record
creation, which prevents the commit from happening.

This solution has a downside; creating the record also sends an email
and the report attached to that email will have broken styling. This is
still an improvement to the current behaviour, which doesn't send the
first email at all.

[1]: https://github.com/odoo-dev/odoo/commit/5a499ecf113f08c11d2b33b47680dd00ec1b297b

opw-3183912

closes odoo/odoo#122571

X-original-commit: a3249673b9a992a17ff4d1bc4c355c3c49cd7e57
Signed-off-by: Romain Derie (rde) <rde@odoo.com>
---
 addons/website/controllers/form.py           | 5 ++++-
 odoo/addons/base/models/ir_actions_report.py | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/addons/website/controllers/form.py b/addons/website/controllers/form.py
index 4701156b2f80..2bdf643760b3 100644
--- a/addons/website/controllers/form.py
+++ b/addons/website/controllers/form.py
@@ -214,7 +214,10 @@ class WebsiteForm(http.Controller):
         model_name = model.sudo().model
         if model_name == 'mail.mail':
             values.update({'reply_to': values.get('email_from')})
-        record = request.env[model_name].with_user(SUPERUSER_ID).with_context(mail_create_nosubscribe=True).create(values)
+        record = request.env[model_name].with_user(SUPERUSER_ID).with_context(
+            mail_create_nosubscribe=True,
+            commit_assetsbundle=False,
+        ).create(values)
 
         if custom or meta:
             _custom_label = "%s\n___________\n\n" % _("Other Information:")  # Title for custom fields
diff --git a/odoo/addons/base/models/ir_actions_report.py b/odoo/addons/base/models/ir_actions_report.py
index 30f6a73b58e5..a65229af29c0 100644
--- a/odoo/addons/base/models/ir_actions_report.py
+++ b/odoo/addons/base/models/ir_actions_report.py
@@ -849,7 +849,7 @@ class IrActionsReport(models.Model):
         # assets are not in cache and must be generated. To workaround this issue, we manually
         # commit the writes in the `ir.attachment` table. It is done thanks to a key in the context.
         context = dict(self.env.context)
-        if not config['test_enable']:
+        if not config['test_enable'] and 'commit_assetsbundle' not in context:
             context['commit_assetsbundle'] = True
 
         # Disable the debug mode in the PDF rendering in order to not split the assets bundle
-- 
GitLab