[MIG] crm_project_issue: migrate into new api
Part of the code was refactor by applying `self.ensure_one()`, and removing the loop 'for'.
This commit is contained in:
committed by
Jérome Maes
parent
ee566f6042
commit
989bccca45
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
{
|
||||
'name': 'Lead to Issue',
|
||||
'version': '1.0',
|
||||
'summary': 'Create Issues from Leads',
|
||||
'sequence': '19',
|
||||
'category': 'Project Management',
|
||||
@@ -16,5 +17,4 @@ Link module to map leads to issues
|
||||
'views/crm_lead_views.xml'
|
||||
],
|
||||
'depends': ['crm', 'project_issue'],
|
||||
'installable': True,
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<field name="inherit_id" ref="crm.crm_case_form_view_leads" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//div[@name='button_box']" position="inside">
|
||||
<button class="oe_inline oe_stat_button" type="action" name="%(convert_lead2projectissue_wizard_action)d" icon="fa-bug" help="Convert to Issue">
|
||||
<button class="oe_stat_button" type="action" name="%(convert_lead2projectissue_wizard_action)d" icon="fa-bug" help="Convert to Issue">
|
||||
<div class="o_form_field o_stat_info">
|
||||
<span class="o_stat_text">Convert To</span>
|
||||
<span class="o_stat_text">Issue</span>
|
||||
|
||||
@@ -1,67 +1,58 @@
|
||||
|
||||
from openerp.osv import osv, fields
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class crm_lead_to_project_issue_wizard(osv.TransientModel):
|
||||
class CrmLeadToProjectIssueWizard(models.TransientModel):
|
||||
""" wizard to convert a Lead into a Project Issue and move the Mail Thread """
|
||||
|
||||
_name = "crm.lead2projectissue.wizard"
|
||||
_inherit = 'crm.partner.binding'
|
||||
|
||||
_columns = {
|
||||
"lead_id": fields.many2one("crm.lead", "Lead", domain=[("type", "=", "lead")]),
|
||||
"project_id": fields.many2one("project.project", "Project", domain=[("use_issues", "=", True)])
|
||||
}
|
||||
@api.model
|
||||
def default_get(self, fields):
|
||||
result = super(CrmLeadToProjectIssueWizard, self).default_get(fields)
|
||||
lead_id = self.env.context.get('active_id')
|
||||
if lead_id:
|
||||
result['lead_id'] = lead_id
|
||||
return result
|
||||
|
||||
_defaults = {
|
||||
"lead_id": lambda self, cr, uid, context=None: context.get('active_id')
|
||||
}
|
||||
lead_id = fields.Many2one('crm.lead', string='Lead', domain=[('type', '=', 'lead')])
|
||||
project_id = fields.Many2one('project.project', string='Project', domain=[('use_issues', '=', True)])
|
||||
|
||||
def action_lead_to_project_issue(self, cr, uid, ids, context=None):
|
||||
# get the wizards and models
|
||||
wizards = self.browse(cr, uid, ids, context=context)
|
||||
Lead = self.pool["crm.lead"]
|
||||
Issue = self.pool["project.issue"]
|
||||
Attachment = self.pool['ir.attachment']
|
||||
|
||||
for wizard in wizards:
|
||||
# get the lead to transform
|
||||
lead = wizard.lead_id
|
||||
|
||||
partner = self._find_matching_partner(cr, uid, context=context)
|
||||
if not partner and (lead.partner_name or lead.contact_name):
|
||||
partner_ids = Lead.handle_partner_assignation(cr, uid, [lead.id], context=context)
|
||||
partner = partner_ids[lead.id]
|
||||
|
||||
# create new project.issue
|
||||
vals = {
|
||||
"name": lead.name,
|
||||
"description": lead.description,
|
||||
"email_from": lead.email_from,
|
||||
"project_id": wizard.project_id.id,
|
||||
"partner_id": partner,
|
||||
"user_id": None
|
||||
}
|
||||
issue_id = Issue.create(cr, uid, vals, context=None)
|
||||
issue = Issue.browse(cr, uid, issue_id, context=None)
|
||||
# move the mail thread
|
||||
Lead.message_change_thread(cr, uid, [lead.id], issue, context=context)
|
||||
# Move attachments
|
||||
attachment_ids = Attachment.search(
|
||||
cr, uid, [('res_model', '=', 'crm.lead'), ('res_id', '=', lead.id)], context=context)
|
||||
Attachment.write(
|
||||
cr, uid, attachment_ids, {'res_model': 'project.issue', 'res_id': issue_id}, context=context)
|
||||
# Archive the lead
|
||||
Lead.write(cr, uid, [lead.id], {'active': False}, context=context)
|
||||
@api.multi
|
||||
def action_lead_to_project_issue(self):
|
||||
self.ensure_one()
|
||||
# get the lead to transform
|
||||
lead = self.lead_id
|
||||
partner_id = self._find_matching_partner()
|
||||
if not partner_id and (lead.partner_name or lead.contact_name):
|
||||
partner_id = lead.handle_partner_assignation()[lead.id]
|
||||
# create new project.issue
|
||||
vals = {
|
||||
"name": lead.name,
|
||||
"description": lead.description,
|
||||
"email_from": lead.email_from,
|
||||
"project_id": self.project_id.id,
|
||||
"partner_id": partner_id,
|
||||
"user_id": None
|
||||
}
|
||||
issue = self.env['project.issue'].create(vals)
|
||||
# move the mail thread
|
||||
lead.message_change_thread(issue)
|
||||
# move attachments
|
||||
attachments = self.env['ir.attachment'].search([('res_model', '=', 'crm.lead'), ('res_id', '=', lead.id)])
|
||||
attachments.write({'res_model': 'project.issue', 'res_id': issue.id})
|
||||
# archive the lead
|
||||
lead.write({'active': False})
|
||||
# return the action to go to the form view of the new Issue
|
||||
view_id = self.pool['ir.model.data'].xmlid_to_res_id(
|
||||
cr, uid, 'project_issue.project_issue_form_view')
|
||||
view = self.env.ref('project_issue.project_issue_form_view')
|
||||
return {
|
||||
'name': 'Issue created',
|
||||
'view_type': 'form',
|
||||
'view_mode': 'form',
|
||||
'view_id': view_id,
|
||||
'view_id': view.id,
|
||||
'res_model': 'project.issue',
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_id': issue_id,
|
||||
'context': context
|
||||
'res_id': issue.id,
|
||||
'context': self.env.context
|
||||
}
|
||||
|
||||
@@ -1,18 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
|
||||
<!-- action of converting, via wizard -->
|
||||
<record model="ir.actions.act_window" id="convert_lead2projectissue_wizard_action" >
|
||||
<field name="name">Convert to Issue</field>
|
||||
<field name="res_model">crm.lead2projectissue.wizard</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">form</field>
|
||||
<field name="target">new</field>
|
||||
</record>
|
||||
<odoo>
|
||||
|
||||
<!-- view of the Wizard Form -->
|
||||
<record model="ir.ui.view" id="view_crm_lead2projectissue_wizard" >
|
||||
<record id="view_crm_lead2projectissue_wizard" model="ir.ui.view">
|
||||
<field name="name">crm.lead2projectissue.wizard.form</field>
|
||||
<field name="model">crm.lead2projectissue.wizard</field>
|
||||
<field name="arch" type="xml">
|
||||
@@ -28,5 +18,13 @@
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
<!-- action of converting, via wizard -->
|
||||
<record id="convert_lead2projectissue_wizard_action" model="ir.actions.act_window">
|
||||
<field name="name">Convert to Issue</field>
|
||||
<field name="res_model">crm.lead2projectissue.wizard</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">form</field>
|
||||
<field name="target">new</field>
|
||||
</record>
|
||||
|
||||
</odoo>
|
||||
|
||||
Reference in New Issue
Block a user