Changed encoding to coding ref: PEP: 0263
[odoo/odoo.git] / bin / addons / base / ir / ir_attachment.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 from osv import fields,osv
23 from osv.orm import except_orm
24 import tools
25
26 class ir_attachment(osv.osv):
27     def check(self, cr, uid, ids, mode, context=None):
28         if not ids: 
29             return
30         ima = self.pool.get('ir.model.access')
31         if isinstance(ids, (int, long)):
32             ids = [ids]
33         cr.execute('select distinct res_model from ir_attachment where id in ('+','.join(map(str, ids))+')')
34         for obj in cr.fetchall():
35             if obj[0]:
36                 ima.check(cr, uid, obj[0], mode, context=context)
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']:
52                 if m['res_model'] not in cache:
53                     cache[m['res_model']] = ima.check(cr, uid, m['res_model'], 'read',
54                                                       raise_exception=False, context=context)
55                 if not cache[m['res_model']]:
56                     ids.remove(m['id'])
57
58         if count:
59             return len(ids)
60         return ids
61
62     def read(self, cr, uid, ids, fields_to_read=None, context=None, load='_classic_read'):
63         self.check(cr, uid, ids, 'read', context=context)
64         return super(ir_attachment, self).read(cr, uid, ids, fields_to_read, context, load)
65
66     def write(self, cr, uid, ids, vals, context=None):
67         self.check(cr, uid, ids, 'write', context=context)
68         return super(ir_attachment, self).write(cr, uid, ids, vals, context)
69     
70     def copy(self, cr, uid, id, default=None, context=None):
71         self.check(cr, uid, [id], 'write', context=context)
72         return super(ir_attachment, self).copy(cr, uid, id, default, context)
73
74     def unlink(self, cr, uid, ids, context=None):
75         self.check(cr, uid, ids, 'unlink', context=context)
76         return super(ir_attachment, self).unlink(cr, uid, ids, context)
77
78     def create(self, cr, uid, values, context=None):
79         if 'res_model' in values and values['res_model'] != '':
80             self.pool.get('ir.model.access').check(cr, uid, values['res_model'], 'create', context=context)
81         return super(ir_attachment, self).create(cr, uid, values, context)
82
83     def action_get(self, cr, uid, context=None):
84         dataobj = self.pool.get('ir.model.data')
85         data_id = dataobj._get_id(cr, 1, 'base', 'action_attachment')
86         res_id = dataobj.browse(cr, uid, data_id, context).res_id
87         return self.pool.get('ir.actions.act_window').read(cr, uid, res_id, [], context)
88
89     def _get_preview(self, cr, uid, ids, name, arg, context=None):
90         result = {}
91         if context is None:
92             context = {}
93         context['bin_size'] = False
94         for i in self.browse(cr, uid, ids, context=context):
95             result[i.id] = False
96             for format in ('png','jpg','jpeg','gif','bmp'):
97                 if (i.datas_fname and i.datas_fname.lower() or '').endswith(format):
98                     result[i.id]= i.datas
99                     break
100         return result
101
102     _name = 'ir.attachment'
103     _columns = {
104         'name': fields.char('Attachment Name',size=64, required=True),
105         'datas': fields.binary('Data'),
106         'preview': fields.function(_get_preview, type='binary', string='Image Preview', method=True),
107         'datas_fname': fields.char('Filename',size=64),
108         'description': fields.text('Description'),
109         # Not required due to the document module !
110         'res_model': fields.char('Resource Object',size=64, readonly=True),
111         'res_id': fields.integer('Resource ID', readonly=True),
112         'link': fields.char('Link', size=256),
113
114         'create_date': fields.datetime('Date Created', readonly=True),
115         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
116     }
117 ir_attachment()
118
119
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
121