~unifield-team/unifield-wm/us-671-homere

« back to all changes in this revision

Viewing changes to msf_profile/deleted_object.py

  • Committer: chloups208
  • Date: 2012-11-21 11:15:15 UTC
  • mto: This revision was merged to the branch mainline in revision 1340.
  • Revision ID: chloups208@chloups208-laptop-20121121111515-myqv282h6xmgh053
utp-171 modification of fields po, po line, product, so, so line

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2015 MSF, TeMPO consulting
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
 
 
22
 
import time
23
 
import types
24
 
import functools
25
 
from osv import osv
26
 
from osv import fields
27
 
from osv import orm
28
 
 
29
 
class deleted_object(osv.osv):
30
 
    """Keep track of deleted objects"""
31
 
    _name = 'deleted.object'
32
 
    _auto = True
33
 
    _log_access = False
34
 
    _order = 'deletion_date desc, model, deleted_obj_id'
35
 
 
36
 
    _columns={
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,
40
 
            read_only=True),
41
 
        'deletion_date': fields.datetime('Deletion Date', read_only=True),
42
 
        'user_id': fields.many2one('res.users', 'User who delete',
43
 
            read_only=True),
44
 
    }
45
 
 
46
 
deleted_object()
47
 
 
48
 
 
49
 
class extended_delete_orm(osv.osv):
50
 
    """Extend orm methods"""
51
 
    _auto = False
52
 
    _name = 'deleted_object.orm_extended'
53
 
    _description = "Flag that certify presence of extended ORM methods"
54
 
 
55
 
extended_delete_orm()
56
 
 
57
 
def orm_delete_method_overload(method):
58
 
    """
59
 
    Wrapper method to override orm.orm classic methods
60
 
    """
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)
66
 
        else:
67
 
            return original_method(self, *args, **kwargs)
68
 
    return wrapper
69
 
 
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)
76
 
        if not res:
77
 
            return res
78
 
        if not ids:
79
 
            return True
80
 
        if isinstance(ids, (int, long)):
81
 
            ids = [ids]
82
 
 
83
 
        # we don't need to keep a track of all deleted objects
84
 
        model_deleted_black_list = ['funding.pool.distribution.line',
85
 
                                    ]
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
90
 
            return res
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
95
 
            return res
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,
105
 
                        'user_id': uid,}
106
 
                deleted_obj_module = self.pool.get('deleted.object')
107
 
                del_obj = deleted_obj_module.create(cr, uid, vals)
108
 
        return res
109
 
 
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)