Skip to content

Commit d693007

Browse files
committed
[FIX] auditlog: Ensure display_name computation works with new record.
1 parent b80bca1 commit d693007

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

auditlog/models/http_request.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class AuditlogHTTPRequest(models.Model):
2525
@api.depends("create_date", "name")
2626
def _compute_display_name(self):
2727
for httprequest in self:
28-
create_date = fields.Datetime.from_string(httprequest.create_date)
28+
create_date = (
29+
fields.Datetime.from_string(httprequest.create_date)
30+
or fields.Datetime.now()
31+
)
2932
tz_create_date = fields.Datetime.context_timestamp(httprequest, create_date)
3033
httprequest.display_name = "{} ({})".format(
3134
httprequest.name or "?", fields.Datetime.to_string(tz_create_date)

auditlog/models/http_session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ class AuditlogtHTTPSession(models.Model):
2020
@api.depends("create_date", "user_id")
2121
def _compute_display_name(self):
2222
for httpsession in self:
23-
create_date = fields.Datetime.from_string(httpsession.create_date)
23+
create_date = (
24+
fields.Datetime.from_string(httpsession.create_date)
25+
or fields.Datetime.now()
26+
)
2427
tz_create_date = fields.Datetime.context_timestamp(httpsession, create_date)
2528
httpsession.display_name = "{} ({})".format(
2629
httpsession.user_id and httpsession.user_id.name or "?",

auditlog/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
22
from . import test_auditlog
33
from . import test_autovacuum
4+
from . import test_http
45
from . import test_multi_company

auditlog/tests/test_http.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
2+
from odoo.tests.common import HttpCase, tagged
3+
4+
5+
@tagged("post_install", "-at_install")
6+
class TestAuditlogHttp(HttpCase):
7+
def test_compute_display_name(self):
8+
self.authenticate("admin", "admin")
9+
rule = self.env["auditlog.rule"].create(
10+
{
11+
"name": "res.partner",
12+
"model_id": self.env.ref("base.model_res_partner").id,
13+
"log_type": "full",
14+
"state": "subscribed",
15+
}
16+
)
17+
self.addCleanup(rule.unsubscribe)
18+
partner = self.env.ref("base.partner_demo")
19+
self.make_jsonrpc_request(
20+
"/web/dataset/call_kw",
21+
params={
22+
"model": "res.partner",
23+
"method": "write",
24+
"args": [partner.id, {"name": "test"}],
25+
"kwargs": {},
26+
},
27+
headers={
28+
"Cookie": f"session_id={self.session.sid};",
29+
},
30+
)
31+
logs = self.env["auditlog.log"].search(
32+
[("model_id", "=", rule.model_id.id), ("res_id", "=", partner.id)]
33+
)
34+
self.assertEqual(len(logs), 1)
35+
http_request_id = logs[0]["http_request_id"]
36+
self.assertRegex(
37+
http_request_id.display_name,
38+
r"/web/dataset/call_kw \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\)",
39+
)
40+
http_session_id = logs[0]["http_session_id"]
41+
self.assertRegex(
42+
http_session_id.display_name,
43+
r"Mitchell Admin \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\)",
44+
)

0 commit comments

Comments
 (0)