diff --git a/auditlog/migrations/14.0.1.1.0/pre-migration.py b/auditlog/migrations/14.0.1.1.0/pre-migration.py deleted file mode 100644 index 53a120e98..000000000 --- a/auditlog/migrations/14.0.1.1.0/pre-migration.py +++ /dev/null @@ -1,73 +0,0 @@ -# © 2018 Pieter Paulussen -# 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") diff --git a/auditlog/models/__init__.py b/auditlog/models/__init__.py index d277d21a0..75e7754d2 100644 --- a/auditlog/models/__init__.py +++ b/auditlog/models/__init__.py @@ -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 diff --git a/auditlog/models/auditlog_log_line_view.py b/auditlog/models/auditlog_log_line_view.py new file mode 100644 index 000000000..4703b95eb --- /dev/null +++ b/auditlog/models/auditlog_log_line_view.py @@ -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()) + ) diff --git a/auditlog/models/log.py b/auditlog/models/log.py index ce9e52a85..e6ddeef79 100644 --- a/auditlog/models/log.py +++ b/auditlog/models/log.py @@ -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): diff --git a/auditlog/security/ir.model.access.csv b/auditlog/security/ir.model.access.csv index 09bf61103..d8d83977a 100644 --- a/auditlog/security/ir.model.access.csv +++ b/auditlog/security/ir.model.access.csv @@ -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 diff --git a/auditlog/tests/test_auditlog.py b/auditlog/tests/test_auditlog.py index 78d5959b9..bee1a9024 100644 --- a/auditlog/tests/test_auditlog.py +++ b/auditlog/tests/test_auditlog.py @@ -2,7 +2,6 @@ # © 2018 Pieter Paulussen # © 2021 Stefan Rijnhart # 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): diff --git a/auditlog/views/auditlog_view.xml b/auditlog/views/auditlog_view.xml index 6b297cfd6..2cf98b20b 100644 --- a/auditlog/views/auditlog_view.xml +++ b/auditlog/views/auditlog_view.xml @@ -268,7 +268,7 @@ view.auditlog.line.tree - auditlog.log.line + auditlog.log.line.view @@ -292,7 +292,7 @@ auditlog.line.search - auditlog.log.line + auditlog.log.line.view @@ -348,7 +348,7 @@ Log Lines - auditlog.log.line + auditlog.log.line.view tree {'search_default_group_by_model_id': 1}