From 642f72bfcc6fefde6445fd1842ded8fefcc09069 Mon Sep 17 00:00:00 2001 From: Adrien Widart Date: Tue, 9 Mar 2021 07:57:04 +0000 Subject: [PATCH] [FIX] microsoft_calendar: warn user if attendee without email When syncing both Odoo and Outlook Calendar, if one Odoo event has an attendee who does not have any email adress, it will lead to a traceback error. To reproduce the error: 1. Create a partner P who does not have an email address 2. In Calendar, create an event E with P 3. Sync with Outlook Error: An Odoo Error is raised. When syncing from Outlook to Odoo, it also sync the E-event (in case the latter has been updated on Outlook). The Microsoft2Odoo syncing uses the email address of each attendee. Since P does not have any, this creates the error. Although Odoo allows the use of participants without email, this is not the case for Outlook. On Outlook Calendar, as soon as a user adds an attendee to an Event, Outlook will want to send an invitation before saving this event (so, if the participant does not have any email address, it won't be possible to save the event). When syncing, the E-event will be added to Outlook Calendar. However, for the same reasons as in the previous paragraph, it will not be possible to update this event from Outlook Calendar. Therefore, the solution is to prevent the user from having attendees without email when his calendar is synchronized with Outlook. OPW-2439027 closes odoo/odoo#68407 X-original-commit: dbb65f3abfbc7d6e3d8167839659eac79b1e6b31 Signed-off-by: Yannick Tivisse (yti) Signed-off-by: Adrien Widart --- .../i18n/microsoft_calendar.pot | 9 ++++++++ addons/microsoft_calendar/models/calendar.py | 23 ++++++++++++++++++- .../models/calendar_recurrence_rule.py | 3 +++ .../models/microsoft_sync.py | 6 +++++ .../tests/test_sync_microsoft2odoo.py | 20 +++++++++++++++- 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/addons/microsoft_calendar/i18n/microsoft_calendar.pot b/addons/microsoft_calendar/i18n/microsoft_calendar.pot index 04f8dd36ea4..40855de147d 100644 --- a/addons/microsoft_calendar/i18n/microsoft_calendar.pot +++ b/addons/microsoft_calendar/i18n/microsoft_calendar.pot @@ -166,6 +166,15 @@ msgstr "" msgid "Event Recurrence Rule" msgstr "" +#. module: microsoft_calendar +#: code:addons/microsoft_calendar/models/calendar.py:0 +#, python-format +msgid "" +"For a correct synchronization between Odoo and Outlook Calendar, all attendees must have an email address. However, some events do not respect this condition. As long as the events are incorrect, the calendars will not be synchronized.\n" +"Either update the events/attendees or archive these events %s:\n" +"%s" +msgstr "" + #. module: microsoft_calendar #: model:ir.model.fields,field_description:microsoft_calendar.field_calendar_attendee__id #: model:ir.model.fields,field_description:microsoft_calendar.field_calendar_event__id diff --git a/addons/microsoft_calendar/models/calendar.py b/addons/microsoft_calendar/models/calendar.py index 233f53e544d..1c98788a596 100644 --- a/addons/microsoft_calendar/models/calendar.py +++ b/addons/microsoft_calendar/models/calendar.py @@ -6,7 +6,7 @@ from dateutil.parser import parse from dateutil.relativedelta import relativedelta from odoo import api, fields, models, _ -from odoo.exceptions import UserError +from odoo.exceptions import UserError, ValidationError ATTENDEE_CONVERTER_O2M = { 'needsAction': 'notresponded', @@ -353,6 +353,27 @@ class Meeting(models.Model): return values + def _ensure_attendees_have_email(self): + invalid_event_ids = self.env['calendar.event'].search_read( + domain=[('id', 'in', self.ids), ('attendee_ids.partner_id.email', '=', False)], + fields=['display_time', 'display_name'], + order='start', + ) + if invalid_event_ids: + list_length_limit = 50 + total_invalid_events = len(invalid_event_ids) + invalid_event_ids = invalid_event_ids[:list_length_limit] + invalid_events = ['\t- %s: %s' % (event['display_time'], event['display_name']) + for event in invalid_event_ids] + invalid_events = '\n'.join(invalid_events) + details = "(%d/%d)" % (list_length_limit, total_invalid_events) if list_length_limit < total_invalid_events else "(%d)" % total_invalid_events + raise ValidationError(_("For a correct synchronization between Odoo and Outlook Calendar, " + "all attendees must have an email address. However, some events do " + "not respect this condition. As long as the events are incorrect, " + "the calendars will not be synchronized." + "\nEither update the events/attendees or archive these events %s:" + "\n%s", details, invalid_events)) + def _microsoft_values_occurence(self, initial_values={}): values = dict(initial_values) values['id'] = self.microsoft_id diff --git a/addons/microsoft_calendar/models/calendar_recurrence_rule.py b/addons/microsoft_calendar/models/calendar_recurrence_rule.py index 55c735fa03b..b26776195b3 100644 --- a/addons/microsoft_calendar/models/calendar_recurrence_rule.py +++ b/addons/microsoft_calendar/models/calendar_recurrence_rule.py @@ -108,3 +108,6 @@ class RecurrenceRule(models.Model): values += [event_value] return values + + def _ensure_attendees_have_email(self): + self.calendar_event_ids.filtered(lambda e: e.active)._ensure_attendees_have_email() diff --git a/addons/microsoft_calendar/models/microsoft_sync.py b/addons/microsoft_calendar/models/microsoft_sync.py index cbec487ff45..5145002d992 100644 --- a/addons/microsoft_calendar/models/microsoft_sync.py +++ b/addons/microsoft_calendar/models/microsoft_sync.py @@ -129,6 +129,7 @@ class MicrosoftSync(models.AbstractModel): records_to_sync = self cancelled_records = self - records_to_sync + records_to_sync._ensure_attendees_have_email() updated_records = records_to_sync.filtered('microsoft_id') new_records = records_to_sync - updated_records for record in cancelled_records.filtered('microsoft_id'): @@ -297,6 +298,7 @@ class MicrosoftSync(models.AbstractModel): def _microsoft_patch(self, microsoft_service: MicrosoftCalendarService, microsoft_id, values, timeout=TIMEOUT): with microsoft_calendar_token(self.env.user.sudo()) as token: if token: + self._ensure_attendees_have_email() microsoft_service.patch(microsoft_id, values, token=token, timeout=timeout) self.need_sync_m = False @@ -306,6 +308,7 @@ class MicrosoftSync(models.AbstractModel): return with microsoft_calendar_token(self.env.user.sudo()) as token: if token: + self._ensure_attendees_have_email() microsoft_id = microsoft_service.insert(values, token=token, timeout=timeout) self.write({ 'microsoft_id': microsoft_id, @@ -343,6 +346,9 @@ class MicrosoftSync(models.AbstractModel): """ raise NotImplementedError() + def _ensure_attendees_have_email(self): + raise NotImplementedError() + def _get_microsoft_sync_domain(self): """Return a domain used to search records to synchronize. e.g. return a domain to synchronize records owned by the current user. diff --git a/addons/microsoft_calendar/tests/test_sync_microsoft2odoo.py b/addons/microsoft_calendar/tests/test_sync_microsoft2odoo.py index 9528666f0b1..a3922d87eb2 100644 --- a/addons/microsoft_calendar/tests/test_sync_microsoft2odoo.py +++ b/addons/microsoft_calendar/tests/test_sync_microsoft2odoo.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo.addons.microsoft_calendar.utils.microsoft_calendar import MicrosoftEvent +from odoo.addons.microsoft_calendar.utils.microsoft_calendar import MicrosoftCalendarService, MicrosoftEvent +from odoo.exceptions import ValidationError import pytz from datetime import datetime, date from odoo.tests.common import TransactionCase @@ -221,3 +222,20 @@ class TestSyncMicrosoft2Odoo(TransactionCase): events = self.env['calendar.event'].browse(event_ids).exists() self.assertFalse(recurrence, "It should remove recurrence") self.assertFalse(events, "It should remove all events") + + def test_attendees_must_have_email(self): + """ + Synching with a partner without mail raises a ValidationError because Microsoft don't accept attendees without one. + """ + MicrosoftCal = MicrosoftCalendarService(self.env['microsoft.service']) + partner = self.env['res.partner'].create({ + 'name': 'SuperPartner', + }) + event = self.env['calendar.event'].create({ + 'name': "SuperEvent", + 'start': datetime(2020, 3, 16, 11, 0), + 'stop': datetime(2020, 3, 16, 13, 0), + 'partner_ids': [(4, partner.id)], + }) + with self.assertRaises(ValidationError): + event._sync_odoo2microsoft(MicrosoftCal)