11# Copyright 2025 Akretion (http://www.akretion.com).
22# @author Florian Mounier <florian.mounier@akretion.com>
3+ # Copyright 2025 Simone Rubino - PyTech
34# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
5+
46import logging
57
6- from odoo import registry , tools
78from odoo .http import _dispatchers
89
910from odoo .addons .fastapi .fastapi_dispatcher import (
@@ -27,16 +28,8 @@ def dispatch(self, endpoint, args):
2728 ._get_endpoint (environ ["PATH_INFO" ])
2829 )
2930 if fastapi_endpoint .log_requests :
30- if tools .config ["test_enable" ]:
31- cr = getattr (
32- self .request .env .registry , "test_log_cr" , self .request .env .cr
33- )
34- else :
35- # Create an independent cursor
36- cr = registry (self .request .env .cr .dbname ).cursor ()
37-
38- env = self .request .env (cr = cr , su = True )
3931 request = self .request
32+ env = request .env (su = True )
4033 try :
4134 log = env ["api.log" ].log_request (request )
4235 except Exception as e :
@@ -50,18 +43,17 @@ def dispatch(self, endpoint, args):
5043 log and log .log_exception (response_exc )
5144 except Exception as e :
5245 _logger .warning ("Failed to log exception" , exc_info = e )
46+ else :
47+ # Be sure to commit/save the exception's log
48+ env .cr .commit ()
49+
5350 raise response_exc
5451 else :
5552 try :
5653 log and log .log_response (response )
5754 except Exception as e :
5855 _logger .warning ("Failed to log response" , exc_info = e )
59- finally :
60- if not tools .config ["test_enable" ]:
61- try :
62- cr .commit () # pylint: disable=E8102
63- finally :
64- cr .close ()
56+
6557 return response
6658 else :
6759 return super ().dispatch (endpoint , args )
0 commit comments