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.

72 lines
2.7 KiB

  1. # Copyright 2014-2016 Therp BV <http://therp.nl>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import UserError
  5. from ..identifier_adapter import IdentifierAdapter
  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. if self:
  16. objs = self
  17. else:
  18. objs = self.env['cleanup.purge.line.data']\
  19. .browse(self._context.get('active_ids'))
  20. to_unlink = objs.filtered(lambda x: not x.purged and x.data_id)
  21. self.logger.info('Purging data entries: %s', to_unlink.mapped('name'))
  22. to_unlink.mapped('data_id').unlink()
  23. return to_unlink.write({'purged': True})
  24. class CleanupPurgeWizardData(models.TransientModel):
  25. _inherit = 'cleanup.purge.wizard'
  26. _name = 'cleanup.purge.wizard.data'
  27. _description = 'Purge data'
  28. @api.model
  29. def find(self):
  30. """Collect all rows from ir_model_data that refer
  31. to a nonexisting model, or to a nonexisting
  32. row in the model's table."""
  33. res = []
  34. data_ids = []
  35. unknown_models = []
  36. self.env.cr.execute("""SELECT DISTINCT(model) FROM ir_model_data""")
  37. for model, in self.env.cr.fetchall():
  38. if not model:
  39. continue
  40. if model not in self.env:
  41. unknown_models.append(model)
  42. continue
  43. self.env.cr.execute(
  44. """
  45. SELECT id FROM ir_model_data
  46. WHERE model = %s
  47. AND res_id IS NOT NULL
  48. AND NOT EXISTS (
  49. SELECT id FROM %s WHERE id=ir_model_data.res_id)
  50. """, (model, IdentifierAdapter(self.env[model]._table)))
  51. data_ids.extend(data_row for data_row, in self.env.cr.fetchall())
  52. data_ids += self.env['ir.model.data'].search([
  53. ('model', 'in', unknown_models),
  54. ]).ids
  55. for data in self.env['ir.model.data'].browse(data_ids):
  56. res.append((0, 0, {
  57. 'data_id': data.id,
  58. 'name': "%s.%s, object of type %s" % (
  59. data.module, data.name, data.model)}))
  60. if not res:
  61. raise UserError(_('No orphaned data entries found'))
  62. return res
  63. purge_line_ids = fields.One2many(
  64. 'cleanup.purge.line.data', 'wizard_id', 'Data to purge')