ir.attachment: handle missing name from res_model
[odoo/odoo.git] / bin / addons / base / ir / ir_attachment.py
index bae2ad0..121b6a3 100644 (file)
@@ -1,21 +1,20 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
 #
-#    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    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.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    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.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
+#    You should have received a copy of the GNU Affero General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
@@ -26,20 +25,20 @@ import tools
 
 class ir_attachment(osv.osv):
     def check(self, cr, uid, ids, mode, context=None):
-        if not ids: 
+        if not ids:
             return
         ima = self.pool.get('ir.model.access')
         if isinstance(ids, (int, long)):
             ids = [ids]
-        cr.execute('select distinct res_model from ir_attachment where id = ANY (%s)', (ids,))
+        cr.execute('select distinct res_model from ir_attachment where id IN %s', (tuple(ids),))
         for obj in cr.fetchall():
             if obj[0]:
                 ima.check(cr, uid, obj[0], mode, context=context)
 
     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, 
+        ids = super(ir_attachment, self).search(cr, uid, args, offset=offset,
+                                                limit=limit, order=order,
                                                 context=context, count=False)
         if not ids:
             if count:
@@ -67,7 +66,7 @@ class ir_attachment(osv.osv):
     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)
-    
+
     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)
@@ -87,35 +86,44 @@ class ir_attachment(osv.osv):
         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)
 
-    def _get_preview(self, cr, uid, ids, name, arg, context=None):
-        result = {}
-        if context is None:
-            context = {}
-        ctx = context.copy()    
-        ctx['bin_size'] = False
-        for i in self.browse(cr, uid, ids, context=ctx):
-            result[i.id] = False
-            for format in ('png','jpg','jpeg','gif','bmp'):
-                if (i.datas_fname and i.datas_fname.lower() or '').endswith(format):
-                    result[i.id]= i.datas
-                    break
-        return result
+    def _name_get_resname(self, cr, uid, ids, object,method, context):
+        data = {}
+        for attachment in self.browse(cr, uid, ids, context=context):
+            model_object = attachment.res_model
+            res_id = attachment.res_id
+            if model_object and res_id:
+                model_pool = self.pool.get(model_object)
+                res = model_pool.name_get(cr,uid,[res_id],context)
+                data[attachment.id] = (res and res[0][1]) or False
+            else:
+                 data[attachment.id] = False
+        return data
 
     _name = 'ir.attachment'
     _columns = {
-        'name': fields.char('Attachment Name',size=64, required=True),
+        'name': fields.char('Attachment Name',size=256, required=True),
         'datas': fields.binary('Data'),
-        'preview': fields.function(_get_preview, type='binary', string='Image Preview', method=True),
-        'datas_fname': fields.char('Filename',size=64),
+        'datas_fname': fields.char('Filename',size=256),
         'description': fields.text('Description'),
-        # Not required due to the document module !
-        'res_model': fields.char('Resource Object',size=64, readonly=True),
-        'res_id': fields.integer('Resource ID', readonly=True),
-        'link': fields.char('Link', size=256),
+        'res_name': fields.function(_name_get_resname, type='char', 
+                string='Resource Name', method=True),
+        'res_model': fields.char('Resource Object',size=64, readonly=True,
+                help="The database object this attachment will be attached to"),
+        'res_id': fields.integer('Resource ID', readonly=True,
+                help="The record id this is attached to"),
+        'url': fields.char('Url', size=512, oldname="link"),
+        'type': fields.selection(
+                [ ('url','URL'), ('binary','Binary'), ],
+                'Type', help="Binary File or external URL", required=True),
 
         'create_date': fields.datetime('Date Created', readonly=True),
         'create_uid':  fields.many2one('res.users', 'Creator', readonly=True),
     }
+    
+    _defaults = {
+        'type': 'binary',
+    }
+
 ir_attachment()