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 synchroniser_google_user(self, event, user): if user: with google_calendar_token(user.sudo()) as token: if token: _logger.warning("## token = %s" % (token)) headers = { 'Content-type': 'application/json', 'Authorization': 'Bearer %s' % token } params = {'access_token': token} values = event._google_values() _logger.warning("## values = %s" % (values)) google_service = GoogleCalendarService( self.with_user(user).env['google.service']) event_id = values.get('id') _logger.warning( "## synchroniser_google_action event_id = %s" % (event_id)) try: _logger.warning("## _google_patch event = %s" % (event)) event.with_user(user)._google_patch(google_service, event_id, values, timeout=3) except: _logger.exception( "## _google_patch ERROR event = %s" % (event))
def reset_account(self): google = GoogleCalendarService(self.env['google.service']) events = self.env['calendar.event'].search([ ('user_id', '=', self.user_id.id), ('google_id', '!=', False)]) if self.delete_policy in ('delete_google', 'delete_both'): with google_calendar_token(self.user_id) as token: for event in events: google.delete(event.google_id, token=token) if self.delete_policy in ('delete_odoo', 'delete_both'): events.google_id = False events.unlink() if self.sync_policy == 'all': events.write({ 'google_id': False, 'need_sync': True, }) self.user_id.google_cal_account_id._set_auth_tokens(False, False, 0) self.user_id.write({ 'google_calendar_sync_token': False, 'google_calendar_cal_id': False, })
def _send_mail_to_attendees(self, template_xmlid, force_send=False): """ Override If not synced with Google, let Odoo in charge of sending emails Otherwise, nothing to do: Google will send them """ with google_calendar_token(self.env.user.sudo()) as token: if not token: super()._send_mail_to_attendees(template_xmlid, force_send)
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)