From 89a1de5232d112d2cdf6e85f1369900e66e95bf0 Mon Sep 17 00:00:00 2001 From: Mahamadasif Ansari Date: Wed, 27 Jul 2022 10:22:38 +0000 Subject: [PATCH] [IMP] calendar: do not switch assignation on activities Currently, if we create an activity for user other than logged in one and create a calendar event from the activity, it automatically changes the attendee on the event and re-assigns the activity to the logged in user instead of the one originally assigned to the activity during creation. It happens because default attendees are not passed while creating the event from activity. ALso, when editing the calendar event, the sync mechanism changes the activity user to the organizer of the event, which by default is logged in user. This commit improves the behavior by passing the appropriate default values so that the calendar attendees and organizer matches with the user to whom the activity is assigned initially. task-2920631 closes odoo/odoo#96943 Signed-off-by: Thibault Delavallee (tde) --- addons/calendar/models/calendar_event.py | 5 ++--- addons/calendar/models/mail_activity.py | 2 ++ addons/calendar/tests/test_calendar.py | 26 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/addons/calendar/models/calendar_event.py b/addons/calendar/models/calendar_event.py index 80988bb28d4..2ae5589c546 100644 --- a/addons/calendar/models/calendar_event.py +++ b/addons/calendar/models/calendar_event.py @@ -407,13 +407,12 @@ class Meeting(models.Model): def create(self, vals_list): # Prevent sending update notification when _inverse_dates is called self = self.with_context(is_calendar_event_new=True) + defaults = self.default_get(['activity_ids', 'res_model_id', 'res_id', 'user_id', 'res_model', 'partner_ids']) vals_list = [ # Else bug with quick_create when we are filter on an other user - dict(vals, user_id=self.env.user.id) if not 'user_id' in vals else vals + dict(vals, user_id=defaults.get('user_id', self.env.user.id)) if not 'user_id' in vals else vals for vals in vals_list ] - - defaults = self.default_get(['activity_ids', 'res_model_id', 'res_id', 'user_id', 'res_model', 'partner_ids']) meeting_activity_type = self.env['mail.activity.type'].search([('category', '=', 'meeting')], limit=1) # get list of models ids and filter out None values directly model_ids = list(filter(None, {values.get('res_model_id', defaults.get('res_model_id')) for values in vals_list})) diff --git a/addons/calendar/models/mail_activity.py b/addons/calendar/models/mail_activity.py index cd921cda669..d3dff3f9f67 100644 --- a/addons/calendar/models/mail_activity.py +++ b/addons/calendar/models/mail_activity.py @@ -20,6 +20,8 @@ class MailActivity(models.Model): 'default_name': self.summary or self.res_name, 'default_description': self.note if not is_html_empty(self.note) else '', 'default_activity_ids': [(6, 0, self.ids)], + 'default_partner_ids': self.user_id.partner_id.ids, + 'default_user_id': self.user_id.id, } return action diff --git a/addons/calendar/tests/test_calendar.py b/addons/calendar/tests/test_calendar.py index 8dfc6bfb89d..f3d81a42306 100644 --- a/addons/calendar/tests/test_calendar.py +++ b/addons/calendar/tests/test_calendar.py @@ -151,6 +151,32 @@ class TestCalendar(SavepointCaseWithUserDemo): self.assertEqual(test_event.res_id, test_record.id) self.assertEqual(len(test_record.activity_ids), 1) + def test_event_activity_user_sync(self): + # ensure phonecall activity type exists + activty_type = self.env['mail.activity.type'].create({ + 'name': 'Call', + 'category': 'phonecall' + }) + activity = self.env['mail.activity'].create({ + 'summary': 'Call with Demo', + 'activity_type_id': activty_type.id, + 'note': 'Schedule call with Admin', + 'res_model_id': self.env['ir.model']._get_id('res.partner'), + 'res_id': self.env['res.partner'].create({'name': 'Test Partner'}).id, + 'user_id': self.user_demo.id, + }) + action_context = activity.action_create_calendar_event().get('context', {}) + event_from_activity = self.env['calendar.event'].with_context(action_context).create({ + 'start': '2022-07-27 14:30:00', + 'stop': '2022-07-27 16:30:00', + }) + # Check that assignation of the activity hasn't changed, and event is having + # correct values set in attendee and organizer related fields + self.assertEqual(activity.user_id, self.user_demo) + self.assertEqual(event_from_activity.partner_ids, activity.user_id.partner_id) + self.assertEqual(event_from_activity.attendee_ids.partner_id, activity.user_id.partner_id) + self.assertEqual(event_from_activity.user_id, activity.user_id) + def test_event_allday(self): self.env.user.tz = 'Pacific/Honolulu'