Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Apr 6, 2021 · 1 comment

Comments

@ghost
Copy link

ghost commented Apr 6, 2021

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.

@ghost
Copy link
Author

ghost commented Apr 6, 2021

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 {}

@ghost ghost closed this as completed Apr 6, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants