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.

119 lines
4.1 KiB

  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. }
  49. for line in self:
  50. self.env.cr.execute(
  51. "SELECT id, model from ir_model WHERE model = %s",
  52. (line.name,))
  53. row = self.env.cr.fetchone()
  54. if not row:
  55. continue
  56. self.logger.info('Purging model %s', row[1])
  57. attachments = self.env['ir.attachment'].search([
  58. ('res_model', '=', line.name)
  59. ])
  60. if attachments:
  61. self.env.cr.execute(
  62. "UPDATE ir_attachment SET res_model = NULL "
  63. "WHERE id in %s",
  64. (tuple(attachments.ids), ))
  65. self.env['ir.model.constraint'].search([
  66. ('model', '=', line.name),
  67. ]).unlink()
  68. relations = self.env['ir.model.fields'].search([
  69. ('relation', '=', row[1]),
  70. ]).with_context(**context_flags)
  71. for relation in relations:
  72. try:
  73. # Fails if the model on the target side
  74. # cannot be instantiated
  75. relation.unlink()
  76. except KeyError:
  77. pass
  78. except AttributeError:
  79. pass
  80. self.env['ir.model.relation'].search([
  81. ('model', '=', line.name)
  82. ]).with_context(**context_flags).unlink()
  83. self.env['ir.model'].browse([row[0]])\
  84. .with_context(**context_flags).unlink()
  85. line.write({'purged': True})
  86. return True
  87. class CleanupPurgeWizardModel(models.TransientModel):
  88. _inherit = 'cleanup.purge.wizard'
  89. _name = 'cleanup.purge.wizard.model'
  90. _description = 'Purge models'
  91. @api.model
  92. def find(self):
  93. """
  94. Search for models that cannot be instantiated.
  95. """
  96. res = []
  97. self.env.cr.execute("SELECT model from ir_model")
  98. for model, in self.env.cr.fetchall():
  99. if model not in self.env:
  100. res.append((0, 0, {'name': model}))
  101. if not res:
  102. raise UserError(_('No orphaned models found'))
  103. return res
  104. purge_line_ids = fields.One2many(
  105. 'cleanup.purge.line.model', 'wizard_id', 'Models to purge')