[IMP] auditlog: make the line views using a non auto model

pull/2512/head
Enric Tobella 2022-12-21 23:29:08 +01:00
parent 4c733a153a
commit 678bc5a5d2
7 changed files with 74 additions and 120 deletions

View File

@ -1,73 +0,0 @@
# © 2018 Pieter Paulussen <pieter_paulussen@me.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
def migrate(cr, version):
if not version:
return
logger = logging.getLogger(__name__)
logger.info(
"Creating columns: auditlog_log (model_name, model_model) "
"and auditlog_log_line (field_name, field_description)."
)
cr.execute(
"""
ALTER TABLE auditlog_log
ADD COLUMN IF NOT EXISTS model_name VARCHAR,
ADD COLUMN IF NOT EXISTS model_model VARCHAR;
ALTER TABLE auditlog_log_line
ADD COLUMN IF NOT EXISTS field_name VARCHAR,
ADD COLUMN IF NOT EXISTS field_description VARCHAR;
ALTER TABLE auditlog_rule
ADD COLUMN IF NOT EXISTS model_name VARCHAR,
ADD COLUMN IF NOT EXISTS model_model VARCHAR;
"""
)
logger.info(
"Creating indexes on auditlog_log column 'model_id' and "
"auditlog_log_line column 'field_id'."
)
cr.execute(
"""
CREATE INDEX IF NOT EXISTS
auditlog_log_model_id_index ON auditlog_log (model_id);
CREATE INDEX IF NOT EXISTS
auditlog_log_line_field_id_index ON auditlog_log_line (field_id);
"""
)
logger.info(
"Preemptive fill auditlog_log columns: 'model_name' and " "'model_model'."
)
cr.execute(
"""
UPDATE auditlog_log al
SET model_name = im.name, model_model = im.model
FROM ir_model im
WHERE im.id = al.model_id AND model_name IS NULL
"""
)
logger.info(
"Preemtive fill of auditlog_log_line columns: 'field_name' and"
" 'field_description'."
)
cr.execute(
"""
UPDATE auditlog_log_line al
SET field_name = imf.name, field_description = imf.field_description
FROM ir_model_fields imf
WHERE imf.id = al.field_id AND field_name IS NULL
"""
)
logger.info(
"Preemptive fill auditlog_rule columns: 'model_name' and " "'model_model'."
)
cr.execute(
"""
UPDATE auditlog_rule al
SET model_name = im.name, model_model = im.model
FROM ir_model im
WHERE im.id = al.model_id AND model_name IS NULL
"""
)
logger.info("Successfully updated auditlog tables")

View File

@ -4,4 +4,5 @@ from . import rule
from . import http_session
from . import http_request
from . import log
from . import auditlog_log_line_view
from . import autovacuum

View File

@ -0,0 +1,69 @@
from odoo import fields, models, tools
class AuditlogLogLineView(models.Model):
_name = "auditlog.log.line.view"
_inherit = "auditlog.log.line"
_description = "Auditlog - Log details (fields updated)"
_auto = False
_log_access = True
name = fields.Char()
model_id = fields.Many2one("ir.model")
model_name = fields.Char()
model_model = fields.Char()
res_id = fields.Integer()
user_id = fields.Many2one("res.users")
method = fields.Char()
http_session_id = fields.Many2one(
"auditlog.http.session", string="Session", index=True
)
http_request_id = fields.Many2one(
"auditlog.http.request", string="HTTP Request", index=True
)
log_type = fields.Selection(
selection=lambda r: r.env["auditlog.rule"]._fields["log_type"].selection,
string="Type",
)
def _select_query(self):
return """
alogl.id,
alogl.create_date,
alogl.create_uid,
alogl.write_uid,
alogl.write_date,
alogl.field_id,
alogl.log_id,
alogl.old_value,
alogl.new_value,
alogl.old_value_text,
alogl.new_value_text,
alogl.field_name,
alogl.field_description,
alog.name,
alog.model_id,
alog.model_name,
alog.model_model,
alog.res_id,
alog.user_id,
alog.method,
alog.http_session_id,
alog.http_request_id,
alog.log_type
"""
def _from_query(self):
return """
auditlog_log_line alogl
JOIN auditlog_log alog ON alog.id = alogl.log_id
"""
def _query(self):
return "SELECT %s FROM %s" % (self._select_query(), self._from_query())
def init(self):
tools.drop_view_if_exists(self.env.cr, self._table)
self.env.cr.execute(
"""CREATE or REPLACE VIEW %s as (%s)""" % (self._table, self._query())
)

View File

