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.

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