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.

135 lines
5.2 KiB

11 years ago
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.osv import orm, fields
  22. from openerp.tools.translate import _
  23. from openerp.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG
  24. class IrModel(orm.Model):
  25. _inherit = 'ir.model'
  26. def _drop_table(self, cr, uid, ids, context=None):
  27. # Allow to skip this step during model unlink
  28. # The super method crashes if the model cannot be instantiated
  29. if context and context.get('no_drop_table'):
  30. return True
  31. return super(IrModel, self)._drop_table(cr, uid, ids, context=context)
  32. class CleanupPurgeLineModel(orm.TransientModel):
  33. _inherit = 'cleanup.purge.line'
  34. _name = 'cleanup.purge.line.model'
  35. _columns = {
  36. 'wizard_id': fields.many2one(
  37. 'cleanup.purge.wizard.model', 'Purge Wizard', readonly=True),
  38. }
  39. def purge(self, cr, uid, ids, context=None):
  40. """
  41. Unlink models upon manual confirmation.
  42. """
  43. model_pool = self.pool['ir.model']
  44. attachment_pool = self.pool['ir.attachment']
  45. constraint_pool = self.pool['ir.model.constraint']
  46. fields_pool = self.pool['ir.model.fields']
  47. relation_pool = self.pool['ir.model.relation']
  48. local_context = (context or {}).copy()
  49. local_context.update({
  50. MODULE_UNINSTALL_FLAG: True,
  51. 'no_drop_table': True,
  52. })
  53. for line in self.browse(cr, uid, ids, context=context):
  54. cr.execute(
  55. "SELECT id, model from ir_model WHERE model = %s",
  56. (line.name,))
  57. row = cr.fetchone()
  58. if row:
  59. self.logger.info('Purging model %s', row[1])
  60. attachment_ids = attachment_pool.search(
  61. cr, uid, [('res_model', '=', line.name)], context=context)
  62. if attachment_ids:
  63. cr.execute(
  64. "UPDATE ir_attachment SET res_model = FALSE "
  65. "WHERE id in %s",
  66. (tuple(attachment_ids), ))
  67. constraint_ids = constraint_pool.search(
  68. cr, uid, [('model', '=', line.name)], context=context)
  69. if constraint_ids:
  70. constraint_pool.unlink(
  71. cr, uid, constraint_ids, context=context)
  72. relation_ids = fields_pool.search(
  73. cr, uid, [('relation', '=', row[1])], context=context)
  74. for relation in relation_ids:
  75. try:
  76. # Fails if the model on the target side
  77. # cannot be instantiated
  78. fields_pool.unlink(cr, uid, [relation],
  79. context=local_context)
  80. except KeyError:
  81. pass
  82. except AttributeError:
  83. pass
  84. relation_ids = relation_pool.search(
  85. cr, uid, [('model', '=', line.name)], context=context)
  86. for relation in relation_ids:
  87. relation_pool.unlink(cr, uid, [relation],
  88. context=local_context)
  89. model_pool.unlink(cr, uid, [row[0]], context=local_context)
  90. line.write({'purged': True})
  91. cr.commit()
  92. return True
  93. class CleanupPurgeWizardModel(orm.TransientModel):
  94. _inherit = 'cleanup.purge.wizard'
  95. _name = 'cleanup.purge.wizard.model'
  96. def default_get(self, cr, uid, fields, context=None):
  97. res = super(CleanupPurgeWizardModel, self).default_get(
  98. cr, uid, fields, context=context)
  99. if 'name' in fields:
  100. res['name'] = _('Purge models')
  101. return res
  102. def find(self, cr, uid, context=None):
  103. """
  104. Search for models that cannot be instantiated.
  105. """
  106. res = []
  107. cr.execute("SELECT model from ir_model")
  108. for (model,) in cr.fetchall():
  109. if not self.pool.get(model):
  110. res.append((0, 0, {'name': model}))
  111. if not res:
  112. raise orm.except_orm(
  113. _('Nothing to do'),
  114. _('No orphaned models found'))
  115. return res
  116. _columns = {
  117. 'purge_line_ids': fields.one2many(
  118. 'cleanup.purge.line.model',
  119. 'wizard_id', 'Models to purge'),
  120. }