Skip to content

Commit e3ed195

Browse files
refactor(spp_audit): use isinstance for Markup checks, simplify loops
Replace fragile str(type(...)) comparisons with isinstance(value, Markup) and restructure sanitization loops to iterate each record's items directly instead of extracting keys from the first record. Addresses gemini-code-assist review feedback on PR #138.
1 parent dc66d1d commit e3ed195

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

spp_audit/tools/decorator.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import copy
55
import logging
66

7+
from markupsafe import Markup
8+
79
from odoo import api
810

911
_logger = logging.getLogger(__name__)
@@ -35,11 +37,10 @@ def audit_create(self, vals_list):
3537
)
3638
)
3739
if new_values:
38-
keys = new_values[0].keys()
39-
for key in keys:
40-
for nv in new_values:
41-
if str(type(nv[key])) == "<class 'markupsafe.Markup'>":
42-
nv[key] = str(nv[key])
40+
for nv in new_values:
41+
for key, value in nv.items():
42+
if isinstance(value, Markup):
43+
nv[key] = str(value)
4344

4445
rules.log("create", new_values=new_values)
4546
return result
@@ -71,13 +72,14 @@ def audit_write(self, vals):
7172
)
7273

7374
if new_values and old_values_copy:
74-
keys = new_values[0].keys()
75-
for key in keys:
76-
for nv, ov in zip(new_values, old_values_copy, strict=False):
77-
if str(type(nv[key])) == "<class 'markupsafe.Markup'>":
78-
nv[key] = str(nv[key])
79-
if str(type(ov[key])) == "<class 'markupsafe.Markup'>":
80-
ov[key] = str(ov[key])
75+
for nv in new_values:
76+
for key, value in nv.items():
77+
if isinstance(value, Markup):
78+
nv[key] = str(value)
79+
for ov in old_values_copy:
80+
for key, value in ov.items():
81+
if isinstance(value, Markup):
82+
ov[key] = str(value)
8183

8284
rules.log("write", old_values_copy, new_values)
8385
return result
@@ -92,11 +94,10 @@ def audit_unlink(self):
9294
)
9395

9496
if old_values:
95-
keys = old_values[0].keys()
96-
for key in keys:
97-
for ov in old_values:
98-
if str(type(ov[key])) == "<class 'markupsafe.Markup'>":
99-
ov[key] = str(ov[key])
97+
for ov in old_values:
98+
for key, value in ov.items():
99+
if isinstance(value, Markup):
100+
ov[key] = str(value)
100101

101102
rules.log("unlink", old_values)
102103
return audit_unlink.origin(self)

0 commit comments

Comments
 (0)