Skip to content

Commit

Permalink
Merge PR OCA#127 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Jun 7, 2024
2 parents a7e12a2 + a3cd5a8 commit da25ff6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
25 changes: 22 additions & 3 deletions resource_booking/models/calendar_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,32 @@ 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)

@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.
mail_notify_author key from context is necessary to force the notification
to be sent to author.
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).
"""
vals_list2 = []
records = self.env["calendar.event"]
Expand All @@ -68,7 +87,7 @@ def create(self, vals_list):
CalendarEvent,
self.with_context(
resource_booking_ids=vals["resource_booking_ids"],
mail_notify_author=True,
resource_booking_event=True,
),
).create(vals)
else:
Expand Down
52 changes: 50 additions & 2 deletions resource_booking/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
_2dt = fields.Datetime.to_datetime


@freeze_time("2021-02-26 09:00:00", tick=True) # Last Friday of February
class BackendCase(TransactionCase):
class BackendCaseBase(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
create_test_data(cls)
cls.plain_user = new_test_user(cls.env, login="plain", groups="base.group_user")


@freeze_time("2021-02-26 09:00:00", tick=True) # Last Friday of February
class BackendCaseMisc(BackendCaseBase):
@users("plain")
def test_plain_user_calendar_event(self):
"""Check that a simple user is able to handle manual calendar events."""
Expand Down Expand Up @@ -1042,3 +1044,49 @@ def test_resource_booking_schedule_unschedule(self):
self.assertNotEqual(
self.mail_activity.date_deadline, fields.Date.from_string("2024-01-01")
)


@freeze_time("2021-02-26 09:00:00", tick=True) # Last Friday of February
class BackendCaseCustom(BackendCaseBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=False))
cls.mt_note = cls.env.ref("mail.mt_note")
cls.mt_note.default = True
cls.partner.email = [email protected]"

def test_resource_booking_message(self):
rb_model = self.env["resource.booking"]
rb = rb_model.create(
{
"partner_ids": [(4, self.partner.id)],
"type_id": self.rbt.id,
"combination_auto_assign": False,
"combination_id": self.rbcs[0].id,
"user_id": self.users[0].id,
}
)
# Simulate the same as portal_booking_confirm
booking_sudo = rb_model.sudo().browse(rb.id)
booking_sudo = booking_sudo.with_context(
using_portal=True, tz=booking_sudo.type_id.resource_calendar_id.tz
)
with Form(booking_sudo) as booking_form:
booking_form.start = datetime(2021, 3, 1, 10)
booking_sudo.action_confirm()
meeting = rb.meeting_id
follower = meeting.message_follower_ids.filtered(
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"
)
self.assertEqual(len(meesages), 2)
partner_message = meesages.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(
lambda x: meeting.user_id.partner_id in x.partner_ids
)
self.assertIn(meeting.user_id.partner_id, user_message.notified_partner_ids)

0 comments on commit da25ff6

Please sign in to comment.