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.

96 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from openerp.osv import orm, fields
  4. from openerp.tools.translate import _
  5. from openerp.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG
  6. class IrModel(orm.Model):
  7. _inherit = 'ir.model'
  8. def _drop_table(self, cr, uid, ids, context=None):
  9. # Allow to skip this step during model unlink
  10. # The super method crashes if the model cannot be instantiated
  11. if context and context.get('no_drop_table'):
  12. return True
  13. return super(IrModel, self)._drop_table(cr, uid, ids, context=context)
  14. class CleanupPurgeLineModel(orm.TransientModel):
  15. _inherit = 'cleanup.purge.line'
  16. _name = 'cleanup.purge.line.model'
  17. _columns = {
  18. 'wizard_id': fields.many2one(
  19. 'cleanup.purge.wizard.model', 'Purge Wizard', readonly=True),
  20. }
  21. def purge(self, cr, uid, ids, context=None):
  22. """
  23. Unlink models upon manual confirmation.
  24. """
  25. model_pool = self.pool['ir.model']
  26. attachment_pool = self.pool['ir.attachment']
  27. constraint_pool = self.pool['ir.model.constraint']
  28. local_context=(context or {}).copy()
  29. local_context.update({
  30. MODULE_UNINSTALL_FLAG: True,
  31. 'no_drop_table': True,
  32. })
  33. for line in self.browse(cr, uid, ids, context=context):
  34. cr.execute(
  35. "SELECT id, model from ir_model WHERE model = %s",
  36. (line.name,))
  37. row = cr.fetchone()
  38. if row:
  39. self.logger.info('Purging model %s', row[1])
  40. attachment_ids = attachment_pool.search(
  41. cr, uid, [('res_model', '=', line.name)], context=context)
  42. if attachment_ids:
  43. attachment_pool.write(
  44. cr, uid, attachment_ids, {'res_model': False},
  45. context=context)
  46. constraint_ids = constraint_pool.search(
  47. cr, uid, [('model', '=', line.name)], context=context)
  48. if constraint_ids:
  49. constraint_pool.unlink(
  50. cr, uid, constraint_ids, context=context)
  51. model_pool.unlink(cr, uid, [row[0]], context=local_context)
  52. line.write({'purged': True})
  53. cr.commit()
  54. return True
  55. class CleanupPurgeWizardModel(orm.TransientModel):
  56. _inherit = 'cleanup.purge.wizard'
  57. _name = 'cleanup.purge.wizard.model'
  58. def default_get(self, cr, uid, fields, context=None):
  59. res = super(CleanupPurgeWizardModel, self).default_get(
  60. cr, uid, fields, context=context)
  61. if 'name' in fields:
  62. res['name'] = _('Purge models')
  63. return res
  64. def find(self, cr, uid, context=None):
  65. """
  66. Search for models that cannot be instantiated.
  67. """
  68. res = []
  69. cr.execute("SELECT model from ir_model")
  70. for (model,) in cr.fetchall():
  71. if not self.pool.get(model):
  72. res.append((0, 0, {'name': model}))
  73. if not res:
  74. raise orm.except_orm(
  75. _('Nothing to do'),
  76. _('No orphaned models found'))
  77. return res
  78. _columns = {
  79. 'purge_line_ids': fields.one2many(
  80. 'cleanup.purge.line.model',
  81. 'wizard_id', 'Models to purge'),
  82. }