[IMP] mail_tracking: Cc name & check if mail is Cc

This commit is contained in:
Alexandre Díaz 2019-07-23 17:50:45 +02:00 committed by Jasmin Solanki
parent 61463fc530
commit 7ef6c69205
3 changed files with 55 additions and 94 deletions

View File

@ -59,14 +59,21 @@ class MailMessage(models.Model):
trackings = self.env['mail.tracking.email'].sudo().search([ trackings = self.env['mail.tracking.email'].sudo().search([
('mail_message_id', '=', message.id), ('mail_message_id', '=', message.id),
]) ])
# Get Cc recipients
email_cc_list = email_split(message.email_cc)
if any(email_cc_list):
partners |= partners.search([('email', 'in', email_cc_list)])
email_cc_list = set(email_cc_list)
# Search all trackings for this message # Search all trackings for this message
for tracking in trackings: for tracking in trackings:
status = self._partner_tracking_status_get(tracking) status = self._partner_tracking_status_get(tracking)
recipient = ( recipient = (
tracking.partner_id.name or tracking.recipient) tracking.partner_id.name or tracking.recipient)
partner_trackings.append(( partner_trackings.append((
status, tracking.id, recipient, tracking.partner_id.id)) status, tracking.id, recipient, tracking.partner_id.id,
False))
if tracking.partner_id: if tracking.partner_id:
email_cc_list.discard(tracking.partner_id.email)
partners_already |= tracking.partner_id partners_already |= tracking.partner_id
# Search all recipients for this message # Search all recipients for this message
if message.partner_ids: if message.partner_ids:
@ -77,47 +84,18 @@ class MailMessage(models.Model):
partners -= partners_already partners -= partners_already
for partner in partners: for partner in partners:
# If there is partners not included, then status is 'unknown' # If there is partners not included, then status is 'unknown'
partner_trackings.append(( # Because can be an Cc recipinet
'unknown', False, partner.name, partner.id, partner.email)) isCc = False
res[message.id] = partner_trackings if partner.email in email_cc_list:
return res
@api.multi
def _get_email_cc(self):
"""This method gets all Cc mails and the associated partner if exist.
The result is a dictionary by 'message id' with a list of tuples
(str:email_cc, list:[partner id, partner display_name] or False)
"""
res = {}
ResPartnerObj = self.env['res.partner']
for message in self:
email_cc_list = email_split(message.email_cc)
email_cc_list_checked = []
if any(email_cc_list):
partners = ResPartnerObj.search([
('email', 'in', email_cc_list)
])
email_cc_list = set(email_cc_list)
for partner in partners:
email_cc_list.discard(partner.email) email_cc_list.discard(partner.email)
email_cc_list_checked.append( isCc = True
(partner.email, [partner.id, partner.display_name])) partner_trackings.append((
'unknown', False, partner.name, partner.id, isCc))
for email in email_cc_list: for email in email_cc_list:
email_cc_list_checked.append((email, False)) # If there is Cc without partner
res.update({ partner_trackings.append((
message.id: email_cc_list_checked 'unknown', False, email, False, True))
}) res[message.id] = partner_trackings
return res
@api.multi
def _get_failed_message(self):
res = {}
for message in self:
res.update({
message.id: message.mail_tracking_needs_action
and bool(message.mail_tracking_ids.filtered(
lambda x: x.state in self.get_failed_states()))
})
return res return res
@api.model @api.model
@ -127,16 +105,16 @@ class MailMessage(models.Model):
mail_message_ids = {m.get('id') for m in messages if m.get('id')} mail_message_ids = {m.get('id') for m in messages if m.get('id')}
mail_messages = self.browse(mail_message_ids) mail_messages = self.browse(mail_message_ids)
partner_trackings = mail_messages.tracking_status() partner_trackings = mail_messages.tracking_status()
email_cc = mail_messages._get_email_cc()
failed_message = mail_messages._get_failed_message() failed_message = mail_messages._get_failed_message()
for message_dict in messages: for message_dict in messages:
mail_message_id = message_dict.get('id', False) mail_message_id = message_dict.get('id', False)
if mail_message_id: if mail_message_id:
message_dict.update({ message_dict.update({
'partner_trackings': partner_trackings[mail_message_id], 'partner_trackings': partner_trackings[mail_message_id],
'email_cc': email_cc[mail_message_id],
'failed_message': failed_message[mail_message_id], 'failed_message': failed_message[mail_message_id],
}) })
message_dict['partner_trackings'] = \
partner_trackings[mail_message_id]
return res return res
@api.model @api.model
@ -200,5 +178,4 @@ class MailMessage(models.Model):
user_signature=user_signature) user_signature=user_signature)
if hide_followers: if hide_followers:
self_sudo.subtype_id = orig_subtype_id self_sudo.subtype_id = orig_subtype_id
return res return res

