Smile-SA / odoo_addons

Odoo addons developed by Smile

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[13.0] After uninstalling using #87, the actions linked to the rules are leftover and cause exceptions

opened this issue · comments

After using #87 to uninstall I noticed that the models that had rules registered prior to uninstalling the module where still trying to check for audit rules, raising an exception in the process

  File "/home/interpod/Development/odoo/13.0/odoo/addons/base/models/ir_actions.py", line 171, in _compute_search_view
    fvg = self.env[act.res_model].fields_view_get(act.search_view_id.id, 'search')
  File "/home/interpod/Development/odoo/13.0/odoo/api.py", line 463, in __getitem__
    return self.registry[model_name]._browse(self, (), ())
  File "/home/interpod/Development/odoo/13.0/odoo/modules/registry.py", line 177, in __getitem__
    return self.models[model_name]
KeyError: 'audit.log'

For some reason the actions linked to the rules are left over after the installation and are referencing the audit.log model that no longer exists.

To solve the Issue I added an uninstall hook to the module that cleans all the actions leftover from previous uninstallations
in the modules init.py

from . import models
from odoo import api, SUPERUSER_ID

import logging
_logger = logging.getLogger(__name__)


def uninstall_hook(cr, registry):
    # Delete actions linked to audit.log model
    cr.execute("""
        DELETE FROM ir_act_window where res_model='audit.log' RETURNING id
    """)
    deleted_action_ids = [r[0] for r in cr.fetchall()]
    _logger.warning("The following actions have been deleted on 'smile_audit' module uninstallation: %s" % deleted_action_ids)

registered it in the manifest.py

 'uninstall_hook': 'uninstall_hook'

And finally a more subtle version of #87 solution is in the audit_rule.py

    @api.model
    @tools.ormcache()
    def _check_audit_rule(self, group_ids):
        # During Uninstall this function is called after the audit.rule model
        # has been deleted from the registry, so we put a sefeguard
        try:
            rules = self.sudo().search([
                '|',
                ('group_id', '=', False),
                ('group_id', 'in', group_ids),
            ])
            return {rule.model_id.model:
                    {method.replace('_', ''): rule.id
                     for method in self._methods
                     if getattr(rule, 'log_%s' % method.replace('_', ''))}
                    for rule in rules}
        except:
            return {}