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.

155 lines
6.1 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. def _inherited_models(self, cr, uid, ids, field_name, arg, context=None):
  33. """this function crashes for undefined models"""
  34. result = dict((i, []) for i in ids)
  35. existing_model_ids = [
  36. this.id for this in self.browse(cr, uid, ids, context=context)
  37. if self.pool.get(this.model)
  38. ]
  39. super_result = super(IrModel, self)._inherited_models(
  40. cr, uid, existing_model_ids, field_name, arg, context=context)
  41. result.update(super_result)
  42. return result
  43. def _register_hook(self, cr):
  44. # patch the function field instead of overwriting it
  45. if self._columns['inherited_model_ids']._fnct !=\
  46. self._inherited_models.__func__:
  47. self._columns['inherited_model_ids']._fnct =\
  48. self._inherited_models.__func__
  49. return super(IrModel, self)._register_hook(cr)
  50. class CleanupPurgeLineModel(orm.TransientModel):
  51. _inherit = 'cleanup.purge.line'
  52. _name = 'cleanup.purge.line.model'
  53. _columns = {
  54. 'wizard_id': fields.many2one(
  55. 'cleanup.purge.wizard.model', 'Purge Wizard', readonly=True),
  56. }
  57. def purge(self, cr, uid, ids, context=None):
  58. """
  59. Unlink models upon manual confirmation.
  60. """
  61. model_pool = self.pool['ir.model']
  62. attachment_pool = self.pool['ir.attachment']
  63. constraint_pool = self.pool['ir.model.constraint']
  64. fields_pool = self.pool['ir.model.fields']
  65. relation_pool = self.pool['ir.model.relation']
  66. local_context = (context or {}).copy()
  67. local_context.update({
  68. MODULE_UNINSTALL_FLAG: True,
  69. 'no_drop_table': True,
  70. })
  71. for line in self.browse(cr, uid, ids, context=context):
  72. cr.execute(
  73. "SELECT id, model from ir_model WHERE model = %s",
  74. (line.name,))
  75. row = cr.fetchone()
  76. if row:
  77. self.logger.info('Purging model %s', row[1])
  78. attachment_ids = attachment_pool.search(
  79. cr, uid, [('res_model', '=', line.name)], context=context)
  80. if attachment_ids:
  81. cr.execute(
  82. "UPDATE ir_attachment SET res_model = FALSE "
  83. "WHERE id in %s",
  84. (tuple(attachment_ids), ))
  85. constraint_ids = constraint_pool.search(
  86. cr, uid, [('model', '=', line.name)], context=context)
  87. if constraint_ids:
  88. constraint_pool.unlink(
  89. cr, uid, constraint_ids, context=context)
  90. relation_ids = fields_pool.search(
  91. cr, uid, [('relation', '=', row[1])], context=context)
  92. for relation in relation_ids:
  93. try:
  94. # Fails if the model on the target side
  95. # cannot be instantiated
  96. fields_pool.unlink(cr, uid, [relation],
  97. context=local_context)
  98. except KeyError:
  99. pass
  100. except AttributeError:
  101. pass
  102. relation_ids = relation_pool.search(
  103. cr, uid, [('model', '=', line.name)], context=context)
  104. for relation in relation_ids:
  105. relation_pool.unlink(cr, uid, [relation],
  106. context=local_context)
  107. model_pool.unlink(cr, uid, [row[0]], context=local_context)
  108. line.write({'purged': True})
  109. cr.commit()
  110. return True
  111. class CleanupPurgeWizardModel(orm.TransientModel):
  112. _inherit = 'cleanup.purge.wizard'
  113. _name = 'cleanup.purge.wizard.model'
  114. def default_get(self, cr, uid, fields, context=None):
  115. res = super(CleanupPurgeWizardModel, self).default_get(
  116. cr, uid, fields, context=context)
  117. if 'name' in fields:
  118. res['name'] = _('Purge models')
  119. return res
  120. def find(self, cr, uid, context=None):
  121. """
  122. Search for models that cannot be instantiated.
  123. """
  124. res = []
  125. cr.execute("SELECT model from ir_model")
  126. for (model,) in cr.fetchall():
  127. if not self.pool.get(model):
  128. res.append((0, 0, {'name': model}))
  129. if not res:
  130. raise orm.except_orm(
  131. _('Nothing to do'),
  132. _('No orphaned models found'))
  133. return res
  134. _columns = {
  135. 'purge_line_ids': fields.one2many(
  136. 'cleanup.purge.line.model',
  137. 'wizard_id', 'Models to purge'),
  138. }