View File

@ -5,39 +5,39 @@
<template> <template>
<t t-name="mail.tracking.status"> <t t-name="mail.tracking.status">
<t t-if="isCc"> <t t-if="tracking[4]">
<span class="mail_tracking_cc"> <span class="mail_tracking_cc">
<i class="fa fa-cc"></i> <i class="fa fa-cc"></i>
</span> </span>
</t> </t>
<t t-elif="tracking[0] == 'unknown'"> <t t-elif="tracking[0] === 'unknown'">
<span class="mail_tracking_unknown"> <span class="mail_tracking_unknown">
<i class="fa fa-ban"></i> <i class="fa fa-ban"></i>
</span> </span>
</t> </t>
<t t-elif="tracking[0] == 'waiting'"> <t t-elif="tracking[0] === 'waiting'">
<span class="mail_tracking_waiting mail_tracking_pointer"> <span class="mail_tracking_waiting mail_tracking_pointer">
<i class="fa fa-clock-o"></i> <i class="fa fa-clock-o"></i>
</span> </span>
</t> </t>
<t t-elif="tracking[0] == 'error'"> <t t-elif="tracking[0] === 'error'">
<span class="mail_tracking_error mail_tracking_pointer"> <span class="mail_tracking_error mail_tracking_pointer">
<i class="fa fa-remove"></i> <i class="fa fa-remove"></i>
</span> </span>
</t> </t>
<t t-elif="tracking[0] == 'sent'"> <t t-elif="tracking[0] === 'sent'">
<span class="mail_tracking_sent mail_tracking_pointer"> <span class="mail_tracking_sent mail_tracking_pointer">
<i class="fa fa-check"></i> <i class="fa fa-check"></i>
</span> </span>
</t> </t>
<t t-elif="tracking[0] == 'delivered'"> <t t-elif="tracking[0] === 'delivered'">
<span class="fa-stack mail_tracking_delivered mail_tracking_pointer"> <span class="fa-stack mail_tracking_delivered mail_tracking_pointer">
<i class="fa fa-check fa-stack-1x" style="margin-left:1px"></i> <i class="fa fa-check fa-stack-1x" style="margin-left:1px"></i>
<i class="fa fa-check fa-inverse fa-stack-1x" style="margin-left:-2px;"></i> <i class="fa fa-check fa-inverse fa-stack-1x" style="margin-left:-2px;"></i>
<i class="fa fa-check fa-stack-1x" style="margin-left:-3px"></i> <i class="fa fa-check fa-stack-1x" style="margin-left:-3px"></i>
</span> </span>
</t> </t>
<t t-elif="tracking[0] == 'opened'"> <t t-elif="tracking[0] === 'opened'">
<span class="fa-stack mail_tracking_opened mail_tracking_pointer"> <span class="fa-stack mail_tracking_opened mail_tracking_pointer">
<i class="fa fa-check fa-stack-1x" style="margin-left:1px"></i> <i class="fa fa-check fa-stack-1x" style="margin-left:1px"></i>
<i class="fa fa-check fa-inverse fa-stack-1x" style="margin-left:-2px;"></i> <i class="fa fa-check fa-inverse fa-stack-1x" style="margin-left:-2px;"></i>
@ -51,52 +51,25 @@
<t t-if="message.hasPartnerTrackings() || message.hasEmailCc()" > <t t-if="message.hasPartnerTrackings() || message.hasEmailCc()" >
<p class="o_mail_tracking"> <p class="o_mail_tracking">
<strong>To:</strong> <strong>To:</strong>
<t t-set="first_tracking" t-value="true"/> <t t-foreach="message.partner_trackings" t-as="tracking">
<t t-foreach="message.getPartnerTrackings()" t-as="tracking"> <t t-if="!tracking_first">
<t t-set="isCc" t-value="message.isEmailCc(tracking[4])" />
<t t-if="!first_tracking">
- -
</t> </t>
<t t-if="tracking[3]"> <t t-if="tracking[3]">
<a t-attf-class="o_mail_action_tracking_partner #{isCc ? 'o_mail_cc' : ''}" <a t-attf-class="o_mail_action_tracking_partner #{tracking[4] ? 'o_mail_cc' : ''}"
t-att-data-partner="tracking[3]" t-att-data-partner="tracking[3]"
t-attf-href="#model=res.partner&amp;id=#{tracking[3]}"> t-attf-href="#model=res.partner&amp;id=#{tracking[3]}">
<t t-esc="tracking[2]"/> <t t-esc="tracking[2]"/>
</a> </a>
</t> </t>
<t t-if="!tracking[3]"> <t t-if="!tracking[3]">
<span><t t-esc="tracking[2]"/></span> <span t-attf-class="#{tracking[4] ? 'o_mail_cc' : ''}"><t t-esc="tracking[2]"/></span>
</t> </t>
<span class="mail_tracking o_mail_action_tracking_status" <span class="mail_tracking o_mail_action_tracking_status"
t-att-data-tracking="tracking[1]" t-att-data-tracking="tracking[1]"
t-attf-title="Status: #{tracking[0]}"> t-attf-title="Status: #{tracking[0]}">
<t t-call="mail.tracking.status"/> <t t-call="mail.tracking.status"/>
</span> </span>
<t t-set="first_tracking" t-value="false"/>
</t>
<t t-foreach="message.getEmailCc()" t-as="cc">
<t t-set="needPrint" t-value="true" />
<t t-foreach="message.getPartnerTrackings()" t-as="tracking">
<t t-if="cc[0] == tracking[4]" t-set="needPrint" t-value="false" />
</t>
<t t-if="needPrint">
<t t-set="isCc" t-value="true" />
<t t-if="cc[1]">
<a t-attf-class="o_mail_action_tracking_partner o_mail_cc"
t-att-data-partner="cc[1][0]"
t-attf-href="#model=res.partner&amp;id=#{cc[1][0]}">
<t t-esc="cc[1][1]"/>
</a>
</t>
<t t-else="">
<span class="o_mail_cc"><t t-esc="cc[0]" /></span>
</t>
<span class="mail_tracking"
title="Status: unknown">
<t t-call="mail.tracking.status"/>
</span>
</t>
</t> </t>
</p> </p>
</t> </t>

