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.

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