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

  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. 'model_id': fields.many2one(
  28. 'ir.model', 'Model',
  29. required=True, ondelete='CASCADE'),
  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 columns 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. """
  57. res = []
  58. data_pool = self.pool['ir.model.data']
  59. data_ids = []
  60. unknown_models = []
  61. cr.execute("""SELECT DISTINCT(model) FROM ir_model_data""")
  62. for (model,) in cr.fetchall():
  63. if not model:
  64. continue
  65. if not self.pool.get(model):
  66. unknown_models.append(model)
  67. continue
  68. cr.execute(
  69. """
  70. SELECT id FROM ir_model_data
  71. WHERE model = %%s
  72. AND res_id IS NOT NULL
  73. AND res_id NOT IN (
  74. SELECT id FROM %s)
  75. """ % self.pool[model]._table, (model,))
  76. data_ids += [data_row[0] for data_row in cr.fetchall()]
  77. data_ids += data_pool.search(
  78. cr, uid, [('model', 'in', unknown_models)], context=context)
  79. for data in data_pool.browse(cr, uid, data_ids, context=context):
  80. res.append((0, 0, {
  81. 'data_id': data.id,
  82. 'name': "%s.%s, object of type %s" % (
  83. data.module, data.name, data.model)}))
  84. if not res:
  85. raise orm.except_orm(
  86. _('Nothing to do'),
  87. _('No orphaned data entries found'))
  88. return res
  89. _columns = {
  90. 'purge_line_ids': fields.one2many(
  91. 'cleanup.purge.line.data',
  92. 'wizard_id', 'Data to purge'),
  93. 'data_id': fields.many2one(
  94. 'ir.model.data', 'Data entry',
  95. ondelete='SET NULL'),
  96. }