@ -66,21 +66,6 @@ class AuditlogLogLine(models.Model):
new_value_text = fields.Text("New value Text")
field_name = fields.Char("Technical name", readonly=True)
field_description = fields.Char("Description", readonly=True)
# From log auditlog.log
name = fields.Char(related="log_id.name", store=True)
model_id = fields.Many2one(related="log_id.model_id", store=True)
model_name = fields.Char(related="log_id.model_name", store=True)
model_model = fields.Char(related="log_id.model_model", store=True)
res_id = fields.Integer(related="log_id.res_id", store=True)
user_id = fields.Many2one(related="log_id.user_id", store=True)
method = fields.Char(related="log_id.method", store=True)
http_session_id = fields.Many2one(
related="log_id.http_session_id", store=True, index=True
)
http_request_id = fields.Many2one(
related="log_id.http_request_id", store=True, index=True
)
log_type = fields.Selection(related="log_id.log_type", store=True)
@api.model_create_multi
def create(self, vals_list):

View File

@ -11,3 +11,4 @@ access_auditlog_log_line_manager,auditlog_log_line_manager,model_auditlog_log_li
access_auditlog_http_session_manager,auditlog_http_session_manager,model_auditlog_http_session,auditlog.group_auditlog_manager,1,1,1,1
access_auditlog_http_request_manager,auditlog_http_request_manager,model_auditlog_http_request,auditlog.group_auditlog_manager,1,1,1,1
access_auditlog_autovacuum,access_auditlog_autovacuum,model_auditlog_autovacuum,auditlog.group_auditlog_user,1,1,1,1
access_auditlog_log_line_view_manager,auditlog_log_line_view,model_auditlog_log_line_view,base.group_erp_manager,1,0,0,0

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
11 access_auditlog_http_request_manager auditlog_http_request_manager model_auditlog_http_request auditlog.group_auditlog_manager 1 1 1 1
12 access_auditlog_autovacuum access_auditlog_autovacuum model_auditlog_autovacuum auditlog.group_auditlog_user 1 1 1 1
13 access_auditlog_log_line_view_manager auditlog_log_line_view model_auditlog_log_line_view base.group_erp_manager 1 0 0 0
14

View File

@ -2,7 +2,6 @@
# © 2018 Pieter Paulussen <pieter_paulussen@me.com>
# © 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 Form, TransactionCase
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
@ -330,34 +329,6 @@ class TestFieldRemoval(TransactionCase):
# Assert rule values
self.assertFalse(self.auditlog_rule.model_id)
def test_02_migration(self):
"""Test the migration of the data model related to this feature"""
# Drop the data model
self.env.cr.execute(
"""ALTER TABLE auditlog_log
DROP COLUMN model_name, DROP COLUMN model_model"""
)
self.env.cr.execute(
"""ALTER TABLE auditlog_rule
DROP COLUMN model_name, DROP COLUMN model_model"""
)
self.env.cr.execute(
"""ALTER TABLE auditlog_log_line
DROP COLUMN field_name, DROP COLUMN field_description"""
)
# Recreate the data model
mod = load_script(
"auditlog/migrations/14.0.1.1.0/pre-migration.py", "pre-migration"
)
mod.migrate(self.env.cr, "14.0.1.0.2")
# Values are restored
self.assert_values()
# The migration script is tolerant if the data model is already in place
mod.migrate(self.env.cr, "14.0.1.0.2")
class TestAuditlogFullCaptureRecord(TransactionCase, AuditlogCommon):
def setUp(self):

View File

@ -268,7 +268,7 @@
<!-- auditlog.log.line -->
<record model="ir.ui.view" id="view_auditlog_line_tree">
<field name="name">view.auditlog.line.tree</field>
<field name="model">auditlog.log.line</field>
<field name="model">auditlog.log.line.view</field>
<field name="arch" type="xml">
<tree create="0">
<field name="create_date" optional="show" />
@ -292,7 +292,7 @@
</record>
<record id="view_auditlog_line_search" model="ir.ui.view">
<field name="name">auditlog.line.search</field>
<field name="model">auditlog.log.line</field>
<field name="model">auditlog.log.line.view</field>
<field name="arch" type="xml">
<search string="Log Lines">
<field name="name" />
@ -348,7 +348,7 @@
</record>
<record id="action_auditlog_line" model="ir.actions.act_window">
<field name="name">Log Lines</field>
<field name="res_model">auditlog.log.line</field>
<field name="res_model">auditlog.log.line.view</field>
<field name="view_mode">tree</field>
<field name="search_view_id" ref="view_auditlog_line_search" />
<field name="context">{'search_default_group_by_model_id': 1}</field>