summaryrefslogtreecommitdiff
path: root/addons/document/document.py
diff options
context:
space:
mode:
authorXavier Morel <xmo@tinyerp.com>2010-02-24 11:55:29 +0100
committerXavier Morel <xmo@tinyerp.com>2010-02-24 11:55:29 +0100
commit3458b06eba04858dec92f6f46ccf56658419178c (patch)
treead36c177a3a0927ef2f430a53493304eba23623e /addons/document/document.py
parentc062d78c0212ff487238cd487b208b5b08d10897 (diff)
parent0adc5881463d83f956faabec28db9a911bb20fca (diff)
[merge] ftp deactivation branch, thanks greg-apieum
bzr revid: xmo@tinyerp.com-20100224105529-4fs9rhk6x2mm28c4
Diffstat (limited to 'addons/document/document.py')
-rw-r--r--addons/document/document.py151
1 files changed, 76 insertions, 75 deletions
diff --git a/addons/document/document.py b/addons/document/document.py
index 5613cc55958..824607fa2e8 100644
--- a/addons/document/document.py
+++ b/addons/document/document.py
@@ -36,26 +36,27 @@ import tools
from tools.translate import _
import nodes
+DMS_ROOT_PATH = tools.config.get('document_path', os.path.join(tools.config['root_path'], 'filestore'))
class document_file(osv.osv):
_inherit = 'ir.attachment'
_rec_name = 'datas_fname'
def _get_filestore(self, cr):
- return os.path.join(tools.config['root_path'], 'filestore', cr.dbname)
+ return os.path.join(DMS_ROOT_PATH, cr.dbname)
def _data_get(self, cr, uid, ids, name, arg, context):
- fbrl = self.browse(cr,uid,ids,context=context)
- nctx = nodes.get_node_context(cr,uid,context)
+ fbrl = self.browse(cr, uid, ids, context=context)
+ nctx = nodes.get_node_context(cr, uid, context)
result = {}
bin_size = context.get('bin_size', False)
for fbro in fbrl:
- fnode = nodes.node_file(None,None,nctx,fbro)
+ fnode = nodes.node_file(None, None, nctx, fbro)
if not bin_size:
- data = fnode.get_data(cr,fbro)
+ data = fnode.get_data(cr, fbro)
result[fbro.id] = base64.encodestring(data or '')
else:
- result[fbro.id] = fnode.get_data_len(cr,fbro)
-
+ result[fbro.id] = fnode.get_data_len(cr, fbro)
+
return result
#
@@ -64,10 +65,10 @@ class document_file(osv.osv):
def _data_set(self, cr, uid, id, name, value, arg, context):
if not value:
return True
- fbro = self.browse(cr,uid,id,context=context)
- nctx = nodes.get_node_context(cr,uid,context)
- fnode = nodes.node_file(None,None,nctx,fbro)
- res = fnode.set_data(cr,base64.decodestring(value),fbro)
+ fbro = self.browse(cr, uid, id, context=context)
+ nctx = nodes.get_node_context(cr, uid, context)
+ fnode = nodes.node_file(None, None, nctx, fbro)
+ res = fnode.set_data(cr, base64.decodestring(value), fbro)
return res
_columns = {
@@ -79,138 +80,138 @@ class document_file(osv.osv):
'file_type': fields.char('Content Type', size=32),
# If ir.attachment contained any data before document is installed, preserve
# the data, don't drop the column!
- 'db_datas': fields.binary('Data',oldname='datas'),
+ 'db_datas': fields.binary('Data', oldname='datas'),
'index_content': fields.text('Indexed Content'),
'write_date': fields.datetime('Date Modified', readonly=True),
'write_uid': fields.many2one('res.users', 'Last Modification User', readonly=True),
'create_date': fields.datetime('Date Created', readonly=True),
'create_uid': fields.many2one('res.users', 'Creator', readonly=True),
- 'store_method': fields.selection([('db','Database'),('fs','Filesystem'),('link','Link')], "Storing Method"),
- 'datas': fields.function(_data_get,method=True,fnct_inv=_data_set,string='File Content',type="binary", nodrop=True),
+ 'store_method': fields.selection([('db', 'Database'), ('fs', 'Filesystem'), ('link', 'Link')], "Storing Method"),
+ 'datas': fields.function(_data_get, method=True, fnct_inv=_data_set, string='File Content', type="binary", nodrop=True),
'store_fname': fields.char('Stored Filename', size=200),
'res_model': fields.char('Attached Model', size=64), #res_model
'res_id': fields.integer('Attached ID'), #res_id
'partner_id':fields.many2one('res.partner', 'Partner', select=1),
- 'title': fields.char('Resource Title',size=64),
+ 'title': fields.char('Resource Title', size=64),
}
- def __get_def_directory(self,cr,uid, context = None):
+ def __get_def_directory(self, cr, uid, context=None):
dirobj = self.pool.get('document.directory')
- return dirobj._get_root_directory(cr,uid,context)
+ return dirobj._get_root_directory(cr, uid, context)
_defaults = {
- 'user_id': lambda self,cr,uid,ctx:uid,
- 'file_size': lambda self,cr,uid,ctx:0,
+ 'user_id': lambda self, cr, uid, ctx:uid,
+ 'file_size': lambda self, cr, uid, ctx:0,
'store_method': lambda *args: 'db',
'parent_id': __get_def_directory
}
_sql_constraints = [
('filename_uniq', 'unique (name,parent_id,res_id,res_model)', 'The file name must be unique !')
]
- def _check_duplication(self, cr, uid,vals,ids=[],op='create'):
- name=vals.get('name',False)
- parent_id=vals.get('parent_id',False)
- res_model=vals.get('res_model',False)
- res_id=vals.get('res_id',0)
- if op=='write':
- for file in self.browse(cr,uid,ids):
+ def _check_duplication(self, cr, uid, vals, ids=[], op='create'):
+ name = vals.get('name', False)
+ parent_id = vals.get('parent_id', False)
+ res_model = vals.get('res_model', False)
+ res_id = vals.get('res_id', 0)
+ if op == 'write':
+ for file in self.browse(cr, uid, ids):
if not name:
- name=file.name
+ name = file.name
if not parent_id:
- parent_id=file.parent_id and file.parent_id.id or False
+ parent_id = file.parent_id and file.parent_id.id or False
if not res_model:
- res_model=file.res_model and file.res_model or False
+ res_model = file.res_model and file.res_model or False
if not res_id:
- res_id=file.res_id and file.res_id or 0
- res=self.search(cr,uid,[('id','<>',file.id),('name','=',name),('parent_id','=',parent_id),('res_model','=',res_model),('res_id','=',res_id)])
+ res_id = file.res_id and file.res_id or 0
+ res = self.search(cr, uid, [('id', '<>', file.id), ('name', '=', name), ('parent_id', '=', parent_id), ('res_model', '=', res_model), ('res_id', '=', res_id)])
if len(res):
return False
- if op=='create':
- res=self.search(cr,uid,[('name','=',name),('parent_id','=',parent_id),('res_id','=',res_id),('res_model','=',res_model)])
+ if op == 'create':
+ res = self.search(cr, uid, [('name', '=', name), ('parent_id', '=', parent_id), ('res_id', '=', res_id), ('res_model', '=', res_model)])
if len(res):
return False
return True
def copy(self, cr, uid, id, default=None, context=None):
if not default:
- default ={}
+ default = {}
if 'name' not in default:
name = self.read(cr, uid, [id])[0]['name']
- default.update({'name': name+ " (copy)"})
- return super(document_file,self).copy(cr,uid,id,default,context)
+ default.update({'name': name + " (copy)"})
+ return super(document_file, self).copy(cr, uid, id, default, context)
def write(self, cr, uid, ids, vals, context=None):
- res=self.search(cr,uid,[('id','in',ids)])
+ res = self.search(cr, uid, [('id', 'in', ids)])
if not len(res):
return False
- if not self._check_duplication(cr,uid,vals,ids,'write'):
+ if not self._check_duplication(cr, uid, vals, ids, 'write'):
raise osv.except_osv(_('ValidateError'), _('File name must be unique!'))
- result = super(document_file,self).write(cr,uid,ids,vals,context=context)
+ result = super(document_file, self).write(cr, uid, ids, vals, context=context)
cr.commit()
return result
def create(self, cr, uid, vals, context=None):
if not context:
context = {}
- vals['title']=vals['name']
- vals['parent_id'] = context.get('parent_id',False) or vals.get('parent_id',False)
- if not vals.get('res_id', False) and context.get('default_res_id',False):
- vals['res_id']=context.get('default_res_id',False)
- if not vals.get('res_model', False) and context.get('default_res_model',False):
- vals['res_model']=context.get('default_res_model',False)
- if vals.get('res_id', False) and vals.get('res_model',False):
- obj_model=self.pool.get(vals['res_model'])
+ vals['title'] = vals['name']
+ vals['parent_id'] = context.get('parent_id', False) or vals.get('parent_id', False)
+ if not vals.get('res_id', False) and context.get('default_res_id', False):
+ vals['res_id'] = context.get('default_res_id', False)
+ if not vals.get('res_model', False) and context.get('default_res_model', False):
+ vals['res_model'] = context.get('default_res_model', False)
+ if vals.get('res_id', False) and vals.get('res_model', False):
+ obj_model = self.pool.get(vals['res_model'])
result = obj_model.read(cr, uid, [vals['res_id']], context=context)
if len(result):
- obj=result[0]
- if obj.get('name',False):
- vals['title'] = (obj.get('name',''))[:60]
- if obj_model._name=='res.partner':
- vals['partner_id']=obj['id']
- elif obj.get('address_id',False):
- if isinstance(obj['address_id'],tuple) or isinstance(obj['address_id'],list):
- address_id=obj['address_id'][0]
+ obj = result[0]
+ if obj.get('name', False):
+ vals['title'] = (obj.get('name', ''))[:60]
+ if obj_model._name == 'res.partner':
+ vals['partner_id'] = obj['id']
+ elif obj.get('address_id', False):
+ if isinstance(obj['address_id'], tuple) or isinstance(obj['address_id'], list):
+ address_id = obj['address_id'][0]
else:
- address_id=obj['address_id']
- address=self.pool.get('res.partner.address').read(cr,uid,[address_id],context=context)
+ address_id = obj['address_id']
+ address = self.pool.get('res.partner.address').read(cr, uid, [address_id], context=context)
if len(address):
- vals['partner_id']=address[0]['partner_id'][0] or False
- elif obj.get('partner_id',False):
- if isinstance(obj['partner_id'],tuple) or isinstance(obj['partner_id'],list):
- vals['partner_id']=obj['partner_id'][0]
+ vals['partner_id'] = address[0]['partner_id'][0] or False
+ elif obj.get('partner_id', False):
+ if isinstance(obj['partner_id'], tuple) or isinstance(obj['partner_id'], list):
+ vals['partner_id'] = obj['partner_id'][0]
else:
- vals['partner_id']=obj['partner_id']
+ vals['partner_id'] = obj['partner_id']
- datas=None
- if vals.get('link',False) :
+ datas = None
+ if vals.get('link', False) :
import urllib
- datas=base64.encodestring(urllib.urlopen(vals['link']).read())
+ datas = base64.encodestring(urllib.urlopen(vals['link']).read())
else:
- datas = vals.get('datas',False)
+ datas = vals.get('datas', False)
- vals['file_size']= datas and len(datas) or 0
- if not self._check_duplication(cr,uid,vals):
+ vals['file_size'] = datas and len(datas) or 0
+ if not self._check_duplication(cr, uid, vals):
raise osv.except_osv(_('ValidateError'), _('File name must be unique!'))
- result = super(document_file,self).create(cr, uid, vals, context)
+ result = super(document_file, self).create(cr, uid, vals, context)
cr.commit()
return result
- def unlink(self,cr, uid, ids, context={}):
+ def unlink(self, cr, uid, ids, context={}):
stor = self.pool.get('document.storage')
- unres= []
+ unres = []
# We have to do the unlink in 2 stages: prepare a list of actual
# files to be unlinked, update the db (safer to do first, can be
# rolled back) and then unlink the files. The list wouldn't exist
# after we discard the objects
-
+
for f in self.browse(cr, uid, ids, context):
# TODO: update the node cache
- r = stor.prepare_unlink(cr,uid,f.parent_id.storage_id, f)
+ r = stor.prepare_unlink(cr, uid, f.parent_id.storage_id, f)
if r:
unres.append(r)
res = super(document_file, self).unlink(cr, uid, ids, context)
- stor.do_unlink(cr,uid,unres)
+ stor.do_unlink(cr, uid, unres)
return res
-
+
document_file()