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.

107 lines
4.0 KiB

11 years ago
11 years ago
11 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm, fields
  22. from openerp.tools.translate import _
  23. from ..identifier_adapter import IdentifierAdapter
  24. class CleanupPurgeLineData(orm.TransientModel):
  25. _inherit = 'cleanup.purge.line'
  26. _name = 'cleanup.purge.line.data'
  27. _columns = {
  28. 'data_id': fields.many2one(
  29. 'ir.model.data', 'Data entry',
  30. ondelete='SET NULL'),
  31. 'wizard_id': fields.many2one(
  32. 'cleanup.purge.wizard.data', 'Purge Wizard', readonly=True),
  33. }
  34. def purge(self, cr, uid, ids, context=None):
  35. """
  36. Unlink data entries upon manual confirmation.
  37. """
  38. data_ids = []
  39. for line in self.browse(cr, uid, ids, context=context):
  40. if line.purged or not line.data_id:
  41. continue
  42. data_ids.append(line.data_id.id)
  43. self.logger.info('Purging data entry: %s', line.name)
  44. self.pool['ir.model.data'].unlink(cr, uid, data_ids, context=context)
  45. return self.write(cr, uid, ids, {'purged': True}, context=context)
  46. class CleanupPurgeWizardData(orm.TransientModel):
  47. _inherit = 'cleanup.purge.wizard'
  48. _name = 'cleanup.purge.wizard.data'
  49. def default_get(self, cr, uid, fields, context=None):
  50. res = super(CleanupPurgeWizardData, self).default_get(
  51. cr, uid, fields, context=context)
  52. if 'name' in fields:
  53. res['name'] = _('Purge data')
  54. return res
  55. def find(self, cr, uid, context=None):
  56. """
  57. Collect all rows from ir_model_data that refer
  58. to a nonexisting model, or to a nonexisting
  59. row in the model's table.
  60. """
  61. res = []
  62. data_pool = self.pool['ir.model.data']
  63. data_ids = []
  64. unknown_models = []
  65. cr.execute("""SELECT DISTINCT(model) FROM ir_model_data""")
  66. for (model,) in cr.fetchall():
  67. if not model:
  68. continue
  69. if not self.pool.get(model):
  70. unknown_models.append(model)
  71. continue
  72. cr.execute(
  73. """
  74. SELECT id FROM ir_model_data
  75. WHERE model = %s
  76. AND res_id IS NOT NULL
  77. AND NOT EXISTS (
  78. SELECT id FROM %s WHERE id=ir_model_data.res_id)
  79. """, (model, IdentifierAdapter(self.pool[model]._table)))
  80. data_ids += [data_row[0] for data_row in cr.fetchall()]
  81. data_ids += data_pool.search(
  82. cr, uid, [('model', 'in', unknown_models)], context=context)
  83. for data in data_pool.browse(cr, uid, data_ids, context=context):
  84. res.append((0, 0, {
  85. 'data_id': data.id,
  86. 'name': "%s.%s, object of type %s" % (
  87. data.module, data.name, data.model)}))
  88. if not res:
  89. raise orm.except_orm(
  90. _('Nothing to do'),
  91. _('No orphaned data entries found'))
  92. return res
  93. _columns = {
  94. 'purge_line_ids': fields.one2many(
  95. 'cleanup.purge.line.data',
  96. 'wizard_id', 'Data to purge'),
  97. }