Merge branch 'master' of openobject-server into mdv-gpl3-py26
[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-2009 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     def check(self, cr, uid, ids, mode):
29         if not ids: 
30             return
31         ima = self.pool.get('ir.model.access')
32         if isinstance(ids, (int, long)):
33             ids = [ids]
34         cr.execute('select distinct res_model from ir_attachment where id in ('+','.join(map(str, ids))+')')
35         for obj in cr.fetchall():
36             ima.check(cr, uid, obj[0], mode)
37
38     def search(self, cr, uid, args, offset=0, limit=None, order=None,
39             context=None, count=False):
40         ids = super(ir_attachment, self).search(cr, uid, args, offset=offset, 
41                                                 limit=limit, order=order, 
42                                                 context=context, count=False)
43         if not ids:
44             if count:
45                 return 0
46             return []
47         models = super(ir_attachment,self).read(cr, uid, ids, ['id', 'res_model'])
48         cache = {}
49         ima = self.pool.get('ir.model.access')
50         for m in models:
51             if m['res_model'] not in cache:
52                 cache[m['res_model']] = ima.check(cr, uid, m['res_model'], 'read',
53                                                   raise_exception=False)
54             if not cache[m['res_model']]:
55                 ids.remove(m['id'])
56
57         if count:
58             return len(ids)
59         return ids
60
61     def read(self, cr, uid, ids, *args, **kwargs):
62         self.check(cr, uid, ids, 'read')
63         return super(ir_attachment, self).read(cr, uid, ids, *args, **kwargs)
64
65     def write(self, cr, uid, ids, *args, **kwargs):
66         self.check(cr, uid, ids, 'write')
67         return super(ir_attachment, self).write(cr, uid, ids, *args, **kwargs)
68     
69     def copy(self, cr, uid, id, *args, **kwargs):
70         self.check(cr, uid, [id], 'write')
71         return super(ir_attachment, self).copy(cr, uid, id, *args, **kwargs)
72
73     def unlink(self, cr, uid, ids, *args, **kwargs):
74         self.check(cr, uid, ids, 'unlink')
75         return super(ir_attachment, self).unlink(cr, uid, ids, *args, **kwargs)
76
77     def create(self, cr, uid, values, *args, **kwargs):
78         if 'res_model' in values and values['res_model'] != '':
79             self.pool.get('ir.model.access').check(cr, uid, values['res_model'], 'create')
80         return super(ir_attachment, self).create(cr, uid, values, *args, **kwargs)
81
82     def action_get(self, cr, uid, context=None):
83         dataobj = self.pool.get('ir.model.data')
84         data_id = dataobj._get_id(cr, 1, 'base', 'action_attachment')
85         res_id = dataobj.browse(cr, uid, data_id, context).res_id
86         return self.pool.get('ir.actions.act_window').read(cr, uid, res_id, [], context)
87
88     def _get_preview(self, cr, uid, ids, name, arg, context=None):
89         result = {}
90         if context is None:
91             context = {}
92         context['bin_size'] = False
93         for i in self.browse(cr, uid, ids, context=context):
94             result[i.id] = False
95             for format in ('png','PNG','jpg','JPG'):
96                 if (i.datas_fname or '').endswith(format):
97                     result[i.id]= i.datas
98         return result
99
100     _name = 'ir.attachment'
101     _columns = {
102         'name': fields.char('Attachment Name',size=64, required=True),
103         'datas': fields.binary('Data'),
104         'preview': fields.function(_get_preview, type='binary', string='Image Preview', method=True),
105         'datas_fname': fields.char('Filename',size=64),
106         'description': fields.text('Description'),
107         # Not required due to the document module !
108         'res_model': fields.char('Resource Object',size=64, readonly=True),
109         'res_id': fields.integer('Resource ID', readonly=True),
110         'link': fields.char('Link', size=256),
111
112         'create_date': fields.datetime('Date Created', readonly=True),
113         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
114     }
115 ir_attachment()
116
117
118 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
119