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.

96 lines
3.4 KiB

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