[MIG] auditlog: Migration to 15.0

pull/2466/head
BT-vgabor 2022-01-31 15:25:37 +01:00 committed by Stefan Rijnhart
parent 3059c6b20c
commit 6dfa4ae522
6 changed files with 51 additions and 49 deletions

View File

@ -3,8 +3,8 @@
{
"name": "Audit Log",
"version": "14.0.1.2.0",
"author": "ABF OSIELL,Odoo Community Association (OCA)",
"version": "15.0.1.0.0",
"author": "ABF OSIELL, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/server-tools",
"category": "Tools",

View File

@ -31,7 +31,7 @@ class AuditlogLog(models.Model):
for vals in vals_list:
if not vals.get("model_id"):
raise UserError(_("No model defined to create log."))
model = self.env["ir.model"].browse(vals["model_id"])
model = self.env["ir.model"].sudo().browse(vals["model_id"])
vals.update({"model_name": model.name, "model_model": model.model})
return super().create(vals_list)
@ -41,7 +41,7 @@ class AuditlogLog(models.Model):
if "model_id" in vals:
if not vals["model_id"]:
raise UserError(_("The field 'model_id' cannot be empty."))
model = self.env["ir.model"].browse(vals["model_id"])
model = self.env["ir.model"].sudo().browse(vals["model_id"])
vals.update({"model_name": model.name, "model_model": model.model})
return super().write(vals)
@ -81,7 +81,7 @@ class AuditlogLogLine(models.Model):
for vals in vals_list:
if not vals.get("field_id"):
raise UserError(_("No field defined to create line."))
field = self.env["ir.model.fields"].browse(vals["field_id"])
field = self.env["ir.model.fields"].sudo().browse(vals["field_id"])
vals.update(
{"field_name": field.name, "field_description": field.field_description}
)
@ -93,7 +93,7 @@ class AuditlogLogLine(models.Model):
if "field_id" in vals:
if not vals["field_id"]:
raise UserError(_("The field 'field_id' cannot be empty."))
field = self.env["ir.model.fields"].browse(vals["field_id"])
field = self.env["ir.model.fields"].sudo().browse(vals["field_id"])
vals.update(
{"field_name": field.name, "field_description": field.field_description}
)

View File

@ -120,14 +120,7 @@ class AuditlogRule(models.Model):
),
states={"subscribed": [("readonly", True)]},
)
# log_action = fields.Boolean(
# "Log Action",
# help=("Select this if you want to keep track of actions on the "
# "model of this rule"))
# log_workflow = fields.Boolean(
# "Log Workflow",
# help=("Select this if you want to keep track of workflow on any "
# "record of the model of this rule"))
state = fields.Selection(
[("draft", "Draft"), ("subscribed", "Subscribed")],
required=True,
@ -139,7 +132,6 @@ class AuditlogRule(models.Model):
states={"subscribed": [("readonly", True)]},
)
capture_record = fields.Boolean(
"Capture Record",
help="Select this if you want to keep track of Unlink Record",
)
@ -170,10 +162,9 @@ class AuditlogRule(models.Model):
updated = False
model_cache = self.pool._auditlog_model_cache
for rule in self:
if rule.state != "subscribed":
continue
if not self.pool.get(rule.model_id.model or rule.model_model):
# ignore rules for models not loadable currently
if rule.state != "subscribed" or not self.pool.get(
rule.model_id.model or rule.model_model
):
continue
model_cache[rule.model_id.model] = rule.model_id.id
model_model = self.env[rule.model_id.model or rule.model_model]
@ -224,7 +215,7 @@ class AuditlogRule(models.Model):
"""Update the registry when a new rule is created."""
if "model_id" not in vals or not vals["model_id"]:
raise UserError(_("No model defined to create line."))
model = self.env["ir.model"].browse(vals["model_id"])
model = self.env["ir.model"].sudo().browse(vals["model_id"])
vals.update({"model_name": model.name, "model_model": model.model})
new_record = super().create(vals)
if new_record._register_hook():
@ -236,7 +227,7 @@ class AuditlogRule(models.Model):
if "model_id" in vals:
if not vals["model_id"]:
raise UserError(_("Field 'model_id' cannot be empty."))
model = self.env["ir.model"].browse(vals["model_id"])
model = self.env["ir.model"].sudo().browse(vals["model_id"])
vals.update({"model_name": model.name, "model_model": model.model})
res = super().write(vals)
if self._register_hook():
@ -519,7 +510,7 @@ class AuditlogRule(models.Model):
# - we use 'search()' then 'read()' instead of the 'search_read()'
# to take advantage of the 'classic_write' loading
# - search the field in the current model and those it inherits
field_model = self.env["ir.model.fields"]
field_model = self.env["ir.model.fields"].sudo()
all_model_ids = [model.id]
all_model_ids.extend(model.inherited_model_ids.ids)
field = field_model.search(
@ -674,5 +665,4 @@ class AuditlogRule(models.Model):
act_window = rule.action_id
if act_window:
act_window.unlink()
self.write({"state": "draft"})
return True
return self.write({"state": "draft"})

View File

@ -8,3 +8,4 @@
* Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Hardik Suthar <hsuthar@opensourceintegrators.com>
* Kitti U. <kittiu@ecosoft.co.th>
* Bogdan Valentin Gabor <valentin.gabor@bt-group.com>

View File

@ -3,7 +3,7 @@
# © 2021 Stefan Rijnhart <stefan@opener.amsterdam>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.modules.migration import load_script
from odoo.tests.common import SavepointCase, TransactionCase
from odoo.tests.common import TransactionCase
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
@ -237,7 +237,7 @@ class TestAuditlogFast(TransactionCase, AuditlogCommon):
super(TestAuditlogFast, self).tearDown()
class TestFieldRemoval(SavepointCase):
class TestFieldRemoval(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
@ -247,32 +247,43 @@ class TestFieldRemoval(SavepointCase):
existing_audit_logs.unlink()
# Create a test model to remove
cls.test_model = cls.env["ir.model"].create(
{"name": "x_test_model", "model": "x_test.model", "state": "manual"}
cls.test_model = (
cls.env["ir.model"]
.sudo()
.create(
[{"name": "x_test_model", "model": "x_test.model", "state": "manual"}]
)
)
# Create a test model field to remove
cls.test_field = cls.env["ir.model.fields"].create(
{
"name": "x_test_field",
"field_description": "x_Test Field",
"model_id": cls.test_model.id,
"ttype": "char",
"state": "manual",
}
cls.test_field = (
cls.env["ir.model.fields"]
.sudo()
.create(
[
{
"name": "x_test_field",
"field_description": "x_Test Field",
"model_id": cls.test_model.id,
"ttype": "char",
"state": "manual",
}
]
)
)
# Setup auditlog rule
cls.auditlog_rule = cls.env["auditlog.rule"].create(
{
"name": "test.model",
"model_id": cls.test_model.id,
"log_type": "fast",
"log_read": False,
"log_create": True,
"log_write": True,
"log_unlink": False,
}
[
{
"name": "test.model",
"model_id": cls.test_model.id,
"log_type": "fast",
"log_read": False,
"log_create": True,
"log_write": True,
"log_unlink": False,
}
]
)
cls.auditlog_rule.subscribe()
@ -305,13 +316,13 @@ class TestFieldRemoval(SavepointCase):
self.assert_values()
# Remove the field
self.test_field.with_context({MODULE_UNINSTALL_FLAG: True}).unlink()
self.test_field.with_context(**{MODULE_UNINSTALL_FLAG: True}).unlink()
self.assert_values()
# The field should not be linked
self.assertFalse(self.logs.mapped("line_ids.field_id"))
# Remove the model
self.test_model.with_context({MODULE_UNINSTALL_FLAG: True}).unlink()
self.test_model.with_context(**{MODULE_UNINSTALL_FLAG: True}).unlink()
self.assert_values()
# The model should not be linked

View File

@ -262,7 +262,7 @@
<field name="name">view.auditlog.line.tree</field>
<field name="model">auditlog.log.line</field>
<field name="arch" type="xml">
<tree string="Log Lines" create="0">
<tree create="0">
<field name="create_date" optional="show" />
<field name="user_id" optional="show" />
<field name="http_session_id" optional="hide" />