summaryrefslogtreecommitdiff
path: root/addons/crm_iap_lead_enrich
diff options
context:
space:
mode:
authorFlorent Lejoly <fle@odoo.com>2019-09-24 13:02:00 +0000
committerThibault Delavallée <tde@odoo.com>2019-09-25 17:30:04 +0000
commit211dba4a83a12f2084083e4ef395c85fb6429d1e (patch)
tree8ed5412ef9e50d8b0fe0effde0bdf26d7f9e02e4 /addons/crm_iap_lead_enrich
parentb7b3c33aa3dc9417c45f3bbb35e108c9af436000 (diff)
[IMP] crm_iap_lead_enrich: improve settings of enrichment
Before this commit it was not possible to enrich leads manually as only a cron enriches leads. It is considered as sub optimal since people would like to do it manually on some leads and not activate it only for all leads, using a cron. After this commit an option is added in CRM settings to choose the type of enrichment: either manual, either automatic. In manual mode, a button appears on the lead to enrich if certain conditions are met (email_adress present, not already enriched,...). It also deactivates the enrich cron. In automatic mode, the button on lead is not displayed and the cron is activated. A config parameter is defined in CRM as it is requested to be able to configure it directly in CRM without having to first install the app then configure it. A post install hook to synchronize cron status with config parameter is therefore required. FP/AJU request Task 2075817 closes odoo/odoo#37367 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Diffstat (limited to 'addons/crm_iap_lead_enrich')
-rw-r--r--addons/crm_iap_lead_enrich/__init__.py10
-rw-r--r--addons/crm_iap_lead_enrich/__manifest__.py4
-rw-r--r--addons/crm_iap_lead_enrich/data/ir_action.xml16
-rw-r--r--addons/crm_iap_lead_enrich/data/mail_data.xml21
-rw-r--r--addons/crm_iap_lead_enrich/models/__init__.py1
-rw-r--r--addons/crm_iap_lead_enrich/models/crm_lead.py38
-rw-r--r--addons/crm_iap_lead_enrich/models/res_config_settings.py16
-rw-r--r--addons/crm_iap_lead_enrich/tests/test_lead_enrich.py6
-rw-r--r--addons/crm_iap_lead_enrich/views/crm_lead_views.xml20
-rw-r--r--addons/crm_iap_lead_enrich/views/res_config_settings_views.xml13
10 files changed, 85 insertions, 60 deletions
diff --git a/addons/crm_iap_lead_enrich/__init__.py b/addons/crm_iap_lead_enrich/__init__.py
index 90b17558ffc..d9d57a38568 100644
--- a/addons/crm_iap_lead_enrich/__init__.py
+++ b/addons/crm_iap_lead_enrich/__init__.py
@@ -3,3 +3,13 @@
from . import models
from . import tests
+
+from odoo.api import Environment, SUPERUSER_ID
+
+
+def _synchronize_cron(cr, registry):
+ env = Environment(cr, SUPERUSER_ID, {'active_test': False})
+ cron = env.ref('crm_iap_lead_enrich.ir_cron_lead_enrichment')
+ if cron:
+ config = env['ir.config_parameter'].get_param('crm.iap.lead.enrich.setting', 'manual')
+ cron.active = config != 'manual'
diff --git a/addons/crm_iap_lead_enrich/__manifest__.py b/addons/crm_iap_lead_enrich/__manifest__.py
index c0a8e8e6f45..5860bd4ff31 100644
--- a/addons/crm_iap_lead_enrich/__manifest__.py
+++ b/addons/crm_iap_lead_enrich/__manifest__.py
@@ -11,7 +11,9 @@
],
'data': [
'data/ir_cron.xml',
+ 'data/ir_action.xml',
'data/mail_data.xml',
- 'views/res_config_settings_views.xml',
+ 'views/crm_lead_views.xml',
],
+ 'post_init_hook': '_synchronize_cron',
}
diff --git a/addons/crm_iap_lead_enrich/data/ir_action.xml b/addons/crm_iap_lead_enrich/data/ir_action.xml
new file mode 100644
index 00000000000..d49171bd232
--- /dev/null
+++ b/addons/crm_iap_lead_enrich/data/ir_action.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<odoo>
+ <data>
+ <record id="action_enrich_mail" model="ir.actions.server" >
+ <field name="name">Enrich</field>
+ <field name="model_id" ref="model_crm_lead"/>
+ <field name="binding_model_id" ref="crm.model_crm_lead"/>
+ <field name="state">code</field>
+ <field name="code">
+ if records:
+ records.iap_enrich()
+ </field>
+ </record>
+
+ </data>
+</odoo>
diff --git a/addons/crm_iap_lead_enrich/data/mail_data.xml b/addons/crm_iap_lead_enrich/data/mail_data.xml
index 6aa47f2f7e9..9f8482618ee 100644
--- a/addons/crm_iap_lead_enrich/data/mail_data.xml
+++ b/addons/crm_iap_lead_enrich/data/mail_data.xml
@@ -1,24 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo><data noupdate="1">
- <!-- MAIL TEMPLATE -->
- <record id="mail_template_data_iap_lead_enrich_nocredit" model="mail.template">
- <field name="name">CRM: IAP Lead Enrichment No Credit</field>
- <field name="subject">IAP Lead Enrichment Notification</field>
- <field name="model_id" ref="iap.model_iap_account"/>
- <field name="email_from">iap@odoo.com</field>
- <field name="email_to">${object.create_uid.email}</field>
- <field name="body_html" type="html">
-<div style="margin: 0px; padding: 0px;">
- <p>Dear ${object.create_uid.name or 'Madam/Sir'},</p><br/>
- <p>Unfortunately, there are no more credits on your IAP Lead Enrichment account.<br/>
- You can charge it back from the Settings of the CRM app or from your IAP portal.<br/></p><br/>
- <p>Best regards,</p><br/>
- <p>Odoo S.A.</p>
-</div>
- </field>
- <field name="lang">${object.create_uid.lang}</field>
- </record>
-
<!-- VIEWS USED FOR MESSAGING -->
<template id="mail_message_lead_enrich_notfound">
<p>Lead Enrichment based on email address</p>
@@ -37,7 +18,7 @@
<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>
+ <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>
</template>
diff --git a/addons/crm_iap_lead_enrich/models/__init__.py b/addons/crm_iap_lead_enrich/models/__init__.py
index 0e5cee2fdbf..dcb5aa3d8a4 100644
--- a/addons/crm_iap_lead_enrich/models/__init__.py
+++ b/addons/crm_iap_lead_enrich/models/__init__.py
@@ -3,3 +3,4 @@
from . import crm_lead
from . import iap_enrich_api
+from . import res_config_settings
diff --git a/addons/crm_iap_lead_enrich/models/crm_lead.py b/addons/crm_iap_lead_enrich/models/crm_lead.py
index dd6e4b9ee1d..86bc421c54a 100644
--- a/addons/crm_iap_lead_enrich/models/crm_lead.py
+++ b/addons/crm_iap_lead_enrich/models/crm_lead.py
@@ -15,6 +15,19 @@ class Lead(models.Model):
reveal_id = fields.Char(string='Reveal ID', index=True)
iap_enrich_done = fields.Boolean(string='Enrichment done', help='Whether IAP service for lead enrichment based on email has been performed on this lead.')
+ show_enrich_button = fields.Boolean(string='Allow manual enrich', compute="_compute_show_enrich_button")
+
+ @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.iap_enrich_done or lead.reveal_id or lead.probability == 100:
+ lead.show_enrich_button = False
+ else:
+ lead.show_enrich_button = True
@api.model
def _iap_enrich_leads_cron(self):
@@ -26,9 +39,9 @@ class Lead(models.Model):
('probability', '<', 100),
('create_date', '>', timeDelta)
])
- leads._iap_enrich(from_cron=True)
+ leads.iap_enrich(from_cron=True)
- def _iap_enrich(self, from_cron=False):
+ def iap_enrich(self, from_cron=False):
lead_emails = {}
for lead in self:
# If lead is lost, active == False, but is anyway removed from the search in the cron.
@@ -47,7 +60,6 @@ class Lead(models.Model):
iap_response = self.env['iap.enrich.api']._request_enrich(lead_emails)
except InsufficientCreditError:
_logger.info('Sent batch %s enrich requests: failed because of credit', len(lead_emails))
- self._iap_enrich_notify_no_more_credit()
if not from_cron:
data = {
'url': self.env['iap.account'].get_credits_url('reveal'),
@@ -61,7 +73,6 @@ class Lead(models.Model):
else:
_logger.info('Sent batch %s enrich requests: success', len(lead_emails))
self._iap_enrich_from_response(iap_response)
- self.env['ir.config_parameter'].sudo().set_param('crm_iap_lead_enrich.credit_notification', False)
@api.model
def _iap_enrich_from_response(self, iap_response):
@@ -124,22 +135,3 @@ class Lead(models.Model):
'timezone_url': company_data.get('timezone_url'),
})
return log_data
-
- @api.model
- def _iap_enrich_notify_no_more_credit(self):
- """ Notify when user has no credits anymore. In order to avoid to spam
- people each hour, an ir.config_parameter is set. """
- already_notified = self.env['ir.config_parameter'].sudo().get_param('crm_iap_lead_enrich.credit_notification')
- if already_notified == 'True':
- return
-
- iap_account = self.env['iap.account'].search([('service_name', '=', 'reveal')], limit=1)
- if not iap_account:
- return
-
- mail_template = self.env.ref('crm_iap_lead_enrich.mail_template_data_iap_lead_enrich_nocredit')
- if not mail_template:
- return
-
- mail_template.sudo().send_mail(iap_account.id, force_send=False, notif_layout='mail.mail_notification_light')
- self.env['ir.config_parameter'].sudo().set_param('lead_enrich.already_notified', True)
diff --git a/addons/crm_iap_lead_enrich/models/res_config_settings.py b/addons/crm_iap_lead_enrich/models/res_config_settings.py
new file mode 100644
index 00000000000..4f9d559dc26
--- /dev/null
+++ b/addons/crm_iap_lead_enrich/models/res_config_settings.py
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, models
+
+
+class ResConfigSettings(models.TransientModel):
+ _inherit = "res.config.settings"
+
+ @api.onchange('lead_enrich_auto')
+ def _onchange_cron_lead_enrich(self):
+ """ change the active status of the cron according to the settings"""
+ if self.module_crm_iap_lead_enrich == True:
+ cron = self.sudo().with_context(active_test=False).env.ref('crm_iap_lead_enrich.ir_cron_lead_enrichment')
+ if cron:
+ cron.active = self.lead_enrich_auto != 'manual'
diff --git a/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py b/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py
index cc5ae025e24..d8ddf38503a 100644
--- a/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py
+++ b/addons/crm_iap_lead_enrich/tests/test_lead_enrich.py
@@ -24,7 +24,7 @@ class TestLeadEnrich(crm_iap_lead_enrich_common.CrmCase, crm_iap_lead_enrich_com
leads[0].write({'partner_name': 'Already set', 'email_from': 'test@test1'})
leads.flush()
with self.mockIAPEnrichGateway(email_data={'test1': {'country_code': 'AU', 'state_code': 'NSW'}}):
- leads._iap_enrich()
+ leads.iap_enrich()
leads.flush()
self.assertEqual(leads[0].partner_name, 'Already set')
@@ -39,13 +39,13 @@ class TestLeadEnrich(crm_iap_lead_enrich_common.CrmCase, crm_iap_lead_enrich_com
# def test_enrich_error_credit(self):
# leads = self.env['crm.lead'].browse(self.leads.ids)
# with self.mockIAPEnrichGateway(sim_error='credit'):
- # leads._iap_enrich()
+ # leads.iap_enrich()
@users('sales_manager')
def test_enrich_error_jsonrpc_exception(self):
leads = self.env['crm.lead'].browse(self.leads.ids)
with self.mockIAPEnrichGateway(sim_error='jsonrpc_exception'):
- leads._iap_enrich()
+ leads.iap_enrich()
for lead in leads:
self.assertEqual(lead.street, False)
diff --git a/addons/crm_iap_lead_enrich/views/crm_lead_views.xml b/addons/crm_iap_lead_enrich/views/crm_lead_views.xml
new file mode 100644
index 00000000000..b65f80cc2ec
--- /dev/null
+++ b/addons/crm_iap_lead_enrich/views/crm_lead_views.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+ <record id="crm_lead_view_form" model="ir.ui.view">
+ <field name="name">crm.lead.view.form.inherit.iap.lead.enrich</field>
+ <field name="model">crm.lead</field>
+ <field name="inherit_id" ref="crm.crm_lead_view_form"/>
+ <field name="arch" type="xml">
+ <xpath expr="//button[@name='%(crm.action_crm_lead2opportunity_partner)d']" position="after">
+ <field name="show_enrich_button" invisible="1"/>
+ <button string="Enrich" name="iap_enrich" type="object" class="btn btn-secondary"
+ title="Enrich this lead with company data based on the email address"
+ attrs="{'invisible':['|',('show_enrich_button', '!=', True),('type','=','opportunity')]}"/>
+ <button string="Enrich" name="iap_enrich" type="object" class="btn btn-secondary"
+ title="Enrich this opportunity with company data based on the email address"
+ attrs="{'invisible':['|',('show_enrich_button', '!=', True),('type','=','lead')]}"/>
+ </xpath>
+ </field>
+ </record>
+</odoo>
+
diff --git a/addons/crm_iap_lead_enrich/views/res_config_settings_views.xml b/addons/crm_iap_lead_enrich/views/res_config_settings_views.xml
deleted file mode 100644
index c637db82e07..00000000000
--- a/addons/crm_iap_lead_enrich/views/res_config_settings_views.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<odoo>
- <record id="res_config_settings_view_form" model="ir.ui.view">
- <field name="name">res.config.settings.view.form.inherit.crm.iap.lead.enrich</field>
- <field name="model">res.config.settings</field>
- <field name="inherit_id" ref="crm.res_config_settings_view_form"/>
- <field name="arch" type="xml">
- <div id="crm_iap_lead_enrich" position="inside">
- <widget name="iap_buy_more_credits" service_name="reveal"/>
- </div>
- </field>
- </record>
-</odoo>