1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields,osv
23 from osv.orm import except_orm
26 class ir_attachment(osv.osv):
27 def check(self, cr, uid, ids, mode, context=None):
30 ima = self.pool.get('ir.model.access')
31 if isinstance(ids, (int, long)):
33 cr.execute('select distinct res_model from ir_attachment where id IN %s', (tuple(ids),))
34 for obj in cr.fetchall():
36 ima.check(cr, uid, obj[0], mode, context=context)
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)
47 models = super(ir_attachment,self).read(cr, uid, ids, ['id', 'res_model'])
49 ima = self.pool.get('ir.model.access')
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']]:
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)
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)
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)
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)
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)
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)
89 def _name_get_resname(self, cr, uid, ids, object,method, context):
91 for attachment in self.browse(cr, uid, ids, context=context):
92 model_object = attachment.res_model
93 res_id = attachment.res_id
94 if model_object and res_id:
95 model_pool = self.pool.get(model_object)
96 res = model_pool.name_get(cr,uid,[res_id],context)
97 data[attachment.id] = (res and res[0][1]) or False
99 data[attachment.id] = False
102 _name = 'ir.attachment'
104 'name': fields.char('Attachment Name',size=256, required=True),
105 'datas': fields.binary('Data'),
106 'datas_fname': fields.char('Filename',size=256),
107 'description': fields.text('Description'),
108 'res_name': fields.function(_name_get_resname, type='char', size=128,
109 string='Resource Name', method=True, store=True),
110 'res_model': fields.char('Resource Object',size=64, readonly=True,
111 help="The database object this attachment will be attached to"),
112 'res_id': fields.integer('Resource ID', readonly=True,
113 help="The record id this is attached to"),
114 'url': fields.char('Url', size=512, oldname="link"),
115 'type': fields.selection(
116 [ ('url','URL'), ('binary','Binary'), ],
117 'Type', help="Binary File or external URL", required=True),
119 'create_date': fields.datetime('Date Created', readonly=True),
120 'create_uid': fields.many2one('res.users', 'Owner', readonly=True),
121 'company_id': fields.many2one('res.company', 'Company'),
126 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'ir.attachment', context=c),
129 def _auto_init(self, cr, context=None):
130 super(ir_attachment, self)._auto_init(cr, context)
131 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_attachment_res_idx',))
132 if not cr.fetchone():
133 cr.execute('CREATE INDEX ir_attachment_res_idx ON ir_attachment (res_model, res_id)')
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: