base_module_uninstall.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class BaseModuleUninstall(models.TransientModel):
  5. _name = "base.module.uninstall"
  6. _description = "Module Uninstall"
  7. show_all = fields.Boolean()
  8. module_id = fields.Many2one(
  9. 'ir.module.module', string="Module", required=True,
  10. domain=[('state', 'in', ['installed', 'to upgrade', 'to install'])],
  11. ondelete='cascade', readonly=True,
  12. )
  13. module_ids = fields.Many2many('ir.module.module', string="Impacted modules",
  14. compute='_compute_module_ids')
  15. model_ids = fields.Many2many('ir.model', string="Impacted data models",
  16. compute='_compute_model_ids')
  17. def _get_modules(self):
  18. """ Return all the modules impacted by self. """
  19. return self.module_id.downstream_dependencies(self.module_id)
  20. @api.depends('module_id', 'show_all')
  21. def _compute_module_ids(self):
  22. for wizard in self:
  23. modules = wizard._get_modules().sorted(lambda m: (not m.application, m.sequence))
  24. wizard.module_ids = modules if wizard.show_all else modules.filtered('application')
  25. def _get_models(self):
  26. """ Return the models (ir.model) to consider for the impact. """
  27. return self.env['ir.model'].search([('transient', '=', False)])
  28. @api.depends('module_ids')
  29. def _compute_model_ids(self):
  30. ir_models = self._get_models()
  31. ir_models_xids = ir_models._get_external_ids()
  32. for wizard in self:
  33. if wizard.module_id:
  34. module_names = set(wizard._get_modules().mapped('name'))
  35. def lost(model):
  36. xids = ir_models_xids.get(model.id, ())
  37. return xids and all(xid.split('.')[0] in module_names for xid in xids)
  38. # find the models that have all their XIDs in the given modules
  39. self.model_ids = ir_models.filtered(lost).sorted('name')
  40. @api.onchange('module_id')
  41. def _onchange_module_id(self):
  42. # if we select a technical module, show technical modules by default
  43. if not self.module_id.application:
  44. self.show_all = True
  45. def action_uninstall(self):
  46. modules = self.module_id
  47. return modules.button_immediate_uninstall()