[IMP] auditlog: make the line views using a non auto model
parent
4c733a153a
commit
678bc5a5d2
|
@ -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")
|
|
|
@ -4,4 +4,5 @@ from . import rule
|
||||||
from . import http_session
|
from . import http_session
|
||||||
from . import http_request
|
from . import http_request
|
||||||
from . import log
|
from . import log
|
||||||
|
from . import auditlog_log_line_view
|
||||||
from . import autovacuum
|
from . import autovacuum
|
||||||
|
|
|
@ -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())
|
||||||
|
)
|
|
@ -66,21 +66,6 @@ class AuditlogLogLine(models.Model):
|
||||||
new_value_text = fields.Text("New value Text")
|
new_value_text = fields.Text("New value Text")
|
||||||
field_name = fields.Char("Technical name", readonly=True)
|
field_name = fields.Char("Technical name", readonly=True)
|
||||||
field_description = fields.Char("Description", 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
|
@api.model_create_multi
|
||||||
def create(self, vals_list):
|
def create(self, vals_list):
|
||||||
|
|
|
@ -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_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_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_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
|
||||||
|
|
|
|
@ -2,7 +2,6 @@
|
||||||
# © 2018 Pieter Paulussen <pieter_paulussen@me.com>
|
# © 2018 Pieter Paulussen <pieter_paulussen@me.com>
|
||||||
# © 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.tests.common import Form, TransactionCase
|
from odoo.tests.common import Form, TransactionCase
|
||||||
|
|
||||||
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
|
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
|
||||||
|
@ -330,34 +329,6 @@ class TestFieldRemoval(TransactionCase):
|
||||||
# Assert rule values
|
# Assert rule values
|
||||||
self.assertFalse(self.auditlog_rule.model_id)
|
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):
|
class TestAuditlogFullCaptureRecord(TransactionCase, AuditlogCommon):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -268,7 +268,7 @@
|
||||||
<!-- auditlog.log.line -->
|
<!-- auditlog.log.line -->
|
||||||
<record model="ir.ui.view" id="view_auditlog_line_tree">
|
<record model="ir.ui.view" id="view_auditlog_line_tree">
|
||||||
<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.view</field>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<tree create="0">
|
<tree create="0">
|
||||||
<field name="create_date" optional="show" />
|
<field name="create_date" optional="show" />
|
||||||
|
@ -292,7 +292,7 @@
|
||||||
</record>
|
</record>
|
||||||
<record id="view_auditlog_line_search" model="ir.ui.view">
|
<record id="view_auditlog_line_search" model="ir.ui.view">
|
||||||
<field name="name">auditlog.line.search</field>
|
<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">
|
<field name="arch" type="xml">
|
||||||
<search string="Log Lines">
|
<search string="Log Lines">
|
||||||
<field name="name" />
|
<field name="name" />
|
||||||
|
@ -348,7 +348,7 @@
|
||||||
</record>
|
</record>
|
||||||
<record id="action_auditlog_line" model="ir.actions.act_window">
|
<record id="action_auditlog_line" model="ir.actions.act_window">
|
||||||
<field name="name">Log Lines</field>
|
<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="view_mode">tree</field>
|
||||||
<field name="search_view_id" ref="view_auditlog_line_search" />
|
<field name="search_view_id" ref="view_auditlog_line_search" />
|
||||||
<field name="context">{'search_default_group_by_model_id': 1}</field>
|
<field name="context">{'search_default_group_by_model_id': 1}</field>
|
||||||
|
|
Loading…
Reference in New Issue