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.

127 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. ids = self.search(cr, uid, [('module', 'in', modules_to_remove)])
  42. for this in self.browse(cr, uid, ids, context=context):
  43. if this.model == 'ir.model.fields':
  44. field = self.pool[this.model].browse(
  45. cr, uid, [this.res_id],
  46. context=dict(
  47. context or {}, **{MODULE_UNINSTALL_FLAG: True}))[0]
  48. if not self.pool.get(field.model):
  49. this.unlink()
  50. continue
  51. if not self.pool.get(this.model):
  52. this.unlink()
  53. return super(IrModelData, self)._module_data_uninstall(
  54. cr, uid, modules_to_remove, context=context)
  55. class CleanupPurgeLineModule(orm.TransientModel):
  56. _inherit = 'cleanup.purge.line'
  57. _name = 'cleanup.purge.line.module'
  58. _columns = {
  59. 'wizard_id': fields.many2one(
  60. 'cleanup.purge.wizard.module', 'Purge Wizard', readonly=True),
  61. }
  62. def purge(self, cr, uid, ids, context=None):
  63. """
  64. Uninstall modules upon manual confirmation, then reload
  65. the database.
  66. """
  67. module_pool = self.pool['ir.module.module']
  68. lines = self.browse(cr, uid, ids, context=context)
  69. module_names = [line.name for line in lines if not line.purged]
  70. module_ids = module_pool.search(
  71. cr, uid, [('name', 'in', module_names)], context=context)
  72. if not module_ids:
  73. return True
  74. self.logger.info('Purging modules %s', ', '.join(module_names))
  75. module_pool.write(
  76. cr, uid, module_ids, {'state': 'to remove'}, context=context)
  77. cr.commit()
  78. _db, _pool = pooler.restart_pool(cr.dbname, update_module=True)
  79. module_pool.unlink(cr, uid, module_ids, context=context)
  80. return self.write(cr, uid, ids, {'purged': True}, context=context)
  81. class CleanupPurgeWizardModule(orm.TransientModel):
  82. _inherit = 'cleanup.purge.wizard'
  83. _name = 'cleanup.purge.wizard.module'
  84. def default_get(self, cr, uid, fields, context=None):
  85. res = super(CleanupPurgeWizardModule, self).default_get(
  86. cr, uid, fields, context=context)
  87. if 'name' in fields:
  88. res['name'] = _('Purge modules')
  89. return res
  90. def find(self, cr, uid, context=None):
  91. module_pool = self.pool['ir.module.module']
  92. module_ids = module_pool.search(cr, uid, [], context=context)
  93. res = []
  94. for module in module_pool.browse(cr, uid, module_ids, context=context):
  95. if get_module_path(module.name):
  96. continue
  97. if module.state == 'uninstalled':
  98. module_pool.unlink(cr, uid, module.id, context=context)
  99. continue
  100. res.append((0, 0, {'name': module.name}))
  101. if not res:
  102. raise orm.except_orm(
  103. _('Nothing to do'),
  104. _('No modules found to purge'))
  105. return res
  106. _columns = {
  107. 'purge_line_ids': fields.one2many(
  108. 'cleanup.purge.line.module',
  109. 'wizard_id', 'Modules to purge'),
  110. }