View File

@ -115,6 +115,25 @@ class TestMailTracking(TransactionCase):
tracking_email.event_create('open', metadata) tracking_email.event_create('open', metadata)
self.assertEqual(tracking_email.state, 'opened') self.assertEqual(tracking_email.state, 'opened')
def _check_partner_trackings(self, message):
message_dict = message.message_format()[0]
self.assertEqual(len(message_dict['partner_trackings']), 3)
# mail cc
foundPartner = False
foundNoPartner = False
for tracking in message_dict['partner_trackings']:
if tracking[3] == self.sender.id:
foundPartner = True
self.assertTrue(tracking[4])
elif tracking[2] == 'unnamed@test.com':
foundNoPartner = True
self.assertFalse(tracking[3])
self.assertTrue(tracking[4])
elif tracking[3] == self.recipient.id:
self.assertFalse(tracking[4])
self.assertTrue(foundPartner)
self.assertTrue(foundNoPartner)
def test_email_cc(self): def test_email_cc(self):
message = self.env['mail.message'].create({ message = self.env['mail.message'].create({
'subject': 'Message test', 'subject': 'Message test',
@ -127,17 +146,7 @@ class TestMailTracking(TransactionCase):
'email_cc': 'unnamed@test.com, sender@example.com', 'email_cc': 'unnamed@test.com, sender@example.com',
'body': '<p>This is a test message</p>', 'body': '<p>This is a test message</p>',
}) })
self._check_partner_trackings(message)
message_dict = message.message_format()[0]
self.assertEqual(len(message_dict['email_cc']), 2)
# mail cc
# 'mail.message' First check Cc with res.partner
email_cc = message_dict['email_cc'][0]
self.assertEqual(email_cc[0], 'sender@example.com')
self.assertTrue(email_cc[1])
email_cc = message_dict['email_cc'][1]
self.assertEqual(email_cc[0], 'unnamed@test.com')
self.assertFalse(email_cc[1])
# suggested recipients # suggested recipients
recipients = self.recipient.message_get_suggested_recipients() recipients = self.recipient.message_get_suggested_recipients()
suggested_mails = { suggested_mails = {
@ -154,11 +163,13 @@ class TestMailTracking(TransactionCase):
'model': 'res.partner', 'model': 'res.partner',
'res_id': self.recipient.id, 'res_id': self.recipient.id,
'partner_ids': [(4, self.recipient.id)], 'partner_ids': [(4, self.recipient.id)],
'email_cc': 'unnamed@test.com, sender@example.com', 'email_cc': 'unnamed@test.com, sender@example.com'
', recipient@example.com',
'body': '<p>This is another test message</p>', 'body': '<p>This is another test message</p>',
}) })
recipients = self.recipient.message_get_suggested_recipients() recipients = self.recipient.message_get_suggested_recipients()
self.assertEqual(len(recipients[self.recipient.id][0]), 3) self.assertEqual(len(recipients[self.recipient.id][0]), 3)
self._check_partner_trackings(message)
def test_failed_message(self): def test_failed_message(self):
# Create message # Create message