2008-07-23 15:01:27 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2009-01-04 22:13:29 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
|
2008-11-03 18:27:16 +00:00
|
|
|
# $Id$
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields,osv
|
2008-10-16 18:28:16 +00:00
|
|
|
from osv.orm import except_orm
|
|
|
|
import tools
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
class ir_attachment(osv.osv):
|
2009-08-10 16:24:09 +00:00
|
|
|
def check(self, cr, uid, ids, mode, context=None):
|
2008-10-16 18:28:16 +00:00
|
|
|
if not ids:
|
|
|
|
return
|
|
|
|
ima = self.pool.get('ir.model.access')
|
|
|
|
if isinstance(ids, (int, long)):
|
|
|
|
ids = [ids]
|
2008-12-14 16:46:47 +00:00
|
|
|
cr.execute('select distinct res_model from ir_attachment where id in ('+','.join(map(str, ids))+')')
|
|
|
|
for obj in cr.fetchall():
|
2009-03-23 13:50:57 +00:00
|
|
|
if obj[0]:
|
2009-08-10 16:24:09 +00:00
|
|
|
ima.check(cr, uid, obj[0], mode, context=context)
|
2008-12-13 06:01:18 +00:00
|
|
|
|
2008-10-20 12:00:08 +00:00
|
|
|
def search(self, cr, uid, args, offset=0, limit=None, order=None,
|
|
|
|
context=None, count=False):
|
|
|
|
ids = super(ir_attachment, self).search(cr, uid, args, offset=offset,
|
|
|
|
limit=limit, order=order,
|
|
|
|
context=context, count=False)
|
2008-10-16 18:28:16 +00:00
|
|
|
if not ids:
|
2008-10-20 12:00:08 +00:00
|
|
|
if count:
|
|
|
|
return 0
|
2008-10-16 18:28:16 +00:00
|
|
|
return []
|
|
|
|
models = super(ir_attachment,self).read(cr, uid, ids, ['id', 'res_model'])
|
|
|
|
cache = {}
|
|
|
|
ima = self.pool.get('ir.model.access')
|
|
|
|
for m in models:
|
2009-03-15 18:13:01 +00:00
|
|
|
if m['res_model']:
|
|
|
|
if m['res_model'] not in cache:
|
|
|
|
cache[m['res_model']] = ima.check(cr, uid, m['res_model'], 'read',
|
2009-08-10 16:24:09 +00:00
|
|
|
raise_exception=False, context=context)
|
2009-03-15 18:13:01 +00:00
|
|
|
if not cache[m['res_model']]:
|
|
|
|
ids.remove(m['id'])
|
2008-10-20 12:00:08 +00:00
|
|
|
|
|
|
|
if count:
|
|
|
|
return len(ids)
|
2008-10-16 18:28:16 +00:00
|
|
|
return ids
|
|
|
|
|
2009-08-10 16:24:09 +00:00
|
|
|
def read(self, cr, uid, ids, fields_to_read=None, context=None, load='_classic_read'):
|
|
|
|
self.check(cr, uid, ids, 'read', context=context)
|
|
|
|
return super(ir_attachment, self).read(cr, uid, ids, fields_to_read, context, load)
|
2008-10-16 18:28:16 +00:00
|
|
|
|
2009-08-10 16:24:09 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
|
|
|
self.check(cr, uid, ids, 'write', context=context)
|
|
|
|
return super(ir_attachment, self).write(cr, uid, ids, vals, context)
|
2008-10-16 18:28:16 +00:00
|
|
|
|
2009-08-10 16:24:09 +00:00
|
|
|
def copy(self, cr, uid, id, default=None, context=None):
|
|
|
|
self.check(cr, uid, [id], 'write', context=context)
|
|
|
|
return super(ir_attachment, self).copy(cr, uid, id, default, context)
|
2008-10-16 18:28:16 +00:00
|
|
|
|
2009-08-10 16:24:09 +00:00
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
|
|
|
self.check(cr, uid, ids, 'unlink', context=context)
|
|
|
|
return super(ir_attachment, self).unlink(cr, uid, ids, context)
|
2008-10-16 18:28:16 +00:00
|
|
|
|
2009-08-10 16:24:09 +00:00
|
|
|
def create(self, cr, uid, values, context=None):
|
2008-10-16 18:28:16 +00:00
|
|
|
if 'res_model' in values and values['res_model'] != '':
|
2009-08-10 16:24:09 +00:00
|
|
|
self.pool.get('ir.model.access').check(cr, uid, values['res_model'], 'create', context=context)
|
|
|
|
return super(ir_attachment, self).create(cr, uid, values, context)
|
2008-10-16 18:28:16 +00:00
|
|
|
|
2008-11-12 14:22:25 +00:00
|
|
|
def action_get(self, cr, uid, context=None):
|
|
|
|
dataobj = self.pool.get('ir.model.data')
|
|
|
|
data_id = dataobj._get_id(cr, 1, 'base', 'action_attachment')
|
|
|
|
res_id = dataobj.browse(cr, uid, data_id, context).res_id
|
|
|
|
return self.pool.get('ir.actions.act_window').read(cr, uid, res_id, [], context)
|
2008-10-16 18:28:16 +00:00
|
|
|
|
2008-11-10 17:58:28 +00:00
|
|
|
def _get_preview(self, cr, uid, ids, name, arg, context=None):
|
|
|
|
result = {}
|
2008-11-12 14:22:25 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
context['bin_size'] = False
|
|
|
|
for i in self.browse(cr, uid, ids, context=context):
|
2008-11-10 17:58:28 +00:00
|
|
|
result[i.id] = False
|
2009-06-12 11:41:19 +00:00
|
|
|
for format in ('png','jpg','jpeg','gif','bmp'):
|
2009-06-17 08:55:52 +00:00
|
|
|
if (i.datas_fname and i.datas_fname.lower() or '').endswith(format):
|
2008-11-10 17:58:28 +00:00
|
|
|
result[i.id]= i.datas
|
2009-06-12 11:41:19 +00:00
|
|
|
break
|
2008-11-10 17:58:28 +00:00
|
|
|
return result
|
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
_name = 'ir.attachment'
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Attachment Name',size=64, required=True),
|
|
|
|
'datas': fields.binary('Data'),
|
2008-11-10 17:58:28 +00:00
|
|
|
'preview': fields.function(_get_preview, type='binary', string='Image Preview', method=True),
|
|
|
|
'datas_fname': fields.char('Filename',size=64),
|
2008-07-22 14:24:36 +00:00
|
|
|
'description': fields.text('Description'),
|
|
|
|
# Not required due to the document module !
|
2008-08-21 13:49:55 +00:00
|
|
|
'res_model': fields.char('Resource Object',size=64, readonly=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
'res_id': fields.integer('Resource ID', readonly=True),
|
2008-11-10 17:58:28 +00:00
|
|
|
'link': fields.char('Link', size=256),
|
|
|
|
|
|
|
|
'create_date': fields.datetime('Date Created', readonly=True),
|
|
|
|
'create_uid': fields.many2one('res.users', 'Creator', readonly=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
}
|
2006-12-07 13:41:40 +00:00
|
|
|
ir_attachment()
|
|
|
|
|
2008-07-23 15:01:27 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|