diff options
| author | Harry (Open ERP) <hmo@tinyerp.com> | 2010-03-16 18:43:28 +0530 |
|---|---|---|
| committer | Harry (Open ERP) <hmo@tinyerp.com> | 2010-03-16 18:43:28 +0530 |
| commit | 0db347d65f9c67f4b114a96874ea120cf7eacdee (patch) | |
| tree | 14b476a2f5cfca2255c1a289b40a64ce35531dac /addons/document | |
| parent | 4b08bc496fe4945b25580d004b7f8745d00f7b15 (diff) | |
| parent | b509b2d0867f0283e7876c6ee636f7e18f48f11e (diff) | |
[MERGE] merged from lp:~openerp-commiter/openobject-addons/crm-improvement
bzr revid: hmo@tinyerp.com-20100316131328-lgttnpy712rqr7ga
Diffstat (limited to 'addons/document')
| -rw-r--r-- | addons/document/content_index.py | 3 | ||||
| -rw-r--r-- | addons/document/document_directory.py | 18 | ||||
| -rw-r--r-- | addons/document/document_storage.py | 52 | ||||
| -rw-r--r-- | addons/document/nodes.py | 33 | ||||
| -rw-r--r-- | addons/document/std_index.py | 17 |
5 files changed, 115 insertions, 8 deletions
diff --git a/addons/document/content_index.py b/addons/document/content_index.py index 9fc0023e833..9e47621b4c3 100644 --- a/addons/document/content_index.py +++ b/addons/document/content_index.py @@ -158,7 +158,8 @@ class contentIndex(): os.write(fd, content) os.close(fd) - fp = Popen(['file','-b','--mime-type',fname], shell=False, stdout=PIPE).stdout + #fp = Popen(['file','-b','--mime-type',fname], shell=False, stdout=PIPE).stdout + fp = Popen(['file','-b',fname], shell=False, stdout=PIPE).stdout result = fp.read() fp.close() mime2 = result.strip() diff --git a/addons/document/document_directory.py b/addons/document/document_directory.py index 17c3dfb66d9..1557941e7c6 100644 --- a/addons/document/document_directory.py +++ b/addons/document/document_directory.py @@ -65,9 +65,10 @@ class document_directory(osv.osv): objid=self.pool.get('ir.model.data') try: mid = objid._get_id(cr, uid, 'document', 'dir_root') - return False # TODO: check why not display root dir if not mid: return False + root_id = objid.read(cr, uid, mid, ['res_id'])['res_id'] + return root_id except Exception, e: import netsvc logger = netsvc.Logger() @@ -111,6 +112,21 @@ class document_directory(osv.osv): res.append((d.id, s)) return res + def get_full_path(self, cr, uid, dir_id, context=None): + """ Return the full path to this directory, in a list, root first + """ + def _parent(dir_id, path): + parent=self.browse(cr,uid,dir_id) + if parent.parent_id and not parent.ressource_parent_type_id: + _parent(parent.parent_id.id,path) + path.append(parent.name) + else: + path.append(parent.name) + return path + path = [] + _parent(dir_id, path) + return path + def ol_get_resource_path(self,cr,uid,dir_id,res_model,res_id): # this method will be used in process module # to be need test and Improvement if resource dir has parent resource (link resource) diff --git a/addons/document/document_storage.py b/addons/document/document_storage.py index 2b50e7a27c9..0fd85e0be6d 100644 --- a/addons/document/document_storage.py +++ b/addons/document/document_storage.py @@ -154,7 +154,6 @@ class document_storage(osv.osv): netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "ir.attachment #%d does not have a filename, but is at filestore, fix it!" % ira.id) return None fpath = os.path.join(boo.path, ira.store_fname) - print "Trying to read \"%s\".." % fpath return file(fpath, 'rb').read() elif boo.type == 'db': # TODO: we need a better api for large files @@ -164,8 +163,19 @@ class document_storage(osv.osv): out = '' return out elif boo.type == 'realstore': - # fpath = os.path.join(boo.path, - return None + if not ira.store_fname: + # On a migrated db, some files may have the wrong storage type + # try to fix their directory. + if ira.file_size: + netsvc.Logger().notifyChannel('document',netsvc.LOG_WARNING,"ir.attachment #%d does not have a filename, trying the name." %ira.id) + sfname = ira.name + fpath = os.path.join(boo.path,ira.store_fname or ira.name) + if os.path.exists(fpath): + return file(fpath,'rb').read() + elif not ira.store_fname: + return None + else: + raise IOError("File not found: %s" % fpath) else: raise TypeError("No %s storage" % boo.type) @@ -219,6 +229,36 @@ class document_storage(osv.osv): out = base64.encodestring(data) cr.execute('UPDATE ir_attachment SET db_datas = %s WHERE id = %s', (out, file_node.file_id)) + elif boo.type == 'realstore': + try: + file_node.fix_ppath(cr, ira) + npath = file_node.full_path() or [] + # npath may contain empty elements, for root directory etc. + for i, n in enumerate(npath): + if n == None: + del npath[i] + for n in npath: + for ch in ('*', '|', "\\", '/', ':', '"', '<', '>', '?', '..'): + if ch in n: + raise ValueError("Invalid char %s in path %s" %(ch, n)) + dpath = [boo.path,] + dpath += npath[:-1] + path = os.path.join(*dpath) + if not os.path.isdir(path): + os.makedirs(path) + fname = os.path.join(path, npath[-1]) + fp = file(fname,'wb') + fp.write(data) + fp.close() + logger.notifyChannel('document',netsvc.LOG_DEBUG,"Saved data to %s" % fname) + filesize = len(data) # os.stat(fname).st_size + store_fname = os.path.join(*npath) + # TODO Here, an old file would be left hanging. + except Exception,e : + import traceback + traceback.print_exc() + netsvc.Logger().notifyChannel('document',netsvc.LOG_WARNING,"Couldn't save data: %s" % e) + raise except_orm(_('Error!'), str(e)) else: raise TypeError("No %s storage" % boo.type) @@ -262,6 +302,12 @@ class document_storage(osv.osv): return (storage_bo.id, 'file', os.path.join(path, fname)) elif storage_bo.type == 'db': return None + elif storage_bo.type == 'realstore': + fname = fil_bo.store_fname + if not fname: + return None + path = storage_bo.path + return ( storage_bo.id, 'file', os.path.join(path,fname)) else: raise TypeError("No %s storage" % boo.type) diff --git a/addons/document/nodes.py b/addons/document/nodes.py index c6e2f1b10d1..543610a0d22 100644 --- a/addons/document/nodes.py +++ b/addons/document/nodes.py @@ -599,7 +599,33 @@ class node_file(node_class): self.storage_id = fil.parent_id.storage_id.id else: self.storage_id = None - + + def fix_ppath(self, cr, fbro): + """Sometimes we may init this w/o path, parent. + This function fills the missing path from the file browse object + + Note: this may be an expensive operation, do on demand. However, + once caching is in, we might want to do that at init time and keep + this object anyway + """ + if self.path or self.parent: + return + assert fbro + uid = self.context.uid + + dirpath = [] + if fbro.parent_id: + dirobj = self.context._dirobj.pool.get('document.directory') + dirpath = dirobj.get_full_path(cr, uid, fbro.parent_id.id, context=self.context.context) + if fbro.datas_fname: + dirpath.append(fbro.datas_fname) + else: + dirpath.append(fbro.name) + + if len(dirpath)>1: + self.path = dirpath + else: + self.path = dirpath[0] def get_data(self, cr, fil_obj = None): """ Retrieve the data for some file. @@ -631,6 +657,11 @@ class node_file(node_class): the browse object. """ # this is where storage kicks in.. stor = self.storage_id + if not stor: + data_obj = self.context._dirobj.pool.get('ir.model.data') + data_id = data_obj._get_id(cr, self.context.uid, 'document', 'storage_db') + if data_id: + stor = data_obj.browse(cr, self.context.uid, data_id, context=self.context.context).res_id assert stor stobj = self.context._dirobj.pool.get('document.storage') return stobj.set_data(cr,self.context.uid,stor, self, data, self.context.context, fil_obj) diff --git a/addons/document/std_index.py b/addons/document/std_index.py index 1f639a32fa3..26e5d542f01 100644 --- a/addons/document/std_index.py +++ b/addons/document/std_index.py @@ -21,7 +21,7 @@ import StringIO import odt2txt - +from subprocess import Popen, PIPE from content_index import indexer, cntIndex from subprocess import Popen, PIPE @@ -50,6 +50,19 @@ class TxtIndex(indexer): cntIndex.register(TxtIndex()) +class PptIndex(indexer): + def _getMimeTypes(self): + return [ 'application/ms-word'] + + def _getExtensions(self): + return ['.ppt'] + + def _doIndexFile(self,fname): + fp = Popen(['ppthtml', fname], shell=False, stdout=PIPE).stdout + return _to_unicode( fp.read()) + +cntIndex.register(PptIndex()) + class DocIndex(indexer): def _getMimeTypes(self): return [ 'application/ms-word'] @@ -58,7 +71,7 @@ class DocIndex(indexer): return ['.doc'] def _doIndexFile(self,fname): - fp = Popen(['antiword',fname], shell=False, stdout=PIPE).stdout + fp = Popen(['antiword', fname], shell=False, stdout=PIPE).stdout return _to_unicode( fp.read()) cntIndex.register(DocIndex()) |
