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.

120 lines
4.1 KiB

8 years ago
  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, models, fields
  5. from openerp.exceptions import UserError
  6. from openerp.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG
  7. class IrModel(models.Model):
  8. _inherit = 'ir.model'
  9. @api.multi
  10. def _drop_table(self):
  11. # Allow to skip this step during model unlink
  12. # The super method crashes if the model cannot be instantiated
  13. if self.env.context.get('no_drop_table'):
  14. return True
  15. return super(IrModel, self)._drop_table()
  16. @api.multi
  17. def _inherited_models(self, field_name, arg):
  18. """this function crashes for undefined models"""
  19. result = dict((i, []) for i in self.ids)
  20. existing_model_ids = [
  21. this.id for this in self if this.model in self.env
  22. ]
  23. super_result = super(IrModel, self.browse(existing_model_ids))\
  24. ._inherited_models(field_name, arg)
  25. result.update(super_result)
  26. return result
  27. def _register_hook(self, cr):
  28. # patch the function field instead of overwriting it
  29. if self._columns['inherited_model_ids']._fnct !=\
  30. self._inherited_models.__func__:
  31. self._columns['inherited_model_ids']._fnct =\
  32. self._inherited_models.__func__
  33. return super(IrModel, self)._register_hook(cr)
  34. class CleanupPurgeLineModel(models.TransientModel):
  35. _inherit = 'cleanup.purge.line'
  36. _name = 'cleanup.purge.line.model'
  37. _description = 'Purge models'
  38. wizard_id = fields.Many2one(
  39. 'cleanup.purge.wizard.model', 'Purge Wizard', readonly=True)
  40. @api.multi
  41. def purge(self):
  42. """
  43. Unlink models upon manual confirmation.
  44. """
  45. context_flags = {
  46. MODULE_UNINSTALL_FLAG: True,
  47. 'no_drop_table': True,
  48. 'purge': True,
  49. }
  50. for line in self:
  51. self.env.cr.execute(
  52. "SELECT id, model from ir_model WHERE model = %s",
  53. (line.name,))
  54. row = self.env.cr.fetchone()
  55. if not row:
  56. continue
  57. self.logger.info('Purging model %s', row[1])
  58. attachments = self.env['ir.attachment'].search([
  59. ('res_model', '=', line.name)
  60. ])
  61. if attachments:
  62. self.env.cr.execute(
  63. "UPDATE ir_attachment SET res_model = NULL "
  64. "WHERE id in %s",
  65. (tuple(attachments.ids), ))
  66. self.env['ir.model.constraint'].search([
  67. ('model', '=', line.name),
  68. ]).unlink()
  69. relations = self.env['ir.model.fields'].search([
  70. ('relation', '=', row[1]),
  71. ]).with_context(**context_flags)
  72. for relation in relations:
  73. try:
  74. # Fails if the model on the target side
  75. # cannot be instantiated
  76. relation.unlink()
  77. except KeyError:
  78. pass
  79. except AttributeError:
  80. pass
  81. self.env['ir.model.relation'].search([
  82. ('model', '=', line.name)
  83. ]).with_context(**context_flags).unlink()
  84. self.env['ir.model'].browse([row[0]])\
  85. .with_context(**context_flags).unlink()
  86. line.write({'purged': True})
  87. return True
  88. class CleanupPurgeWizardModel(models.TransientModel):
  89. _inherit = 'cleanup.purge.wizard'
  90. _name = 'cleanup.purge.wizard.model'
  91. _description = 'Purge models'
  92. @api.model
  93. def find(self):
  94. """
  95. Search for models that cannot be instantiated.
  96. """
  97. res = []
  98. self.env.cr.execute("SELECT model from ir_model")
  99. for model, in self.env.cr.fetchall():
  100. if model not in self.env:
  101. res.append((0, 0, {'name': model}))
  102. if not res:
  103. raise UserError(_('No orphaned models found'))
  104. return res
  105. purge_line_ids = fields.One2many(
  106. 'cleanup.purge.line.model', 'wizard_id', 'Models to purge')