From 83022f1799c5a09e37be8c0fb38ea4d0a8a4d27b Mon Sep 17 00:00:00 2001 From: "Gabriel de Paula Felix (gdpf)" Date: Mon, 13 Nov 2023 14:54:03 -0300 Subject: [PATCH] [FIX] microsoft_calendar: fix recurrence being trimmed Before this commit, recurrences were being cut off after being fetched when their length exceeded the range of the current calendar view. This was because we were fetching the new events within the `get_events_delta` function, but skipping their fetch at the `_get_occurrence_details` function for occurrences outside the current calendar view range, leaving them not created when they should have been. Additionally, the last day within the weekly view was not being fetched because the API request ignored the "23:59" hour/minute information and fetched events up to the "00:00" hour/minute of the respective day. After this commit, they are now entirely created during their search in the `_get_events_delta` function call. This solves the problem of leaving occurrences uncreated and maintains the improvement of not fetching all recurrences every time as in the previous code after the performance task. In addition, to correct the substitution of the hour/minute "00:00", a day is now being added to the end date to fix the interval obtained. Benchmark test with 300 recurrences of 500 events each (150.000 events): - Before the performance task: 01:02:09 (hour/min/sec) - After this PR: 00:09:51 (hour/min/sec) Issue from task-id: 3525764 closes odoo/odoo#142014 X-original-commit: 18b978e7a87e36b21449a7981720e4f24630b1c6 Signed-off-by: Arnaud Joset (arj) Signed-off-by: Gabriel de Paula Felix (gdpf) --- .../utils/microsoft_calendar.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/addons/microsoft_calendar/utils/microsoft_calendar.py b/addons/microsoft_calendar/utils/microsoft_calendar.py index b22cb2866cd..db91998a451 100644 --- a/addons/microsoft_calendar/utils/microsoft_calendar.py +++ b/addons/microsoft_calendar/utils/microsoft_calendar.py @@ -61,7 +61,7 @@ class MicrosoftCalendarService(): end_date = self.microsoft_service._context.get('range_end_date') if start_date and end_date: start_date = start_date.strftime("%Y-%m-%dT00:00:00Z") - end_date = end_date.strftime("%Y-%m-%dT00:00:00Z") + end_date = fields.Datetime.add(end_date, days=1).strftime("%Y-%m-%dT00:00:00Z") else: ICP = self.microsoft_service.env['ir.config_parameter'].sudo() day_range = int(ICP.get_param('microsoft_calendar.sync.range_days', default=365)) @@ -110,6 +110,20 @@ class MicrosoftCalendarService(): return self._get_events_delta(token=token, timeout=timeout) raise e + # update sync min and max date range after retrieving occurrences, they must be entirely created once + # otherwise we would have to retrieve everytime all occurrences from all recurrences in _get_occurrence_details + min_start_dt = self.microsoft_service._context.get('range_start_date') + max_stop_dt = self.microsoft_service._context.get('range_end_date') + for event in events: + if event.get('type') == 'occurrence' and event.get('start') and event.get('stop'): + # get time values and update min start and max stop from sync context range + time_values = self.microsoft_service.env['calendar.event']._microsoft_to_odoo_recurrence_values(MicrosoftEvent([event])) + if not min_start_dt or time_values['start'] < min_start_dt: + min_start_dt = time_values['start'] + if not max_stop_dt or time_values['stop'] > max_stop_dt: + max_stop_dt = time_values['stop'] + self.microsoft_service = self.microsoft_service.with_context(range_start_date=min_start_dt, range_end_date=max_stop_dt) + # event occurrences (from a recurrence) are retrieved separately to get all their info, # # and mainly the iCalUId attribute which is not provided by the 'get_delta' api end point events = [e for e in events if e.get('type') != 'occurrence']