[MERGE] crm, crm_iap_enrich, various: improve IAP enrich

PURPOSE

Cleanup automatic enrichment of leads.

SPECIFICATIONS

Right now, when we choose to automatically enrich the leads from CRM
settings, it activates a cron to periodically enrich the leads using
IAP service.

This commit improves the behavior by enriching the leads after the
records creation using cron trigger. That way it is done nearly after
creation and user gets enrich information sooner.

To make it clear to users, the description for 'auto' mode is improved to
'Enrich all leads on creation'.

Also, now we select 'auto' mode by default instead of 'manual', and
display 'Enrich' button on form view irrespective of the selected mode
(if lead meets certain conditions) unlike before. Rest of the behavior
is still same as before. For example, we still have server action which
can enrich the selected leads in batch, which is useful if there are
existing leads before we enable 'Lead Enrichment' feature.

SIDE SPECIFICATIONS

Improve lead / opportunity marketing information display on form view.

Send html toast notifications when running out of IAP credits for enrich.

Fix some corner cases for lead enrich (involving probability, email).

LINKS

Task-2269743

closes odoo/odoo#60605

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Odoo's Mergebot
2021-09-28 18:27:18 +02:00
committed by GitHub
8 changed files with 37 additions and 21 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ class ResConfigSettings(models.TransientModel):
lead_enrich_auto = fields.Selection([
('manual', 'Enrich leads on demand only'),
('auto', 'Enrich all leads automatically'),
], string='Enrich lead automatically', default='manual', config_parameter='crm.iap.lead.enrich.setting')
], string='Enrich lead automatically', default='auto', config_parameter='crm.iap.lead.enrich.setting')
lead_mining_in_pipeline = fields.Boolean("Create a lead mining request directly from the opportunity pipeline.", config_parameter='crm.lead_mining_in_pipeline')
predictive_lead_scoring_start_date = fields.Date(string='Lead Scoring Starting Date', compute="_compute_pls_start_date", inverse="_inverse_pls_start_date_str")
predictive_lead_scoring_start_date_str = fields.Char(string='Lead Scoring Starting Date in String', config_parameter='crm.pls_start_date')
+5 -5
View File
@@ -261,7 +261,7 @@
<group string="Email" groups="base.group_no_one">
<field name="message_bounce" readonly="1"/>
</group>
<group string="Tracking" name="categorization">
<group string="Marketing" name="categorization">
<field name="company_id"
groups="base.group_multi_company"
options="{'no_create': True}"/>
@@ -312,11 +312,11 @@
<field name="campaign_id" />
<field name="medium_id" />
<field name="source_id" />
</group>
<group string="Misc" name="Misc">
<field name="day_open" groups="base.group_no_one"/>
<field name="day_close" groups="base.group_no_one"/>
<field name="referred"/>
</group>
<group string="Tracking" name="Misc" groups="base.group_no_one">
<field name="day_open" />
<field name="day_close"/>
<field name="type" invisible="1"/>
</group>
</group>
@@ -127,7 +127,7 @@
<div class="o_setting_right_pane" id="crm_iap_enrich_settings">
<label string="Lead Enrichment" for="module_crm_iap_enrich"/>
<div class="text-muted">
Enrich your leads automatically with company data based on their email address
Enrich your leads with company data based on their email addresses
</div>
<div class="mt8" attrs="{'invisible': [('module_crm_iap_enrich','=',False)]}">
<field name="lead_enrich_auto" class="o_light_label" widget="radio" required="True"/>
+2 -2
View File
@@ -10,5 +10,5 @@ def _synchronize_cron(cr, registry):
env = Environment(cr, SUPERUSER_ID, {'active_test': False})
cron = env.ref('crm_iap_enrich.ir_cron_lead_enrichment')
if cron:
config = env['ir.config_parameter'].get_param('crm.iap.lead.enrich.setting', 'manual')
cron.active = config != 'manual'
config = env['ir.config_parameter'].get_param('crm.iap.lead.enrich.setting', 'auto')
cron.active = config == 'auto'
@@ -16,9 +16,9 @@
</template>
<template id="mail_message_lead_enrich_no_credit">
<p>Lead enriched (based on email address)</p>
<div style="background-color:#ffffff;padding:15px;">
<span>Your balance for Lead Enrichment is insufficient. Please go to your <a t-attf-href="{{url}}" target="_blank">IAP account</a> to buy credits.</span>
<div>
<span>Not Enough Credits for Lead Enrichment.</span><br/>
<a t-attf-href="{{url}}" target="_blank"><i class="fa fa-arrow-right"/> Buy More Credits</a>
</div>
</template>
</data></odoo>
+23 -9
View File
@@ -3,6 +3,7 @@
import datetime
import logging
from psycopg2 import OperationalError
from odoo import _, api, fields, models, tools
@@ -19,10 +20,6 @@ class Lead(models.Model):
@api.depends('email_from', 'probability', 'iap_enrich_done', 'reveal_id')
def _compute_show_enrich_button(self):
config = self.env['ir.config_parameter'].sudo().get_param('crm.iap.lead.enrich.setting', 'manual')
if not config or config != 'manual':
self.show_enrich_button = False
return
for lead in self:
if not lead.active or not lead.email_from or lead.email_state == 'incorrect' or lead.iap_enrich_done or lead.reveal_id or lead.probability == 100:
lead.show_enrich_button = False
@@ -36,11 +33,21 @@ class Lead(models.Model):
leads = self.search([
('iap_enrich_done', '=', False),
('reveal_id', '=', False),
('probability', '<', 100),
'|', ('probability', '<', 100), ('probability', '=', False),
('create_date', '>', timeDelta)
])
leads.iap_enrich(from_cron=True)
@api.model_create_multi
def create(self, vals_list):
leads = super(Lead, self).create(vals_list)
enrich_mode = self.env['ir.config_parameter'].sudo().get_param('crm.iap.lead.enrich.setting', 'auto')
if enrich_mode == 'auto':
cron = self.env.ref('crm_iap_enrich.ir_cron_lead_enrichment', raise_if_not_found=False)
if cron:
cron._trigger()
return leads
def iap_enrich(self, from_cron=False):
# Split self in a list of sub-recordsets or 50 records to prevent timeouts
batches = [self[index:index + 50] for index in range(0, len(self), 50)]
@@ -55,6 +62,9 @@ class Lead(models.Model):
# If lead is lost, active == False, but is anyway removed from the search in the cron.
if lead.probability == 100 or lead.iap_enrich_done:
continue
# Skip if no email (different from wrong email leading to no email_normalized)
if not lead.email_from:
continue
normalized_email = tools.email_normalize(lead.email_from)
if not normalized_email:
@@ -82,10 +92,14 @@ class Lead(models.Model):
data = {
'url': self.env['iap.account'].get_credits_url('reveal'),
}
leads[0].message_post_with_view(
'crm_iap_enrich.mail_message_lead_enrich_no_credit',
values=data,
subtype_id=self.env.ref('mail.mt_note').id)
self.env['bus.bus'].sendone(
(self._cr.dbname, 'res.partner', self.env.user.partner_id.id),
{
'type': 'simple_notification',
'message': self.env.ref('crm_iap_enrich.mail_message_lead_enrich_no_credit')._render(data),
'message_is_html': True,
}
)
# Since there are no credits left, there is no point to process the other batches
break
except Exception as e:
+1
View File
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from contextlib import contextmanager
from unittest.mock import patch
@@ -508,6 +508,7 @@ function factory(dependencies) {
this.env.services['notification'].notify({
message: data.message,
sticky: data.sticky,
messageIsHtml: data.message_is_html,
type: data.warning ? 'warning' : 'danger',
});
} else if (type === 'toggle_star') {