def _sync_google_calendar(self, calendar_service: GoogleCalendarService): self.ensure_one() if self.google_synchronization_stopped: return False full_sync = not bool(self.google_calendar_sync_token) with google_calendar_token(self) as token: try: events, next_sync_token, default_reminders = calendar_service.get_events( self.google_calendar_sync_token, token=token) except InvalidSyncToken: events, next_sync_token, default_reminders = calendar_service.get_events( token=token) full_sync = True self.google_calendar_sync_token = next_sync_token # Google -> Odoo recurrences = events.filter(lambda e: e.is_recurrence()) synced_recurrences = self.env['calendar.recurrence']._sync_google2odoo( recurrences) synced_events = self.env['calendar.event']._sync_google2odoo( events - recurrences, default_reminders=default_reminders) # Odoo -> Google recurrences = self.env['calendar.recurrence']._get_records_to_sync( full_sync=full_sync) recurrences -= synced_recurrences recurrences._sync_odoo2google(calendar_service) synced_events |= recurrences.calendar_event_ids - recurrences._get_outliers( ) events = self.env['calendar.event']._get_records_to_sync( full_sync=full_sync) (events - synced_events)._sync_odoo2google(calendar_service) return bool(events | synced_events) or bool(recurrences | synced_recurrences)
def _sync_google_calendar(self, calendar_service: GoogleCalendarService): self.ensure_one() if self.google_synchronization_stopped: return False # don't attempt to sync when another sync is already in progress, as we wouldn't be # able to commit the transaction anyway (row is locked) self.env.cr.execute( """SELECT id FROM res_users WHERE id = %s FOR NO KEY UPDATE SKIP LOCKED""", [self.id]) if not self.env.cr.rowcount: _logger.info("skipping calendar sync, locked user %s", self.login) return False full_sync = not bool(self.google_calendar_sync_token) with google_calendar_token(self) as token: try: events, next_sync_token, default_reminders = calendar_service.get_events( self.google_cal_account_id.calendar_sync_token, token=token) except InvalidSyncToken: events, next_sync_token, default_reminders = calendar_service.get_events( token=token) full_sync = True self.google_cal_account_id.calendar_sync_token = next_sync_token # Google -> Odoo events.clear_type_ambiguity(self.env) recurrences = events.filter(lambda e: e.is_recurrence()) synced_recurrences = self.env['calendar.recurrence']._sync_google2odoo( recurrences) synced_events = self.env['calendar.event']._sync_google2odoo( events - recurrences, default_reminders=default_reminders) # Odoo -> Google recurrences = self.env['calendar.recurrence']._get_records_to_sync( full_sync=full_sync) recurrences -= synced_recurrences recurrences._sync_odoo2google(calendar_service) synced_events |= recurrences.calendar_event_ids - recurrences._get_outliers( ) synced_events |= synced_recurrences.calendar_event_ids - synced_recurrences._get_outliers( ) events = self.env['calendar.event']._get_records_to_sync( full_sync=full_sync) (events - synced_events)._sync_odoo2google(calendar_service) return bool(events | synced_events) or bool(recurrences | synced_recurrences)