1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
23 from osv import fields,osv
24 from osv.orm import except_orm
27 class ir_attachment(osv.osv):
29 def check(self, cr, uid, ids, mode):
32 ima = self.pool.get('ir.model.access')
33 if isinstance(ids, (int, long)):
35 objs = self.browse(cr, uid, ids) or []
38 ima.check(cr, uid, o.res_model, mode)
40 def search(self, cr, uid, args, offset=0, limit=None, order=None,
41 context=None, count=False):
42 ids = super(ir_attachment, self).search(cr, uid, args, offset=offset,
43 limit=limit, order=order,
44 context=context, count=False)
49 models = super(ir_attachment,self).read(cr, uid, ids, ['id', 'res_model'])
51 ima = self.pool.get('ir.model.access')
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)
56 if not cache[m['res_model']]:
63 def read(self, cr, uid, ids, *args, **kwargs):
64 self.check(cr, uid, ids, 'read')
65 return super(ir_attachment, self).read(cr, uid, ids, *args, **kwargs)
67 def write(self, cr, uid, ids, *args, **kwargs):
68 self.check(cr, uid, ids, 'write')
69 return super(ir_attachment, self).write(cr, uid, ids, *args, **kwargs)
71 def copy(self, cr, uid, id, *args, **kwargs):
72 self.check(cr, uid, [id], 'write')
73 return super(ir_attachment, self).copy(cr, uid, id, *args, **kwargs)
75 def unlink(self, cr, uid, ids, *args, **kwargs):
76 self.check(cr, uid, ids, 'unlink')
77 return super(ir_attachment, self).unlink(cr, uid, ids, *args, **kwargs)
79 def create(self, cr, uid, values, *args, **kwargs):
80 if 'res_model' in values and values['res_model'] != '':
81 self.pool.get('ir.model.access').check(cr, uid, values['res_model'], 'create')
82 return super(ir_attachment, self).create(cr, uid, values, *args, **kwargs)
84 def clear_cache(self):
87 def action_get(self, cr, uid, context=None):
88 dataobj = self.pool.get('ir.model.data')
89 data_id = dataobj._get_id(cr, 1, 'base', 'action_attachment')
90 res_id = dataobj.browse(cr, uid, data_id, context).res_id
91 return self.pool.get('ir.actions.act_window').read(cr, uid, res_id, [], context)
93 def __init__(self, *args, **kwargs):
94 r = super(ir_attachment, self).__init__(*args, **kwargs)
95 self.pool.get('ir.model.access').register_cache_clearing_method(self._name, 'clear_cache')
99 self.pool.get('ir.model.access').unregister_cache_clearing_method(self._name, 'clear_cache')
100 return super(ir_attachment, self).__del__()
102 def _get_preview(self, cr, uid, ids, name, arg, context=None):
106 context['bin_size'] = False
107 for i in self.browse(cr, uid, ids, context=context):
109 for format in ('png','PNG','jpg','JPG'):
110 if (i.datas_fname or '').endswith(format):
111 result[i.id]= i.datas
114 _name = 'ir.attachment'
116 'name': fields.char('Attachment Name',size=64, required=True),
117 'datas': fields.binary('Data'),
118 'preview': fields.function(_get_preview, type='binary', string='Image Preview', method=True),
119 'datas_fname': fields.char('Filename',size=64),
120 'description': fields.text('Description'),
121 # Not required due to the document module !
122 'res_model': fields.char('Resource Object',size=64, readonly=True),
123 'res_id': fields.integer('Resource ID', readonly=True),
124 'link': fields.char('Link', size=256),
126 'create_date': fields.datetime('Date Created', readonly=True),
127 'create_uid': fields.many2one('res.users', 'Creator', readonly=True),
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: