File size: 3,912 Bytes
550665c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
from typing import Union
from gcsa._services.base_service import BaseService
from gcsa.calendar import Calendar, CalendarListEntry
from gcsa.serializers.calendar_serializer import CalendarSerializer
class CalendarsService(BaseService):
"""Calendars management methods of the `GoogleCalendar`"""
def get_calendar(
self,
calendar_id: str = None
) -> Calendar:
"""Returns the calendar with the corresponding calendar_id.
:param calendar_id:
Calendar identifier. Default is `default_calendar` specified in `GoogleCalendar`.
To retrieve calendar IDs call the :py:meth:`~gcsa.google_calendar.GoogleCalendar.get_calendar_list`.
If you want to access the primary calendar of the currently logged-in user, use the "primary" keyword.
:return:
The corresponding :py:class:`~gcsa.calendar.Calendar` object.
"""
calendar_id = calendar_id or self.default_calendar
calendar_resource = self.service.calendars().get(
calendarId=calendar_id
).execute()
return CalendarSerializer.to_object(calendar_resource)
def add_calendar(
self,
calendar: Calendar
):
"""Creates a secondary calendar.
:param calendar:
Calendar object.
:return:
Created calendar object with ID.
"""
body = CalendarSerializer.to_json(calendar)
calendar_json = self.service.calendars().insert(
body=body
).execute()
return CalendarSerializer.to_object(calendar_json)
def update_calendar(
self,
calendar: Calendar
):
"""Updates metadata for a calendar.
:param calendar:
Calendar object with set `calendar_id`
:return:
Updated calendar object
"""
calendar_id = self._get_resource_id(calendar)
body = CalendarSerializer.to_json(calendar)
calendar_json = self.service.calendars().update(
calendarId=calendar_id,
body=body
).execute()
return CalendarSerializer.to_object(calendar_json)
def delete_calendar(
self,
calendar: Union[Calendar, CalendarListEntry, str]
):
"""Deletes a secondary calendar.
Use :py:meth:`~gcsa.google_calendar.GoogleCalendar.clear_calendar` for clearing all events on primary calendars.
:param calendar:
Calendar's ID or :py:class:`~gcsa.calendar.Calendar` object with set `calendar_id`.
"""
calendar_id = self._get_resource_id(calendar)
self.service.calendars().delete(calendarId=calendar_id).execute()
def clear_calendar(self):
"""Clears a **primary** calendar.
This operation deletes all events associated with the **primary** calendar of an account.
Currently, there is no way to clear a secondary calendar.
You can use :py:meth:`~gcsa.google_calendar.GoogleCalendar.delete_event` method with the secondary calendar's ID
to delete events from a secondary calendar.
"""
self.service.calendars().clear(calendarId='primary').execute()
def clear(self):
"""Kept for back-compatibility. Use :py:meth:`~gcsa.google_calendar.GoogleCalendar.clear_calendar` instead.
Clears a **primary** calendar.
This operation deletes all events associated with the **primary** calendar of an account.
Currently, there is no way to clear a secondary calendar.
You can use :py:meth:`~gcsa.google_calendar.GoogleCalendar.delete_event` method with the secondary calendar's ID
to delete events from a secondary calendar.
"""
self.clear_calendar()
|