[IMP] Properly import files
authorFabien Meghazi <fme@openerp.com>
Mon, 24 Mar 2014 15:17:11 +0000 (16:17 +0100)
committerFabien Meghazi <fme@openerp.com>
Mon, 24 Mar 2014 15:17:11 +0000 (16:17 +0100)
bzr revid: fme@openerp.com-20140324151711-89mfg14573dpnh0p

addons/base_import_module/models/ir_module.py

index 318af92..18e60f8 100644 (file)
@@ -43,13 +43,14 @@ class view(osv.osv):
                 convert_file(cr, module, filename, idref, mode=mode, noupdate=noupdate, kind=kind, pathname=pathname)
 
         path_static = opj(path, 'static')
+        ir_attach = self.pool['ir.attachment']
         if os.path.isdir(path_static):
             for root, _, files in os.walk(path_static):
                 for static_file in files:
                     full_path = opj(root, static_file)
                     with open(full_path, 'r') as fp:
                         data = fp.read().encode('base64')
-                    url_path = full_path.split(path)[1].replace(os.path.sep, '/')
+                    url_path = '/%s%s' % (module, full_path.split(path)[1].replace(os.path.sep, '/'))
                     url_path = url_path.decode(sys.getfilesystemencoding())
                     filename = os.path.split(url_path)[1]
                     values = dict(
@@ -60,7 +61,11 @@ class view(osv.osv):
                         type='binary',
                         datas=data,
                     )
-                    self.pool['ir.attachment'].create(cr, uid, values, context=context)
+                    att_id = ir_attach.search(cr, uid, [('url', '=', url_path), ('type', '=', 'binary'), ('res_model', '=', 'ir.ui.view')], context=context)
+                    if att_id:
+                        ir_attach.write(cr, uid, att_id, values, context=context)
+                    else:
+                        ir_attach.create(cr, uid, values, context=context)
 
         return True