[IMP] mail_plugin: enable enrichment and update of an existing company
Add an enrich and update endpoint allowing the user to update an existing company if the company has no insights, as this can bring valuable information for the user especially if the company has no infos. Task-2563180 closes odoo/odoo#73658 X-original-commit: 72c35667d3e6f99cb4cfdbd1c93b877cc9fef750 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
@@ -53,6 +53,74 @@ class MailPluginController(http.Controller):
|
||||
|
||||
return response
|
||||
|
||||
@http.route('/mail_plugin/partner/enrich_and_update_company', type='json', auth='outlook', cors='*')
|
||||
def res_partner_enrich_and_update_company(self, partner_id):
|
||||
"""
|
||||
Enriches an existing company using IAP
|
||||
"""
|
||||
partner = request.env['res.partner'].browse(partner_id)
|
||||
if not partner.is_company:
|
||||
return {'error': 'Contact must be a company'}
|
||||
|
||||
normalized_email = partner.email_normalized
|
||||
if not normalized_email:
|
||||
return {'error': 'Contact has no valid email'}
|
||||
|
||||
domain = tools.email_domain_extract(normalized_email)
|
||||
iap_data = self._iap_enrich(domain)
|
||||
|
||||
if 'enrichment_info' in iap_data: # means that an issue happened with the enrichment request
|
||||
return {
|
||||
'enrichment_info': iap_data['enrichment_info'],
|
||||
'company': self._get_company_data(partner),
|
||||
}
|
||||
|
||||
phone_numbers = iap_data.get('phone_numbers')
|
||||
|
||||
partner_values = {}
|
||||
|
||||
if not partner.phone and phone_numbers:
|
||||
partner_values.update({'phone': phone_numbers[0]})
|
||||
|
||||
if not partner.iap_enrich_info:
|
||||
partner_values.update({'iap_enrich_info': json.dumps(iap_data)})
|
||||
|
||||
if not partner.image_128:
|
||||
logo_url = iap_data.get('logo')
|
||||
if logo_url:
|
||||
try:
|
||||
response = requests.get(logo_url, timeout=2)
|
||||
if response.ok:
|
||||
partner_values.update({'image_1920': base64.b64encode(response.content)})
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
model_fields_to_iap_mapping = {
|
||||
'street': 'street_name',
|
||||
'city': 'city',
|
||||
'zip': 'postal_code',
|
||||
'website': 'domain',
|
||||
}
|
||||
|
||||
# only update keys for which we dont have values yet
|
||||
partner_values.update({
|
||||
model_field: iap_data.get(iap_key)
|
||||
for model_field, iap_key in model_fields_to_iap_mapping.items() if not partner[model_field]
|
||||
})
|
||||
|
||||
partner.write(partner_values)
|
||||
|
||||
partner.message_post_with_view(
|
||||
'iap_mail.enrich_company',
|
||||
values=iap_data,
|
||||
subtype_id=request.env.ref('mail.mt_note').id,
|
||||
)
|
||||
|
||||
return {
|
||||
'enrichment_info': {'type': 'company_updated'},
|
||||
'company': self._get_company_data(partner),
|
||||
}
|
||||
|
||||
@http.route(['/mail_client_extension/partner/get', '/mail_plugin/partner/get']
|
||||
, type="json", auth="outlook", cors="*")
|
||||
def res_partner_get(self, email=None, name=None, partner_id=None, **kwargs):
|
||||
@@ -181,6 +249,10 @@ class MailPluginController(http.Controller):
|
||||
return self._prepare_translations()
|
||||
|
||||
def _iap_enrich(self, domain):
|
||||
"""
|
||||
Returns enrichment data for a given domain, in case an error happens the response will
|
||||
contain an enrichment_info key explaining what went wrong
|
||||
"""
|
||||
enriched_data = {}
|
||||
try:
|
||||
response = request.env['iap.enrich.api']._request_enrich({domain: domain}) # The key doesn't matter
|
||||
|
||||
Reference in New Issue
Block a user