def move_event(self, event, from_id, to_id): """Move an event between calendars. :param event: Event to move between calendars :type event: :class:`Event` :param str from_id: Calendar Id to move the event from :param str to_id: Calendar ID to move the event to :raises: :class:`GoogleCalendarAPIMissingID`, :class:`GoogleCalendarAPIErrorNotFound` :returns: The Google Calendar API response. :rtype: dict """ if not event.gcal_id: raise GoogleCalendarAPIMissingID() app.logger.info('[GOOGLE_CALENDAR]: Move Event') request = self.service.events().move(calendarId=from_id, eventId=event.gcal_id, destination=to_id) # Execute the request try: return self._execute_request(request) except GoogleCalendarAPIErrorNotFound as e: self.create_event(event) message = 'Move failed. Successfully fell back to create.' app.logger.warning('[GOOGLE_CALENDAR]: ' + message) raise GoogleCalendarAPIErrorNotFound(message, uri=e.uri)
def delete_event(self, event, as_exception=False): if not event.gcal_id: raise GoogleCalendarAPIMissingID() calendar_id = self._calendar_id_for_event(event) if as_exception: app.logger.info('[GOOGLE_CALENDAR]: Delete Event (as exception)') resource = GoogleCalendarResourceBuilder.event_resource(event) instance = self._instance_resource_for_event_in_series(event) instance.update(resource) instance['status'] = u'cancelled' request = self.service.events().update(calendarId=calendar_id, eventId=instance['id'], body=instance) else: app.logger.info('[GOOGLE_CALENDAR]: Delete Event') request = self.service.events().delete(calendarId=calendar_id, eventId=event.gcal_id) try: return self._execute_request(request) except HttpError as e: # If the resource has already been deleted, fail quietly. app.logger.warning(e) raise GoogleCalendarAPIEventAlreadyDeleted
def delete_event(self, event, as_exception=False): """Delete an event or series from Google Calendar, or cancel a single event from a series. :param event: The event to delete. :type event: :class:`Event` :param bool as_exception: Whether or not to cancel this event as an exception in a series. Otherwise, series' will be deleted in their entirety. :raises: :class:`GoogleCalendarAPIBadStatusLine`, :class:`GoogleCalendarAPIErrorNotFound`, :class:`GoogleCalendarAPIMissingID` :returns: The Google Calendar API response. :rtype: dict """ if not event.gcal_id: raise GoogleCalendarAPIMissingID() calendar_id = self._calendar_id_for_event(event) # Create the request if as_exception: app.logger.info('[GOOGLE_CALENDAR]: Delete Event (as exception)') resource = GoogleCalendarResourceBuilder.event_resource(event) instance = self._instance_resource_for_event_in_series(event) instance.update(resource) instance['status'] = u'cancelled' request = self.service.events().update(calendarId=calendar_id, eventId=instance['id'], body=instance) else: app.logger.info('[GOOGLE_CALENDAR]: Delete Event') request = self.service.events().delete(calendarId=calendar_id, eventId=event.gcal_id) # Execute the request, failing silently if the event has already been # deleted from Google Calendar. try: return self._execute_request(request) except GoogleCalendarAPIErrorNotFound as e: # If the resource has already been deleted, fail quietly. app.logger.warning(e) raise GoogleCalendarAPIEventAlreadyDeleted
def move_event(self, event, from_id, to_id): """Move an event between calendars. - event (Event): Event to move between calendars - from_id (string): Calendar Id to move the event from - to_id (string): Calendar ID to move the event to """ if not event.gcal_id: raise GoogleCalendarAPIMissingID() app.logger.info('[GOOGLE_CALENDAR]: Move Event') request = self.service.events().move(calendarId=from_id, eventId=event.gcal_id, destination=to_id) try: return self._execute_request(request) except GoogleCalendarAPIErrorNotFound: self._execute_request(event) raise GoogleCalendarAPIErrorNotFound( 'Move failed. Successfully fell back to create.')
def update_event(self, stale_event, as_exception=False): """""" event = Event.objects().get(id=stale_event.id) if not event.gcal_id: self.create_event(stale_event) raise GoogleCalendarAPIMissingID( 'Missing gplus_id. Successfully fell back to create.') resource = None resource = GoogleCalendarResourceBuilder.event_resource( event, for_update=True) calendar_id = self._calendar_id_for_event(event) event_id_for_update = event.gcal_id if as_exception: instance = self._instance_resource_for_event_in_series(event) instance.update(resource) resource = instance event_id_for_update = instance['id'] app.logger.info('[GOOGLE_CALENDAR]: Update Event') request = self.service.events().update(calendarId=calendar_id, eventId=event_id_for_update, body=resource) try: updated_event = self._execute_request(request) except GoogleCalendarAPIErrorNotFound as e: self.create_event(event) app.logger.warning(e.message) raise GoogleCalendarAPIErrorNotFound( 'Couldn\'t find event to update. ' 'Successfully fell back to create.') self._update_event_from_response(event, updated_event) return updated_event
def update_event(self, stale_event, as_exception=False): """Updates the event in Google Calendar. The first argument is called ``stale_event`` because it might have outdated fields. The first thing we do is find a fresh event with it's id in mongo. This method will fall back to creating a new event if we don't have reference to a ``gcal_id`` for the event, or if the update otherwise fails. :param stale_event: The event to update. :type stale_event: :class:`Event` :param bool as_exception: Whether or not this update should happen as an exception in a series. Otherwise, series' will be updated in their entirety. :raises: :class:`GoogleCalendarAPIBadStatusLine`, :class:`GoogleCalendarAPIErrorNotFound`, :class:`GoogleCalendarAPIMissingID` :returns: The Google Calendar API response. :rtype: dict """ # Freshen up stale_event event = Event.objects().get(id=stale_event.id) if not event.gcal_id: # If we don't have a reference if it's associate Google Calendar # ID, then create it fresh. This raises still because it # *shouldn't* ever happen, but it does. self.create_event(stale_event) raise GoogleCalendarAPIMissingID('Missing gplus_id. Successfully ' 'fell back to create.') resource = None resource = GoogleCalendarResourceBuilder.event_resource( event, for_update=True) calendar_id = self._calendar_id_for_event(event) # If this update should be an exception to a series of events, then # we only want to update the instance id. Otherwise, using the # ``event.gcal_id`` will update the entire series. event_id_for_update = event.gcal_id if as_exception: instance = self._instance_resource_for_event_in_series(event) instance.update(resource) resource = instance event_id_for_update = instance['id'] app.logger.info('[GOOGLE_CALENDAR]: Update Event') request = self.service.events().update(calendarId=calendar_id, eventId=event_id_for_update, body=resource) # Send the request, falling back to update if it fails. try: updated_event = self._execute_request(request) except GoogleCalendarAPIErrorNotFound as e: self.create_event(event) app.logger.warning(e.message) message = ('Couldn\'t find event to update. ' 'Successfully fell back to create.') raise GoogleCalendarAPIErrorNotFound(message) # Update the Event with the latest info from the response. self._update_event_from_response(event, updated_event) # Return the Google Calendar response dict return updated_event