diff options
| author | Harry (OpenERP) <hmo@tinyerp.com> | 2010-07-11 11:36:46 +0530 |
|---|---|---|
| committer | Harry (OpenERP) <hmo@tinyerp.com> | 2010-07-11 11:36:46 +0530 |
| commit | fad9edc19eb7e0ac32aab96f6f299e328b6574f2 (patch) | |
| tree | 6279c86757be0f9754e2c33d0a1d6388778084d4 /addons/document/document.py | |
| parent | 6ccff1aab8992bd56a9e0ac45ad5988b549ab530 (diff) | |
| parent | d6f1e1f29f8110c363d8a413da8dcf17284cc205 (diff) | |
[MERGE]
bzr revid: hmo@tinyerp.com-20100711060646-gwp6039vsln1x30a
Diffstat (limited to 'addons/document/document.py')
| -rw-r--r-- | addons/document/document.py | 131 |
1 files changed, 83 insertions, 48 deletions
diff --git a/addons/document/document.py b/addons/document/document.py index aa893b617c3..6ab6cae5281 100644 --- a/addons/document/document.py +++ b/addons/document/document.py @@ -28,6 +28,7 @@ import os import pooler import netsvc +from osv.orm import except_orm #import StringIO from psycopg2 import Binary @@ -72,33 +73,32 @@ class document_file(osv.osv): return res _columns = { - 'user_id': fields.many2one('res.users', 'Owner', select=1), - 'group_ids': fields.many2many('res.groups', 'document_group_rel', 'item_id', 'group_id', 'Groups'), - # the directory id now is mandatory. It can still be computed automatically. - 'parent_id': fields.many2one('document.directory', 'Directory', select=1), - 'file_size': fields.integer('File Size', required=True), - 'file_type': fields.char('Content Type', size=128), + # Columns from ir.attachment: + 'create_date': fields.datetime('Date Created', readonly=True), + 'create_uid': fields.many2one('res.users', 'Creator', readonly=True), + 'write_date': fields.datetime('Date Modified', readonly=True), + 'write_uid': fields.many2one('res.users', 'Last Modification User', readonly=True), + 'res_model': fields.char('Attached Model', size=64, readonly=True), + 'res_id': fields.integer('Attached ID', readonly=True), + # 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'), - '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), - 'url': fields.char('File URL',size=64), - '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), - 'type':fields.selection([ - ('url','URL'), - ('binary','Binary'), - ],'Type', help="Type is used to separate URL and binary File"), + # Fields of document: + 'user_id': fields.many2one('res.users', 'Owner', select=1), + # 'group_ids': fields.many2many('res.groups', 'document_group_rel', 'item_id', 'group_id', 'Groups'), + # the directory id now is mandatory. It can still be computed automatically. + 'parent_id': fields.many2one('document.directory', 'Directory', select=1, required=True), + 'index_content': fields.text('Indexed Content'), + 'partner_id':fields.many2one('res.partner', 'Partner', select=1), 'company_id': fields.many2one('res.company', 'Company'), + 'file_size': fields.integer('File Size', required=True), + 'file_type': fields.char('Content Type', size=128), + + # fields used for file storage + 'store_fname': fields.char('Stored Filename', size=200), } def __get_def_directory(self, cr, uid, context=None): @@ -109,8 +109,6 @@ class document_file(osv.osv): 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'ir.attachment', context=c), 'user_id': lambda self, cr, uid, ctx:uid, 'file_size': lambda self, cr, uid, ctx:0, - 'store_method': lambda *args: 'db', - 'type': 'binary', 'parent_id': __get_def_directory } _sql_constraints = [ @@ -122,7 +120,7 @@ class document_file(osv.osv): 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): + for file in self.browse(cr, uid, ids): # FIXME fields_only if not name: name = file.name if not parent_id: @@ -156,8 +154,41 @@ class document_file(osv.osv): return False 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) - cr.commit() + if ('parent_id' in vals) or ('name' in vals): + # perhaps this file is renaming or changing directory + nctx = nodes.get_node_context(cr,uid,context) + dirobj = self.pool.get('document.directory') + if 'parent_id' in vals: + dbro = dirobj.browse(cr, uid, vals['parent_id'], context=context) + dnode = nctx.get_dir_node(cr, dbro) + else: + dbro = None + dnode = None + ids2 = [] + result = False + for fbro in self.browse(cr, uid, ids, context=context): + if ('parent_id' not in vals or fbro.parent_id.id == vals['parent_id']) \ + and ('name' not in vals or fbro.name == vals['name']) : + ids2.append(fbro.id) + continue + fnode = nctx.get_file_node(cr, fbro) + res = fnode.move_to(cr, dnode or fnode.parent, vals.get('name', fbro.name), fbro, dbro, True) + if isinstance(res, dict): + vals2 = vals.copy() + vals2.update(res) + wid = res.get('id', fbro.id) + result = super(document_file,self).write(cr,uid,wid,vals2,context=context) + # TODO: how to handle/merge several results? + elif res == True: + ids2.append(fbro.id) + elif res == False: + pass + ids = ids2 + if 'file_size' in vals: # only write that field using direct SQL calls + del vals['file_size'] + if len(ids) and len(vals): + result = super(document_file,self).write(cr, uid, ids, vals, context=context) + cr.commit() # ? return result def create(self, cr, uid, vals, context=None): @@ -170,26 +201,10 @@ class document_file(osv.osv): 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']], ['name', 'partner_id', 'address_id'], context=context) - if len(result): - obj = result[0] - 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) - 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] - else: - vals['partner_id'] = obj['partner_id'] + if vals.get('res_id', False) and vals.get('res_model', False) \ + and not vals.get('partner_id', False): + vals['partner_id'] = self.__get_partner_id(cr, uid, \ + vals['res_model'], vals['res_id'], context) datas = None if vals.get('link', False) : @@ -198,13 +213,33 @@ class document_file(osv.osv): else: datas = vals.get('datas', False) - vals['file_size'] = datas and len(datas) or 0 + if datas: + vals['file_size'] = len(datas) + else: + if vals.get('file_size'): + del vals['file_size'] 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) - cr.commit() + cr.commit() # ? return result + def __get_partner_id(self, cr, uid, res_model, res_id, context): + """ A helper to retrieve the associated partner from any res_model+id + It is a hack that will try to discover if the mentioned record is + clearly associated with a partner record. + """ + obj_model = self.pool.get(res_model) + if obj_model._name == 'res.partner': + return res_id + elif 'partner_id' in obj_model._columns and obj_model._columns['partner_id']._obj == 'res.partner': + bro = obj_model.browse(self, cr, uid, res_id, context=context) + return bro.partner_id.id + elif 'address_id' in obj_model._columns and obj_model._columns['address_id']._obj == 'res.partner.address': + bro = obj_model.browse(self, cr, uid, res_id, context=context) + return bro.address_id.partner_id.id + return False + def unlink(self, cr, uid, ids, context={}): stor = self.pool.get('document.storage') unres = [] |
