[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", "name": "Audit Log",
"version": "14.0.1.2.0", "version": "15.0.1.0.0",
"author": "ABF OSIELL,Odoo Community Association (OCA)", "author": "ABF OSIELL, Odoo Community Association (OCA)",
"license": "AGPL-3", "license": "AGPL-3",
"website": "https://github.com/OCA/server-tools", "website": "https://github.com/OCA/server-tools",
"category": "Tools", "category": "Tools",

View File

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

View File

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

View File

@ -8,3 +8,4 @@
* Bhavesh Odedra <bodedra@opensourceintegrators.com> * Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Hardik Suthar <hsuthar@opensourceintegrators.com> * Hardik Suthar <hsuthar@opensourceintegrators.com>
* Kitti U. <kittiu@ecosoft.co.th> * 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> # © 2021 Stefan Rijnhart <stefan@opener.amsterdam>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.modules.migration import load_script 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 from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
@ -237,7 +237,7 @@ class TestAuditlogFast(TransactionCase, AuditlogCommon):
super(TestAuditlogFast, self).tearDown() super(TestAuditlogFast, self).tearDown()
class TestFieldRemoval(SavepointCase): class TestFieldRemoval(TransactionCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
@ -247,12 +247,20 @@ class TestFieldRemoval(SavepointCase):
existing_audit_logs.unlink() existing_audit_logs.unlink()
# Create a test model to remove # Create a test model to remove
cls.test_model = cls.env["ir.model"].create( cls.test_model = (
{"name": "x_test_model", "model": "x_test.model", "state": "manual"} cls.env["ir.model"]
.sudo()
.create(
[{"name": "x_test_model", "model": "x_test.model", "state": "manual"}]
)
) )
# Create a test model field to remove # Create a test model field to remove
cls.test_field = cls.env["ir.model.fields"].create( cls.test_field = (
cls.env["ir.model.fields"]
.sudo()
.create(
[
{ {
"name": "x_test_field", "name": "x_test_field",
"field_description": "x_Test Field", "field_description": "x_Test Field",
@ -260,10 +268,12 @@ class TestFieldRemoval(SavepointCase):
"ttype": "char", "ttype": "char",
"state": "manual", "state": "manual",
} }
]
)
) )
# Setup auditlog rule # Setup auditlog rule
cls.auditlog_rule = cls.env["auditlog.rule"].create( cls.auditlog_rule = cls.env["auditlog.rule"].create(
[
{ {
"name": "test.model", "name": "test.model",
"model_id": cls.test_model.id, "model_id": cls.test_model.id,
@ -273,6 +283,7 @@ class TestFieldRemoval(SavepointCase):
"log_write": True, "log_write": True,
"log_unlink": False, "log_unlink": False,
} }
]
) )
cls.auditlog_rule.subscribe() cls.auditlog_rule.subscribe()
@ -305,13 +316,13 @@ class TestFieldRemoval(SavepointCase):
self.assert_values() self.assert_values()
# Remove the field # 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() self.assert_values()
# The field should not be linked # The field should not be linked
self.assertFalse(self.logs.mapped("line_ids.field_id")) self.assertFalse(self.logs.mapped("line_ids.field_id"))
# Remove the model # 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() self.assert_values()
# The model should not be linked # The model should not be linked

View File

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