[FIX] : modified according to tde's remarks

bzr revid: jem@openerp.com-20140218143038-msk038rdmrp1fox1
This commit is contained in:
Jérôme 2014-02-18 15:30:38 +01:00
parent f1e7517ab3
commit 41a4d00abe
2 changed files with 46 additions and 30 deletions

View File

@ -5,28 +5,38 @@ class crm_lead_to_project_issue_wizard(osv.TransientModel):
""" wizard to convert a Lead into a Project Issue and move the Mail Thread """ """ wizard to convert a Lead into a Project Issue and move the Mail Thread """
def action_lead_to_project_issue(self, cr, uid, ids, context=None): def action_lead_to_project_issue(self, cr, uid, ids, context=None):
# get the wizards # get the wizards and models
wizards = self.browse(cr, uid, ids, context=context) wizards = self.browse(cr, uid, ids, context=context)
lead_model = self.pool.get("crm.lead") Lead = self.pool["crm.lead"]
issue_model = self.pool.get("project.issue") Issue = self.pool["project.issue"]
lead_id = context.get('active_id',0)
for wizard in wizards: for wizard in wizards:
# get the lead to transform # get the lead to transform
lead = lead_model.browse(cr, uid, lead_id, context=context) lead = wizard.lead_id
# create new project.issue # create new project.issue
vals = {} vals = {"name": lead.name,
vals["name"] = lead.name "description": lead.description,
vals["description"] = lead.description "email_from": lead.email_from,
vals["email_from"] = lead.email_from "partner_id": lead.partner_id.id,
vals["partner_id"] = lead.partner_id.id "project_id": wizard.project_id.id
vals["project_id"] = wizard.project_id.id }
issue_id = issue_model.create(cr, uid, vals, context=None) issue_id = Issue.create(cr, uid, vals, context=None)
# move the mail thread # move the mail thread
lead_model.transform_model_messages(cr, uid, lead_id, issue_id, "project.issue", context=context) Lead.message_change_thread(cr, uid, lead.id, issue_id, "project.issue", context=context)
# delete the lead # delete the lead
lead_model.unlink(cr, uid, [lead_id], context=None) Lead.unlink(cr, uid, [lead.id], context=None)
return False # return the action to go to the form view of the new Issue
view_id = self.pool.get('ir.ui.view').search(cr,uid,[('model','=','project.issue'), ('name','=','project_issue_form_view')])
return {
'name': 'Issue created',
'view_type': 'form',
'view_mode': 'form',
'view_id': view_id,
'res_model': 'project.issue',
'type': 'ir.actions.act_window',
'res_id': issue_id,
'context' : context
}
_name = "crm.lead2projectissue.wizard" _name = "crm.lead2projectissue.wizard"
@ -34,4 +44,8 @@ class crm_lead_to_project_issue_wizard(osv.TransientModel):
_columns = { _columns = {
"lead_id" : fields.many2one("crm.lead","Lead", domain=[("type","=","lead")]), "lead_id" : fields.many2one("crm.lead","Lead", domain=[("type","=","lead")]),
"project_id" : fields.many2one("project.project", "Project", domain=[("use_issues","=",True)]) "project_id" : fields.many2one("project.project", "Project", domain=[("use_issues","=",True)])
}
_defaults = {
"lead_id" : lambda self, cr, uid, context=None: context.get('active_id')
} }

View File

@ -1798,7 +1798,7 @@ class mail_thread(osv.AbstractModel):
return sorted(threads, key=lambda x: (x['popularity'], x['id']), reverse=True)[:3] return sorted(threads, key=lambda x: (x['popularity'], x['id']), reverse=True)[:3]
def transform_model_messages(self, cr, uid, id, new_res_id, new_model, context=None): def message_change_thread(self, cr, uid, id, new_res_id, new_model, context=None):
""" """
Transfert the list of the mail thread messages from an model to another Transfert the list of the mail thread messages from an model to another
@ -1806,24 +1806,26 @@ class mail_thread(osv.AbstractModel):
:param new_res_id : the new res_id of the mail.message :param new_res_id : the new res_id of the mail.message
:param new_model : the name of the new model of the mail.message :param new_model : the name of the new model of the mail.message
Example : self.pool.get("crm.lead").transform_model_messages(self, cr, uid, 2, 4, "project.issue", context) Example : self.pool.get("crm.lead").message_change_thread(self, cr, uid, 2, 4, "project.issue", context)
will transfert thread of the lead (id=2) to the issue (id=4) will transfert thread of the lead (id=2) to the issue (id=4)
""" """
# get the message ids belonging to the thread to migrate # get the sbtype id of the comment Message
message_obj = self.pool.get('mail.message')
msg_ids = message_obj.search(cr, uid, [
('model', '=', self._name),
('res_id', '=', id)], context=context)
subtype_res_id = self.pool.get('ir.model.data').xmlid_to_res_id(cr, uid, 'mail.mt_comment', raise_if_not_found=True) subtype_res_id = self.pool.get('ir.model.data').xmlid_to_res_id(cr, uid, 'mail.mt_comment', raise_if_not_found=True)
# get the ids of the comment and none-comment of the thread
message_obj = self.pool.get('mail.message')
msg_ids_comment = message_obj.search(cr, uid, [
('model', '=', self._name),
('res_id', '=', id),
('subtype_id.id', '=', subtype_res_id)], context=context)
msg_ids_not_comment = message_obj.search(cr, uid, [
('model', '=', self._name),
('res_id', '=', id),
('subtype_id.id', '!=', subtype_res_id)], context=context)
# update the messages # update the messages
for message in message_obj.browse(cr, uid, msg_ids, context=context): message_obj.write(cr, uid, msg_ids_comment, {"res_id" : new_res_id, "model" : new_model}, context=context)
if subtype_res_id == message.subtype_id.id: message_obj.write(cr, uid, msg_ids_not_comment, {"res_id" : new_res_id, "model" : new_model, "subtype_id" : None}, context=context)
# no change the subtype
message_obj.write(cr, uid, message.id, {"res_id" : new_res_id, "model" : new_model}, context=context)
else:
# modify the subtype
message_obj.write(cr, uid, message.id, {"res_id" : new_res_id, "model" : new_model, "subtype_id" : None}, context=context)
return True return True