def _sync_microsoft_calendar(self, calendar_service: MicrosoftCalendarService):
        self.ensure_one()
        full_sync = not bool(self.microsoft_calendar_sync_token)
        with microsoft_calendar_token(self) as token:
            try:
                events, next_sync_token, default_reminders = calendar_service.get_events(self.microsoft_calendar_sync_token, token=token)
            except InvalidSyncToken:
                events, next_sync_token, default_reminders = calendar_service.get_events(token=token)
                full_sync = True
        self.microsoft_calendar_sync_token = next_sync_token

        # Microsoft -> Odoo
        recurrences = events.filter(lambda e: e.is_recurrent())
        synced_events, synced_recurrences = self.env['calendar.event']._sync_microsoft2odoo(events, default_reminders=default_reminders) if events else (self.env['calendar.event'], self.env['calendar.recurrence'])

        # Odoo -> Microsoft
        recurrences = self.env['calendar.recurrence']._get_microsoft_records_to_sync(full_sync=full_sync)
        recurrences -= synced_recurrences
        recurrences._sync_odoo2microsoft(calendar_service)
        synced_events |= recurrences.calendar_event_ids

        events = self.env['calendar.event']._get_microsoft_records_to_sync(full_sync=full_sync)
        (events - synced_events)._sync_odoo2microsoft(calendar_service)

        return bool(events | synced_events) or bool(recurrences | synced_recurrences)
    def reset_account(self):
        microsoft = MicrosoftCalendarService(self.env['microsoft.service'])

        events = self.env['calendar.event'].search([
            ('user_id', '=', self.user_id.id), ('microsoft_id', '!=', False)
        ])
        if self.delete_policy in ('delete_microsoft', 'delete_both'):
            with microsoft_calendar_token(self.user_id) as token:
                for event in events:
                    microsoft.delete(event.microsoft_id, token=token)

        if self.delete_policy in ('delete_odoo', 'delete_both'):
            events.microsoft_id = False
            events.unlink()

        if self.sync_policy == 'all':
            events.write({
                'microsoft_id': False,
                'need_sync_m': True,
            })

        self.user_id._set_microsoft_auth_tokens(False, False, 0)
        self.user_id.write({
            'microsoft_calendar_sync_token': False,
        })
Exemple #3
0
 def _send_mail_to_attendees(self, mail_template, force_send=False):
     """ Override the super method
     If not synced with Microsoft Outlook, let Odoo in charge of sending emails
     Otherwise, Microsoft Outlook will send them
     """
     with microsoft_calendar_token(self.env.user.sudo()) as token:
         if not token:
             super()._send_mail_to_attendees(mail_template, force_send)