You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2014-2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import UserError
  6. from odoo.modules.registry import RegistryManager
  7. from odoo.modules.module import get_module_path
  8. from odoo.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG
  9. class IrModelData(models.Model):
  10. _inherit = 'ir.model.data'
  11. @api.model
  12. def _module_data_uninstall(self, modules_to_remove):
  13. """this function crashes for xmlids on undefined models or fields
  14. referring to undefined models"""
  15. for this in self.search([('module', 'in', modules_to_remove)]):
  16. if this.model == 'ir.model.fields':
  17. field = self.env[this.model].with_context(
  18. **{MODULE_UNINSTALL_FLAG: True}).browse(this.res_id)
  19. if not field.exists() or field.model not in self.env:
  20. this.unlink()
  21. continue
  22. if this.model not in self.env:
  23. this.unlink()
  24. return super(IrModelData, self)._module_data_uninstall(
  25. modules_to_remove)
  26. class CleanupPurgeLineModule(models.TransientModel):
  27. _inherit = 'cleanup.purge.line'
  28. _name = 'cleanup.purge.line.module'
  29. wizard_id = fields.Many2one(
  30. 'cleanup.purge.wizard.module', 'Purge Wizard', readonly=True)
  31. @api.multi
  32. def purge(self):
  33. """
  34. Uninstall modules upon manual confirmation, then reload
  35. the database.
  36. """
  37. if self:
  38. objs = self
  39. else:
  40. objs = self.env['cleanup.purge.line.module']\
  41. .browse(self._context.get('active_ids'))
  42. module_names = objs.filtered(lambda x: not x.purged).mapped('name')
  43. modules = self.env['ir.module.module'].search([
  44. ('name', 'in', module_names)
  45. ])
  46. if not modules:
  47. return True
  48. self.logger.info('Purging modules %s', ', '.join(module_names))
  49. modules.button_uninstall()
  50. # we need this commit because reloading the registry would roll back
  51. # our changes
  52. self.env.cr.commit() # pylint: disable=invalid-commit
  53. RegistryManager.new(self.env.cr.dbname, update_module=True)
  54. modules.unlink()
  55. return objs.write({'purged': True})
  56. class CleanupPurgeWizardModule(models.TransientModel):
  57. _inherit = 'cleanup.purge.wizard'
  58. _name = 'cleanup.purge.wizard.module'
  59. _description = 'Purge modules'
  60. @api.model
  61. def find(self):
  62. res = []
  63. for module in self.env['ir.module.module'].search([]):
  64. if get_module_path(module.name):
  65. continue
  66. if module.state == 'uninstalled':
  67. self.env['cleanup.purge.line.module'].create({
  68. 'name': module.name,
  69. }).purge()
  70. continue
  71. res.append((0, 0, {'name': module.name}))
  72. if not res:
  73. raise UserError(_('No modules found to purge'))
  74. return res
  75. purge_line_ids = fields.One2many(
  76. 'cleanup.purge.line.module', 'wizard_id', 'Modules to purge')