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.

105 lines
3.9 KiB

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. class CleanupPurgeLineData(orm.TransientModel):
  24. _inherit = 'cleanup.purge.line'
  25. _name = 'cleanup.purge.line.data'
  26. _columns = {
  27. 'data_id': fields.many2one(
  28. 'ir.model.data', 'Data entry',
  29. ondelete='SET NULL'),
  30. 'wizard_id': fields.many2one(
  31. 'cleanup.purge.wizard.data', 'Purge Wizard', readonly=True),
  32. }
  33. def purge(self, cr, uid, ids, context=None):
  34. """
  35. Unlink data entries upon manual confirmation.
  36. """
  37. data_ids = []
  38. for line in self.browse(cr, uid, ids, context=context):
  39. if line.purged or not line.data_id:
  40. continue
  41. data_ids.append(line.data_id.id)
  42. self.logger.info('Purging data entry: %s', line.name)
  43. self.pool['ir.model.data'].unlink(cr, uid, data_ids, context=context)
  44. return self.write(cr, uid, ids, {'purged': True}, context=context)
  45. class CleanupPurgeWizardData(orm.TransientModel):
  46. _inherit = 'cleanup.purge.wizard'
  47. _name = 'cleanup.purge.wizard.data'
  48. def default_get(self, cr, uid, fields, context=None):
  49. res = super(CleanupPurgeWizardData, self).default_get(
  50. cr, uid, fields, context=context)
  51. if 'name' in fields:
  52. res['name'] = _('Purge data')
  53. return res
  54. def find(self, cr, uid, context=None):
  55. """
  56. Collect all rows from ir_model_data that refer
  57. to a nonexisting model, or to a nonexisting
  58. row in the model's table.
  59. """
  60. res = []
  61. data_pool = self.pool['ir.model.data']
  62. data_ids = []
  63. unknown_models = []
  64. cr.execute("""SELECT DISTINCT(model) FROM ir_model_data""")
  65. for (model,) in cr.fetchall():
  66. if not model:
  67. continue
  68. if not self.pool.get(model):
  69. unknown_models.append(model)
  70. continue
  71. cr.execute(
  72. """
  73. SELECT id FROM ir_model_data
  74. WHERE model = %%s
  75. AND res_id IS NOT NULL
  76. AND res_id NOT IN (
  77. SELECT id FROM %s)
  78. """ % self.pool[model]._table, (model,))
  79. data_ids += [data_row[0] for data_row in cr.fetchall()]
  80. data_ids += data_pool.search(
  81. cr, uid, [('model', 'in', unknown_models)], context=context)
  82. for data in data_pool.browse(cr, uid, data_ids, context=context):
  83. res.append((0, 0, {
  84. 'data_id': data.id,
  85. 'name': "%s.%s, object of type %s" % (
  86. data.module, data.name, data.model)}))
  87. if not res:
  88. raise orm.except_orm(
  89. _('Nothing to do'),
  90. _('No orphaned data entries found'))
  91. return res
  92. _columns = {
  93. 'purge_line_ids': fields.one2many(
  94. 'cleanup.purge.line.data',
  95. 'wizard_id', 'Data to purge'),
  96. }