[FIX] Clean and Rewrite the code of the previous commit

bzr revid: stw@openerp.com-20110914103959-c2kbbmosrkv3w6ge
This commit is contained in:
Stephane Wirtel 2011-09-14 12:39:59 +02:00
parent 28986d87fb
commit 4b9d94d3b9
1 changed files with 14 additions and 26 deletions

View File

@ -49,20 +49,9 @@ class crm_merge_opportunity(osv.osv_memory):
def get_attachments(self, cr, uid, id, context=None):
attach_obj = self.pool.get('ir.attachment')
attach_ids = attach_obj.search(cr, uid, [('res_model' , '=', 'crm.lead'), ('res_id', '=', id)])
if attach_ids:
return attach_obj.browse(cr, uid, attach_ids)
else:
return False
def set_attachements_res_id(self, cr, uid, op_id, attachments, context=None):
attach_obj = self.pool.get('ir.attachment')
for attachment in attachments:
attachment.update({'res_id' : op_id})
id = attachment['id']
del attachment['id']
attach_obj.write(cr, uid, id, attachment)
proxy = self.pool.get('ir.attachment')
ids = proxy.search(cr, uid, [('res_model', '=', 'crm.lead'), ('res_id', '=', id)], context=context)
return proxy.browse(cr, uid, ids, context=context)
def find_oldest(self, cr, uid, op_ids, context=None):
if not context:
@ -138,23 +127,22 @@ class crm_merge_opportunity(osv.osv_memory):
tail_opportunities = opportunities_list[1:]
data = self._update_data(op_ids, oldest_opp)
#copy message into the first opportunity + merge attachement
attach = []
count = 1
first_attachments = self.get_attachments(cr, uid, first_opportunity, context=context)
for opp in tail_opportunities:
attachments = self.get_attachments(cr, uid, opp, context=context)
if attachments:
for first in first_attachments:
res = {}
for attachment in attachments:
if attachment.name == first.name:
res.update({'name':attachment.name+'('+`count`+')'})
res.update({'id':[attachment.id]})
attach.append(res)
count+=1
for first in first_attachments:
for attachment in attachments:
if attachment.name == first.name:
values = dict(
name = "%s (%s)" % (attachment.name, count,),
res_id = first_opportunity.id,
)
attachment.write(values)
count+=1
self.set_attachements_res_id(cr, uid, first_opportunity.id, attach)
for history in opp.message_ids:
message_obj.write(cr, uid, history.id, {'res_id': first_opportunity.id, 'name' : _("From %s : %s") % (opp.name, history.name) }, context=context)
@ -190,7 +178,7 @@ class crm_merge_opportunity(osv.osv_memory):
opp_obj._history(cr, uid, [first_opportunity], subject, details=details)
#data.update({'message_ids' : [(6, 0 ,self._concat_o2m('message_ids', op_ids))]})
opp_obj.write(cr, uid, [first_opportunity.id], data)
opp_obj.write(cr, uid, [first_opportunity.id], data, context=context)
unlink_ids = map(lambda x: x.id, tail_opportunities)
opp_obj.unlink(cr, uid, unlink_ids, context=context)