From 79f245444dbc4d36e533fb8e5d0da042bbaf0e93 Mon Sep 17 00:00:00 2001 From: Eduard Brahas Date: Thu, 31 Oct 2024 12:00:17 +0100 Subject: [PATCH] [IMP] impersonate_login: Update create_uid for impersonated record creation - Ensure impersonation is properly reflected in record creation - Update related tests to verify correct impersonation behavior --- impersonate_login/models/model.py | 18 +++++++++++++++--- .../tests/test_impersonate_login.py | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/impersonate_login/models/model.py b/impersonate_login/models/model.py index ece3334f9..16ac8a5a9 100644 --- a/impersonate_login/models/model.py +++ b/impersonate_login/models/model.py @@ -2,6 +2,8 @@ # @author Kévin Roche # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from psycopg2.extensions import AsIs + from odoo import api, models from odoo.http import request @@ -9,7 +11,7 @@ class BaseModel(models.AbstractModel): _inherit = "base" - @api.model_create_multi + @api.model def _create(self, data_list): res = super()._create(data_list) if ( @@ -17,8 +19,18 @@ def _create(self, data_list): and request.session.impersonate_from_uid and "create_uid" in self._fields ): - for rec in res: - rec["create_uid"] = request.session.impersonate_from_uid + self.env.cr.execute( + """ + UPDATE %(table)s + SET create_uid = %(impersonator_id)s + WHERE id IN %(record_ids)s + """, + { + "table": AsIs(self._table), + "impersonator_id": request.session.impersonate_from_uid, + "record_ids": tuple(rec.id for rec in res), + }, + ) return res def write(self, vals): diff --git a/impersonate_login/tests/test_impersonate_login.py b/impersonate_login/tests/test_impersonate_login.py index 85ba94b88..41b5e0406 100644 --- a/impersonate_login/tests/test_impersonate_login.py +++ b/impersonate_login/tests/test_impersonate_login.py @@ -123,6 +123,8 @@ def test_01_admin_impersonates_user_demo(self): # Check impersonate log log2 = self.env["impersonate.log"].search([], order="id desc", limit=1) + # Refresh the log1 after the attribute date_end is updated + log1.refresh() self.assertEqual(log1, log2) self.assertTrue(log1.date_start) self.assertTrue(log1.date_end) @@ -254,6 +256,8 @@ def test_04_write_uid(self): data = response.json() result = data["result"] + # Refresh contact to reflect changes in the database self.assertEqual(result, True) + contact.invalidate_cache() self.assertEqual(contact.ref, "abc") self.assertEqual(contact.write_uid, self.admin_user)