diff options
| author | Aurélien (avd) <avd@odoo.com> | 2021-03-10 15:18:34 +0000 |
|---|---|---|
| committer | Aurélien (avd) <avd@odoo.com> | 2021-04-16 08:25:42 +0000 |
| commit | 4ae90dd6f9e28e6fdff45c7612a3a756665e1489 (patch) | |
| tree | 70249c977cdb23a297d83fd269fba1d12463ace2 | |
| parent | a05244c0b2a1d9c873696f7a856c438435e4cdf5 (diff) | |
[FIX] microsoft_calendar: turn off notifications to improve perfs.
Turn off calendar.attendees notification during synced_events creation
to improve performances. The calendar notification are only computed
once after all the synced events have been created.
Add a flag in microsoft_calendar.js to avoid retriggering synchronization
while navigating the calendar view if the past synchronization is still
pending.
closes odoo/odoo#67192
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
4 files changed, 41 insertions, 2 deletions
diff --git a/addons/microsoft_calendar/models/calendar.py b/addons/microsoft_calendar/models/calendar.py index e7c647573ac..625587c03f9 100644 --- a/addons/microsoft_calendar/models/calendar.py +++ b/addons/microsoft_calendar/models/calendar.py @@ -402,3 +402,10 @@ class Meeting(models.Model): super(Meeting, my_cancelled_records)._cancel_microsoft() attendees = (self - my_cancelled_records).attendee_ids.filtered(lambda a: a.partner_id == user.partner_id) attendees.state = 'declined' + + def _notify_attendees(self): + # filter events before notifying attendees through calendar_alarm_manager + need_notifs = self.filtered(lambda event: event.alarm_ids and event.stop >= fields.Datetime.now()) + partners = need_notifs.partner_ids + if partners: + self.env['calendar.alarm_manager']._notify_next_alarm(partners.ids) diff --git a/addons/microsoft_calendar/models/calendar_recurrence_rule.py b/addons/microsoft_calendar/models/calendar_recurrence_rule.py index 19010abb81c..431e01b6430 100644 --- a/addons/microsoft_calendar/models/calendar_recurrence_rule.py +++ b/addons/microsoft_calendar/models/calendar_recurrence_rule.py @@ -4,6 +4,7 @@ import re from odoo import api, fields, models +from dateutil.relativedelta import relativedelta from odoo.addons.microsoft_calendar.utils.microsoft_calendar import MicrosoftCalendarService @@ -105,3 +106,13 @@ class RecurrenceRule(models.Model): def _ensure_attendees_have_email(self): self.calendar_event_ids.filtered(lambda e: e.active)._ensure_attendees_have_email() + + def _notify_attendees(self): + recurrences = self.filtered( + lambda recurrence: recurrence.base_event_id.alarm_ids and ( + not recurrence.until or recurrence.until >= fields.Date.today() - relativedelta(days=1) + ) and max(recurrence.calendar_event_ids.mapped('stop')) >= fields.Datetime.now() + ) + partners = recurrences.base_event_id.partner_ids + if partners: + self.env['calendar.alarm_manager']._notify_next_alarm(partners.ids) diff --git a/addons/microsoft_calendar/models/microsoft_sync.py b/addons/microsoft_calendar/models/microsoft_sync.py index 1e1147cbc78..848dd59b3b5 100644 --- a/addons/microsoft_calendar/models/microsoft_sync.py +++ b/addons/microsoft_calendar/models/microsoft_sync.py @@ -256,9 +256,12 @@ class MicrosoftSync(models.AbstractModel): dict(self._microsoft_to_odoo_values(e, default_reminders, default_values), need_sync_m=False) for e in (new - new_recurrent) ] - new_odoo = self.create(odoo_values) + new_odoo = self.with_context(dont_notify=True).create(odoo_values) - synced_recurrent_records = self._sync_recurrence_microsoft2odoo(new_recurrent) + synced_recurrent_records = self.with_context(dont_notify=True)._sync_recurrence_microsoft2odoo(new_recurrent) + if not self._context.get("dont_notify"): + new_odoo._notify_attendees() + synced_recurrent_records._notify_attendees() cancelled = existing.cancelled() cancelled_odoo = self.browse(cancelled.odoo_ids(self.env)) @@ -362,3 +365,13 @@ class MicrosoftSync(models.AbstractModel): marks the record to be re-synchronized. """ raise NotImplementedError() + + def _notify_attendees(self): + """ Notify calendar event partners. + This is called when creating new calendar events in _sync_microsoft2odoo. + At the initialization of a synced calendar, Odoo requests all events for a specific + MicrosoftCalendar. Among those there will probably be lots of events that will never triggers a notification + (e.g. single events that occured in the past). Processing all these events through the notification procedure + of calendar.event.create is a possible performance bottleneck. This method aimed at alleviating that. + """ + raise NotImplementedError() diff --git a/addons/microsoft_calendar/static/src/js/microsoft_calendar.js b/addons/microsoft_calendar/static/src/js/microsoft_calendar.js index c8e4ffac63b..dab5def93e9 100644 --- a/addons/microsoft_calendar/static/src/js/microsoft_calendar.js +++ b/addons/microsoft_calendar/static/src/js/microsoft_calendar.js @@ -21,6 +21,7 @@ const MicrosoftCalendarModel = CalendarModel.include({ init: function () { this._super.apply(this, arguments); this.microsoft_is_sync = true; + this.microsoft_pending_sync = false; }, /** @@ -38,6 +39,10 @@ const MicrosoftCalendarModel = CalendarModel.include({ */ async _loadCalendar() { const _super = this._super.bind(this); + // When the calendar synchronization takes some time, prevents retriggering the sync while navigating the calendar. + if (this.microsoft_pending_sync) { + return _super(...arguments); + } try { await Promise.race([ new Promise(resolve => setTimeout(resolve, 1000)), @@ -48,12 +53,14 @@ const MicrosoftCalendarModel = CalendarModel.include({ error.event.preventDefault(); } console.error("Could not synchronize Outlook events now.", error); + this.microsoft_pending_sync = false; } return _super(...arguments); }, _syncMicrosoftCalendar(shadow = false) { var self = this; + this.microsoft_pending_sync = true; return this._rpc({ route: '/microsoft_calendar/sync_data', params: { @@ -66,6 +73,7 @@ const MicrosoftCalendarModel = CalendarModel.include({ } else if (result.status === "no_new_event_from_microsoft" || result.status === "need_refresh") { self.microsoft_is_sync = true; } + self.microsoft_pending_sync = false; return result }); }, |
