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.

68 lines
2.5 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, fields, models
  5. from openerp.exceptions import UserError
  6. from ..identifier_adapter import IdentifierAdapter
  7. class CleanupPurgeLineData(models.TransientModel):
  8. _inherit = 'cleanup.purge.line'
  9. _name = 'cleanup.purge.line.data'
  10. data_id = fields.Many2one('ir.model.data', 'Data entry')
  11. wizard_id = fields.Many2one(
  12. 'cleanup.purge.wizard.data', 'Purge Wizard', readonly=True)
  13. @api.multi
  14. def purge(self):
  15. """Unlink data entries upon manual confirmation."""
  16. to_unlink = self.filtered(lambda x: not x.purged and x.data_id)
  17. self.logger.info('Purging data entries: %s', to_unlink.mapped('name'))
  18. to_unlink.mapped('data_id').unlink()
  19. return self.write({'purged': True})
  20. class CleanupPurgeWizardData(models.TransientModel):
  21. _inherit = 'cleanup.purge.wizard'
  22. _name = 'cleanup.purge.wizard.data'
  23. _description = 'Purge data'
  24. @api.model
  25. def find(self):
  26. """Collect all rows from ir_model_data that refer
  27. to a nonexisting model, or to a nonexisting
  28. row in the model's table."""
  29. res = []
  30. data_ids = []
  31. unknown_models = []
  32. self.env.cr.execute("""SELECT DISTINCT(model) FROM ir_model_data""")
  33. for model, in self.env.cr.fetchall():
  34. if not model:
  35. continue
  36. if model not in self.env:
  37. unknown_models.append(model)
  38. continue
  39. self.env.cr.execute(
  40. """
  41. SELECT id FROM ir_model_data
  42. WHERE model = %s
  43. AND res_id IS NOT NULL
  44. AND NOT EXISTS (
  45. SELECT id FROM %s WHERE id=ir_model_data.res_id)
  46. """, (model, IdentifierAdapter(self.env[model]._table)))
  47. data_ids.extend(data_row for data_row, in self.env.cr.fetchall())
  48. data_ids += self.env['ir.model.data'].search([
  49. ('model', 'in', unknown_models),
  50. ]).ids
  51. for data in self.env['ir.model.data'].browse(data_ids):
  52. res.append((0, 0, {
  53. 'data_id': data.id,
  54. 'name': "%s.%s, object of type %s" % (
  55. data.module, data.name, data.model)}))
  56. if not res:
  57. raise UserError(_('No orphaned data entries found'))
  58. return res
  59. purge_line_ids = fields.One2many(
  60. 'cleanup.purge.line.data', 'wizard_id', 'Data to purge')