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.

81 lines
2.9 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 openerp import _, api, fields, models
  5. from openerp.exceptions import UserError
  6. from openerp.modules.registry import RegistryManager
  7. from openerp.modules.module import get_module_path
  8. from openerp.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 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. module_names = self.filtered(lambda x: not x.purged).mapped('name')
  38. modules = self.env['ir.module.module'].search([
  39. ('name', 'in', module_names)
  40. ])
  41. if not modules:
  42. return True
  43. self.logger.info('Purging modules %s', ', '.join(module_names))
  44. modules.write({'state': 'to remove'})
  45. # we need this commit because reloading the registry would roll back
  46. # our changes
  47. self.env.cr.commit() # pylint: disable=invalid-commit
  48. RegistryManager.new(self.env.cr.dbname, update_module=True)
  49. modules.unlink()
  50. return self.write({'purged': True})
  51. class CleanupPurgeWizardModule(models.TransientModel):
  52. _inherit = 'cleanup.purge.wizard'
  53. _name = 'cleanup.purge.wizard.module'
  54. _description = 'Purge modules'
  55. @api.model
  56. def find(self):
  57. res = []
  58. for module in self.env['ir.module.module'].search([]):
  59. if get_module_path(module.name):
  60. continue
  61. if module.state == 'uninstalled':
  62. module.unlink()
  63. continue
  64. res.append((0, 0, {'name': module.name}))
  65. if not res:
  66. raise UserError(_('No modules found to purge'))
  67. return res
  68. purge_line_ids = fields.One2many(
  69. 'cleanup.purge.line.module', 'wizard_id', 'Modules to purge')