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.

129 lines
5.0 KiB

11 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import pooler
  22. from openerp.osv import orm, fields
  23. from openerp.modules.module import get_module_path
  24. from openerp.tools.translate import _
  25. from openerp.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG
  26. class IrModelConstraint(orm.Model):
  27. _inherit = 'ir.model.constraint'
  28. def _module_data_uninstall(self, cr, uid, ids, context=None):
  29. """this function crashes for constraints on undefined models"""
  30. for this in self.browse(cr, uid, ids, context=context):
  31. if not self.pool.get(this.model.model):
  32. ids.remove(this.id)
  33. this.unlink()
  34. return super(IrModelConstraint, self)._module_data_uninstall(
  35. cr, uid, ids, context=context)
  36. class IrModelData(orm.Model):
  37. _inherit = 'ir.model.data'
  38. def _module_data_uninstall(self, cr, uid, modules_to_remove, context=None):
  39. """this function crashes for xmlids on undefined models or fields
  40. referring to undefined models"""
  41. if context is None:
  42. context = {}
  43. ids = self.search(cr, uid, [('module', 'in', modules_to_remove)])
  44. for this in self.browse(cr, uid, ids, context=context):
  45. if this.model == 'ir.model.fields':
  46. ctx = context.copy()
  47. ctx[MODULE_UNINSTALL_FLAG] = True
  48. field = self.pool[this.model].browse(
  49. cr, uid, this.res_id, context=ctx)
  50. if not self.pool.get(field.model):
  51. this.unlink()
  52. continue
  53. if not self.pool.get(this.model):
  54. this.unlink()
  55. return super(IrModelData, self)._module_data_uninstall(
  56. cr, uid, modules_to_remove, context=context)
  57. class CleanupPurgeLineModule(orm.TransientModel):
  58. _inherit = 'cleanup.purge.line'
  59. _name = 'cleanup.purge.line.module'
  60. _columns = {
  61. 'wizard_id': fields.many2one(
  62. 'cleanup.purge.wizard.module', 'Purge Wizard', readonly=True),
  63. }
  64. def purge(self, cr, uid, ids, context=None):
  65. """
  66. Uninstall modules upon manual confirmation, then reload
  67. the database.
  68. """
  69. module_pool = self.pool['ir.module.module']
  70. lines = self.browse(cr, uid, ids, context=context)
  71. module_names = [line.name for line in lines if not line.purged]
  72. module_ids = module_pool.search(
  73. cr, uid, [('name', 'in', module_names)], context=context)
  74. if not module_ids:
  75. return True
  76. self.logger.info('Purging modules %s', ', '.join(module_names))
  77. module_pool.write(
  78. cr, uid, module_ids, {'state': 'to remove'}, context=context)
  79. cr.commit()
  80. _db, _pool = pooler.restart_pool(cr.dbname, update_module=True)
  81. module_pool.unlink(cr, uid, module_ids, context=context)
  82. return self.write(cr, uid, ids, {'purged': True}, context=context)
  83. class CleanupPurgeWizardModule(orm.TransientModel):
  84. _inherit = 'cleanup.purge.wizard'
  85. _name = 'cleanup.purge.wizard.module'
  86. def default_get(self, cr, uid, fields, context=None):
  87. res = super(CleanupPurgeWizardModule, self).default_get(
  88. cr, uid, fields, context=context)
  89. if 'name' in fields:
  90. res['name'] = _('Purge modules')
  91. return res
  92. def find(self, cr, uid, context=None):
  93. module_pool = self.pool['ir.module.module']
  94. module_ids = module_pool.search(cr, uid, [], context=context)
  95. res = []
  96. for module in module_pool.browse(cr, uid, module_ids, context=context):
  97. if get_module_path(module.name):
  98. continue
  99. if module.state == 'uninstalled':
  100. module_pool.unlink(cr, uid, module.id, context=context)
  101. continue
  102. res.append((0, 0, {'name': module.name}))
  103. if not res:
  104. raise orm.except_orm(
  105. _('Nothing to do'),
  106. _('No modules found to purge'))
  107. return res
  108. _columns = {
  109. 'purge_line_ids': fields.one2many(
  110. 'cleanup.purge.line.module',
  111. 'wizard_id', 'Modules to purge'),
  112. }