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.

79 lines
2.7 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. self.env.cr.commit()
  46. RegistryManager.new(self.env.cr.dbname, update_module=True)
  47. modules.unlink()
  48. return self.write({'purged': True})
  49. class CleanupPurgeWizardModule(models.TransientModel):
  50. _inherit = 'cleanup.purge.wizard'
  51. _name = 'cleanup.purge.wizard.module'
  52. _description = 'Purge modules'
  53. @api.model
  54. def find(self):
  55. res = []
  56. for module in self.env['ir.module.module'].search([]):
  57. if get_module_path(module.name):
  58. continue
  59. if module.state == 'uninstalled':
  60. module.unlink()
  61. continue
  62. res.append((0, 0, {'name': module.name}))
  63. if not res:
  64. raise UserError(_('No modules found to purge'))
  65. return res
  66. purge_line_ids = fields.One2many(
  67. 'cleanup.purge.line.module', 'wizard_id', 'Modules to purge')