passing in GPL-3
[odoo/odoo.git] / bin / addons / base / ir / ir_attachment.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24 from osv.orm import except_orm
25 import tools
26
27 class ir_attachment(osv.osv):
28
29     def check(self, cr, uid, ids, mode):
30         if not ids: 
31             return
32         ima = self.pool.get('ir.model.access')
33         if isinstance(ids, (int, long)):
34             ids = [ids]
35         objs = self.browse(cr, uid, ids) or []
36         for o in objs:
37             if o and o.res_model:
38                 ima.check(cr, uid, o.res_model, mode)
39     
40     check = tools.cache()(check)
41         
42     def search(self, cr, uid, args, offset=0, limit=None, order=None,
43             context=None, count=False):
44         ids = super(ir_attachment, self).search(cr, uid, args, offset=offset, 
45                                                 limit=limit, order=order, 
46                                                 context=context, count=False)
47         if not ids:
48             if count:
49                 return 0
50             return []
51         models = super(ir_attachment,self).read(cr, uid, ids, ['id', 'res_model'])
52         cache = {}
53         ima = self.pool.get('ir.model.access')
54         for m in models:
55             if m['res_model'] in cache:
56                 if not cache[m['res_model']]:
57                     ids.remove(m['id'])
58                 continue
59             cache[m['res_model']] = ima.check(cr, uid, m['res_model'], 'read',
60                                               raise_exception=False)
61
62         if count:
63             return len(ids)
64         return ids
65
66     def read(self, cr, uid, ids, *args, **kwargs):
67         self.check(cr, uid, ids, 'read')
68         return super(ir_attachment, self).read(cr, uid, ids, *args, **kwargs)
69
70     def write(self, cr, uid, ids, *args, **kwargs):
71         self.check(cr, uid, ids, 'write')
72         return super(ir_attachment, self).write(cr, uid, ids, *args, **kwargs)
73     
74     def copy(self, cr, uid, id, *args, **kwargs):
75         self.check(cr, uid, [id], 'write')
76         return super(ir_attachment, self).copy(cr, uid, id, *args, **kwargs)
77
78     def unlink(self, cr, uid, ids, *args, **kwargs):
79         self.check(cr, uid, ids, 'unlink')
80         return super(ir_attachment, self).unlink(cr, uid, ids, *args, **kwargs)
81
82     def create(self, cr, uid, values, *args, **kwargs):
83         if 'res_model' in values and values['res_model'] != '':
84             self.pool.get('ir.model.access').check(cr, uid, values['res_model'], 'create')
85         return super(ir_attachment, self).create(cr, uid, values, *args, **kwargs)
86
87     def clear_cache(self):
88         self.check()    
89
90     def __init__(self, *args, **kwargs):
91         r = super(ir_attachment, self).__init__(*args, **kwargs)
92         self.pool.get('ir.model.access').register_cache_clearing_method(self._name, 'clear_cache')
93         return r
94
95     def __del__(self):
96         self.pool.get('ir.model.access').unregister_cache_clearing_method(self._name, 'clear_cache')
97         return super(ir_attachment, self).__del__()
98
99     _name = 'ir.attachment'
100     _columns = {
101         'name': fields.char('Attachment Name',size=64, required=True),
102         'datas': fields.binary('Data'),
103         'datas_fname': fields.char('Data Filename',size=64),
104         'description': fields.text('Description'),
105         # Not required due to the document module !
106         'res_model': fields.char('Resource Object',size=64, readonly=True),
107         'res_id': fields.integer('Resource ID', readonly=True),
108         'link': fields.char('Link', size=256)
109     }
110 ir_attachment()
111
112
113 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
114