diff --git a/addons/event/models/event_registration.py b/addons/event/models/event_registration.py
index c94040fbc61..d1ee3e7a368 100644
--- a/addons/event/models/event_registration.py
+++ b/addons/event/models/event_registration.py
@@ -152,25 +152,9 @@ class EventRegistration(models.Model):
return ret
def name_get(self):
- """ Custom name_get implementation to better differentiate registrations
- linked to a given partner but with different name (one partner buying
- several registrations)
-
- * name, partner_id has no name -> take name
- * partner_id has name, name void or same -> take partner name
- * both have name: partner + name
+ """ Custom name_get in case a registration is nott linked to an attendee
"""
- ret_list = []
- for registration in self:
- if registration.partner_id.name:
- if registration.name and registration.name != registration.partner_id.name:
- name = '%s, %s' % (registration.partner_id.name, registration.name)
- else:
- name = registration.partner_id.name
- else:
- name = registration.name
- ret_list.append((registration.id, name))
- return ret_list
+ return [(registration.id, registration.name or f"#{registration.id}") for registration in self]
def toggle_active(self):
pre_inactive = self - self.filtered(self._active_name)
@@ -252,6 +236,19 @@ class EventRegistration(models.Model):
# MAILING / GATEWAY
# ------------------------------------------------------------
+ def _message_compute_subject(self):
+ if self.name:
+ return _(
+ "%(event_name)s - Registration for %(attendee_name)s",
+ event_name=self.event_id.name,
+ attendee_name=self.name,
+ )
+ return _(
+ "%(event_name)s - Registration #%(registration_id)s",
+ event_name=self.event_id.name,
+ registration_id=self.id,
+ )
+
def _message_get_suggested_recipients(self):
recipients = super(EventRegistration, self)._message_get_suggested_recipients()
public_users = self.env['res.users'].sudo()
diff --git a/addons/im_livechat/tests/test_message.py b/addons/im_livechat/tests/test_message.py
index 7ef9dde9c5a..3957660a45c 100644
--- a/addons/im_livechat/tests/test_message.py
+++ b/addons/im_livechat/tests/test_message.py
@@ -54,6 +54,7 @@ class TestImLivechatMessage(TransactionCase):
'guestAuthor': [('clear',)],
'history_partner_ids': [],
'id': message.id,
+ 'default_subject': channel_livechat_1.name,
'is_discussion': False,
'is_note': True,
'is_notification': False,
diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py
index 1ca454b6897..8f28628dc9c 100644
--- a/addons/mail/models/mail_message.py
+++ b/addons/mail/models/mail_message.py
@@ -846,13 +846,12 @@ class Message(models.Model):
'name': message_sudo.author_guest_id.name,
} if message_sudo.author_guest_id else [('clear',)]
if message_sudo.model and message_sudo.res_id:
- record_name = self.env[message_sudo.model] \
- .browse(message_sudo.res_id) \
- .sudo() \
- .with_prefetch(thread_ids_by_model_name[message_sudo.model]) \
- .display_name
+ record_sudo = self.env[message_sudo.model].browse(message_sudo.res_id).sudo()
+ record_name = record_sudo.with_prefetch(thread_ids_by_model_name[message_sudo.model]).display_name
+ default_subject = record_sudo._message_compute_subject()
else:
record_name = False
+ default_subject = False
reactions_per_content = defaultdict(lambda: self.env['mail.message.reaction'])
for reaction in message_sudo.reaction_ids:
reactions_per_content[reaction.content] |= reaction
@@ -868,6 +867,7 @@ class Message(models.Model):
allowed_tracking_ids = message_sudo.tracking_value_ids.filtered(lambda tracking: not tracking.field_groups or self.env.is_superuser() or self.user_has_groups(tracking.field_groups))
vals.update({
'author': author,
+ 'default_subject': default_subject,
'guestAuthor': guestAuthor,
'notifications': message_sudo.notification_ids._filtered_for_web_client()._notification_format(),
'attachment_ids': message_sudo.attachment_ids._attachment_format() if not legacy else message_sudo.attachment_ids._attachment_format(legacy=True),
diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py
index 7b6e412e194..763889896dd 100644
--- a/addons/mail/models/mail_thread.py
+++ b/addons/mail/models/mail_thread.py
@@ -2320,6 +2320,14 @@ class MailThread(models.AbstractModel):
parent_id = current_ancestor.parent_id.id if current_ancestor.parent_id else parent_id
return parent_id
+ def _message_compute_subject(self):
+ """Overridable method to get the default subject for a message related to this record.
+
+ :return str:
+ """
+ self.ensure_one()
+ return self.name_get()[0][1]
+
def _message_create(self, values_list):
if not isinstance(values_list, (list)):
values_list = [values_list]
@@ -2739,7 +2747,16 @@ class MailThread(models.AbstractModel):
:param dict base_mail_values: base mail.mail values, holding message
to notify (mail_message_id and its fields), server, references, subject.
"""
- mail_subject = message.subject or (message.record_name and 'Re: %s' % message.record_name) # in cache, no queries
+ mail_subject = message.subject
+ if not message.subject:
+ thread = self
+ if not thread and message.model:
+ thread = self.env[message.model].browse(message.res_id)
+ if not thread:
+ mail_subject = message.record_name
+ else:
+ mail_subject = thread._message_compute_subject()
+
# Replace new lines by spaces to conform to email headers requirements
mail_subject = ' '.join((mail_subject or '').splitlines())
# compute references: set references to the parent and add current message just to
diff --git a/addons/mail/static/src/components/message/message.xml b/addons/mail/static/src/components/message/message.xml
index b31b587da8d..ed5c98dfcfd 100644
--- a/addons/mail/static/src/components/message/message.xml
+++ b/addons/mail/static/src/components/message/message.xml
@@ -119,7 +119,7 @@
'border-warning bg-warning-light opacity-50': message.isHighlighted and (message.isDiscussionOrNotification or message.message_type === 'sms'),
}"
/>
- Subject:
Test Body
', + })._action_send_mail() + + self.assertEqual(message.subject, simple_record.name) + # custom subject + ctx = self._get_web_context(ticket_record, add_web=False, composition_mode='comment') + _, messages = self.env['mail.compose.message'].with_context(ctx).create({ + 'body': 'Test Body
', + })._action_send_mail() + self.assertEqual(messages.subject, ticket_record._message_compute_subject()) + @users('employee') @mute_logger('odoo.models.unlink') def test_mail_composer_recipients(self): @@ -736,7 +753,10 @@ class TestComposerInternals(TestMailComposer): def test_mail_composer_parent(self): """ Test specific management in comment mode when having parent_id set: record_name, subject, parent's partners. """ - parent = self.test_record.message_post(body='Test', partner_ids=(self.partner_1 + self.partner_2).ids) + subject = "Parent Subject" + parent = self.test_record.message_post(subject=subject, + body='Test', + partner_ids=(self.partner_1 + self.partner_2).ids) composer = self.env['mail.compose.message'].with_context( self._get_web_context(self.test_record, add_web=False, default_parent_id=parent.id) @@ -749,7 +769,7 @@ class TestComposerInternals(TestMailComposer): self.assertEqual(composer.parent_id, parent) self.assertEqual(composer.partner_ids, self.partner_1 + self.partner_2) self.assertEqual(composer.record_name, self.test_record.name) - self.assertEqual(composer.subject, 'Re: %s' % self.test_record.name) + self.assertEqual(composer.subject, subject) @users('user_rendering_restricted') @mute_logger('odoo.tests', 'odoo.addons.base.models.ir_rule', 'odoo.addons.mail.models.mail_mail', 'odoo.models.unlink') @@ -970,7 +990,7 @@ class TestComposerResultsComment(TestMailComposer): message = self.test_record.message_ids[0] self.assertEqual(message.author_id, self.user_employee.partner_id) self.assertEqual(message.body, 'Test Body
') - self.assertEqual(message.subject, 'Re: %s' % self.test_record.name) + self.assertEqual(message.subject, self.test_record._message_compute_subject()) self.assertEqual(message.subtype_id, self.env.ref('mail.mt_comment')) self.assertEqual(message.partner_ids, self.partner_1 | self.partner_2) diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index 49472beec66..eaf18c15b14 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -325,6 +325,20 @@ class TestMessageNotify(TestMessagePostCommon): subject='Notify Subject', ) + @users('employee') + def test_notify_default_subject(self): + """ Test notify in batch. Currently not supported. """ + test_partner = self.env['res.partner'].sudo().create([{'name': 'John', 'email': 'john@test.lan'}]) + test_record = self.env['mail.test.ticket'].create({'name': 'Test'}) + mt_comment = self.env['ir.model.data']._xmlid_to_res_id('mail.mt_comment') + test_record.message_subscribe([test_partner.id], subtype_ids=[mt_comment]) + + with self.mock_mail_gateway(), self.mock_mail_app(): + test_record.message_post(body="Test Message", message_type="comment", subtype_id=mt_comment) + + self.assertEqual(len(self._new_msgs), 1) + self.assertEqual(self._new_msgs.subject, test_record._message_compute_subject()) + @users('employee') @mute_logger('odoo.addons.mail.models.mail_mail') def test_notify_from_user_id(self): @@ -911,7 +925,7 @@ class TestMessagePost(TestMessagePostCommon, CronMixinCase): [self.partner_2], body_content='Test Answer Bis
', reply_to=msg.reply_to, - subject='Re: %s' % self.test_record.name, + subject=self.test_record.name, references_content='openerp-%d-mail.test.simple' % self.test_record.id, references='%s %s' % (parent_msg.message_id, new_msg.message_id), )