File size: 5,999 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
from typing import Iterable, Union
from gcsa._services.base_service import BaseService
from gcsa.acl import AccessControlRule
from gcsa.serializers.acl_rule_serializer import ACLRuleSerializer
class ACLService(BaseService):
"""Access Control List management methods of the `GoogleCalendar`"""
def get_acl_rules(
self,
calendar_id: str = None,
show_deleted: bool = False
) -> Iterable[AccessControlRule]:
"""Returns the rules in the access control list for the calendar.
: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.
:param show_deleted:
Whether to include deleted ACLs in the result. Deleted ACLs are represented by role equal to "none".
Deleted ACLs will always be included if syncToken is provided. Optional. The default is False.
:return:
Iterable of `AccessControlRule` objects
"""
calendar_id = calendar_id or self.default_calendar
yield from self._list_paginated(
self.service.acl().list,
serializer_cls=ACLRuleSerializer,
calendarId=calendar_id,
**{
'showDeleted': show_deleted,
}
)
def get_acl_rule(
self,
rule_id: str,
calendar_id: str = None
) -> AccessControlRule:
"""Returns an access control rule
:param rule_id:
ACL rule identifier.
: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 `AccessControlRule` object
"""
calendar_id = calendar_id or self.default_calendar
acl_rule_resource = self.service.acl().get(
calendarId=calendar_id,
ruleId=rule_id
).execute()
return ACLRuleSerializer.to_object(acl_rule_resource)
def add_acl_rule(
self,
acl_rule: AccessControlRule,
send_notifications: bool = True,
calendar_id: str = None
):
"""Adds access control rule
:param acl_rule:
AccessControlRule object.
:param send_notifications:
Whether to send notifications about the calendar sharing change. The default is True.
: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:
Created access control rule with id.
"""
calendar_id = calendar_id or self.default_calendar
body = ACLRuleSerializer.to_json(acl_rule)
acl_rule_json = self.service.acl().insert(
calendarId=calendar_id,
body=body,
sendNotifications=send_notifications
).execute()
return ACLRuleSerializer.to_object(acl_rule_json)
def update_acl_rule(
self,
acl_rule: AccessControlRule,
send_notifications: bool = True,
calendar_id: str = None
):
"""Updates given access control rule
:param acl_rule:
AccessControlRule object.
:param send_notifications:
Whether to send notifications about the calendar sharing change. The default is True.
: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:
Updated access control rule.
"""
calendar_id = calendar_id or self.default_calendar
acl_id = self._get_resource_id(acl_rule)
body = ACLRuleSerializer.to_json(acl_rule)
acl_json = self.service.acl().update(
calendarId=calendar_id,
ruleId=acl_id,
body=body,
sendNotifications=send_notifications
).execute()
return ACLRuleSerializer.to_object(acl_json)
def delete_acl_rule(
self,
acl_rule: Union[AccessControlRule, str],
calendar_id: str = None
):
"""Deletes access control rule.
:param acl_rule:
Access control rule's ID or `AccessControlRule` object with set `acl_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.
"""
calendar_id = calendar_id or self.default_calendar
acl_id = self._get_resource_id(acl_rule)
self.service.acl().delete(
calendarId=calendar_id,
ruleId=acl_id
).execute()
|