1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2015 MSF, TeMPO consulting
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.
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.
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/>.
20
##############################################################################
26
from osv import fields
29
class deleted_object(osv.osv):
30
"""Keep track of deleted objects"""
31
_name = 'deleted.object'
34
_order = 'deletion_date desc, model, deleted_obj_id'
37
'model': fields.char('Object Model', size=64, readonly=True),
38
'deleted_obj_id': fields.integer('Deleted Object ID', readonly=True),
39
'deleted_obj_sd_ref': fields.char('Deleted Object SD Ref', size=128,
41
'deletion_date': fields.datetime('Deletion Date', read_only=True),
42
'user_id': fields.many2one('res.users', 'User who delete',
49
class extended_delete_orm(osv.osv):
50
"""Extend orm methods"""
52
_name = 'deleted_object.orm_extended'
53
_description = "Flag that certify presence of extended ORM methods"
57
def orm_delete_method_overload(method):
59
Wrapper method to override orm.orm classic methods
61
original_method = getattr(orm.orm, method.func_name)
62
@functools.wraps(method)
63
def wrapper(self, *args, **kwargs):
64
if self.pool.get(extended_delete_orm._name) is not None:
65
return method(self, original_method, *args, **kwargs)
67
return original_method(self, *args, **kwargs)
70
class extended_orm_delete_method:
71
@orm_delete_method_overload
72
def unlink(self, original_unlink, cr, uid, ids, context=None):
73
"""Create a delete_object in case the current object is not blacklisted
74
to keep track of deleted objects"""
75
res = original_unlink(self, cr, uid, ids, context=context)
80
if isinstance(ids, (int, long)):
83
# we don't need to keep a track of all deleted objects
84
model_deleted_black_list = ['funding.pool.distribution.line',
86
if self._name in model_deleted_black_list or\
87
self._name.startswith('ir.') or\
88
isinstance(self, orm.orm_memory): # don't track object from
89
# orm.orm_memory class
91
deletion_date = time.strftime('%Y-%m-%d %H:%M:%S')
92
model_obj = self.pool.get(self._name)
93
if not self.pool.get('sync.client.entity'):
94
# sync client not installed
96
obj_sd_ref = model_obj.get_sd_ref(cr, uid, ids)
97
for sub_ids in cr.split_for_in_conditions(ids):
98
# keep a track of deleted object if there are not blacklisted
99
# by creating a delete_object
100
for obj_id in sub_ids:
101
vals = {'model': self._name,
102
'deleted_obj_id': obj_id,
103
'deleted_obj_sd_ref': obj_sd_ref[obj_id],
104
'deletion_date': deletion_date,
106
deleted_obj_module = self.pool.get('deleted.object')
107
del_obj = deleted_obj_module.create(cr, uid, vals)
110
for symbol in filter(lambda sym: isinstance(sym, types.MethodType),
111
map(lambda label: getattr(extended_orm_delete_method, label),
112
dir(extended_orm_delete_method))):
113
setattr(orm.orm, symbol.func_name, symbol.im_func)