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.

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