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