Skip to content

Commit

Permalink
[IMP] resource_booking: Reduce and simplify code according to odoo/odoo@
Browse files Browse the repository at this point in the history
d7bbcef

The changes added at 851dc9a and a3cd5a8 are unnecessary according to the approach added at odoo/odoo@d7bbcef.
We maintain the tests to confirm that the expected behavior is maintained.
  • Loading branch information
victoralmau committed Sep 19, 2024
1 parent 2b8506a commit 2cef0d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 52 deletions.
44 changes: 3 additions & 41 deletions resource_booking/models/calendar_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,51 +52,13 @@ def write(self, vals):
rescheduled._validate_booking_modifications()
return result

def _notify_thread(self, message, msg_vals=False, **kwargs):
"""If we are creating the calendar event from the resource booking
(detected from the resource_booking_event context key), we need to
inject the standard mail context `mail_notify_author` to super to get
the own author notified when someone books a reservation, but only
in the case that the mail is being sent to them, as if not the author
may receive one copy per each of the attendees. This happens only when
the the subtype not is enabled by default in the instance.
"""
if self.env.context.get("resource_booking_event") and msg_vals.get(
"author_id"
) in msg_vals.get("partner_ids", []):
self = self.with_context(mail_notify_author=True)
return super()._notify_thread(message=message, msg_vals=msg_vals, **kwargs)

def _notify_get_recipients(self, message, msg_vals, **kwargs):
"""If we are creating the calendar event from resource booking, we want to
notify only the partner_ids and not all the followers (to avoid that each email
is sent to all followers). Example: Resource booking with combination of several
users. This only happens when the subtype note is enabled by default in the
instance.
"""
res = super()._notify_get_recipients(
message=message, msg_vals=msg_vals, **kwargs
)
if self.env.context.get("resource_booking_event"):
res2 = []
partner_ids = msg_vals.get("partner_ids", [])
for item in res:
if item["id"] in partner_ids:
res2.append(item)
return res2
return res

@api.model_create_multi
def create(self, vals_list):
"""Transfer resource booking to _attendees_values by context.
We need to serialize the creation in that case.
resource_booking_event custom key from context is necessary.
We cannot use mail_notify_author key in the context because if the mail_note
subtype is set by default, the email of each attendee would be sent also to
the author (example: a meeting with 2 attendees would send 2 emails but
each of them would be sent to the partner of the attendee + author of
the email).
mail_notify_author key from context is necessary to force the notification
to be sent to author.
"""
vals_list2 = []
records = self.env["calendar.event"]
Expand All @@ -106,7 +68,7 @@ def create(self, vals_list):
CalendarEvent,
self.with_context(
resource_booking_ids=vals["resource_booking_ids"],
resource_booking_event=True,
mail_notify_author=True,
),
).create(vals)
else:
Expand Down
30 changes: 19 additions & 11 deletions resource_booking/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,13 +1080,17 @@ def test_resource_booking_message_01(self):
lambda x: x.partner_id == meeting.user_id.partner_id
)
self.assertIn(self.mt_note, follower.subtype_ids)
meesages = meeting.message_ids.filtered(
lambda x: x.message_type != "notification"
messages = self.env["mail.message"].search(
[
("model", "=", meeting._name),
("res_id", "=", meeting.id),
("message_type", "=", "user_notification"),
]
)
self.assertEqual(len(meesages), 2)
partner_message = meesages.filtered(lambda x: self.partner in x.partner_ids)
self.assertEqual(len(messages), 2)
partner_message = messages.filtered(lambda x: self.partner in x.partner_ids)
self.assertNotIn(rb.user_id.partner_id, partner_message.notified_partner_ids)
user_message = meesages.filtered(
user_message = messages.filtered(
lambda x: meeting.user_id.partner_id in x.partner_ids
)
self.assertIn(meeting.user_id.partner_id, user_message.notified_partner_ids)
Expand Down Expand Up @@ -1122,14 +1126,18 @@ def test_resource_booking_message_02(self):
lambda x: x.partner_id == meeting.user_id.partner_id
)
self.assertIn(self.mt_note, follower.subtype_ids)
meesages = meeting.message_ids.filtered(
lambda x: x.message_type != "notification"
messages = self.env["mail.message"].search(
[
("model", "=", meeting._name),
("res_id", "=", meeting.id),
("message_type", "=", "user_notification"),
]
)
self.assertEqual(len(meesages), 3)
partner_message = meesages.filtered(lambda x: self.partner in x.partner_ids)
self.assertEqual(len(messages), 3)
partner_message = messages.filtered(lambda x: self.partner in x.partner_ids)
self.assertNotIn(user_0.partner_id, partner_message.notified_partner_ids)
self.assertNotIn(user_1.partner_id, partner_message.notified_partner_ids)
user_0_message = meesages.filtered(lambda x: user_0.partner_id in x.partner_ids)
user_0_message = messages.filtered(lambda x: user_0.partner_id in x.partner_ids)
self.assertIn(user_0.partner_id, user_0_message.notified_partner_ids)
user_1_message = meesages.filtered(lambda x: user_1.partner_id in x.partner_ids)
user_1_message = messages.filtered(lambda x: user_1.partner_id in x.partner_ids)
self.assertIn(user_1.partner_id, user_1_message.notified_partner_ids)

0 comments on commit 2cef0d9

Please sign in to comment.