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.

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